java-构建jar带哟参数提示的

使用command的cli包构建带有参数提示的jar包

需要引入command cli的依赖

<commons.version>1.2</commons.version>
        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>${commons.version}</version>
        </dependency>

 

 

package com.wenbronk.storm.test.optional;

import org.apache.commons.cli.*;
import org.apache.storm.shade.org.apache.commons.lang.StringUtils;

import java.util.function.IntPredicate;

public class OptionTest {

    /**
     * Options使用
     * @return
     */
    private final static Options buildOptions() {
        Options options = new Options();
        options.addOption("intype", true, "[options] txt(default)");
        options.addOption("inn", true, "[required] input original data");
        options.addOption("out", true, "[required] output directory");
        return options;
    }

    private final static void printUsage(Options options) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("OptionsUsage", options);
    }

    public static void main(String[] args) throws ParseException {
        Options options = buildOptions();

        BasicParser basicParser = new BasicParser();
        CommandLine commandLine = basicParser.parse(options, args);

        if (!commandLine.hasOption("intype") && !commandLine.hasOption("out")) {
            printUsage(options);
            return;
        }

        String in = commandLine.getOptionValue("inn");
        if (StringUtils.isNotEmpty(in) && !in.endsWith("txt")) {
            printUsage(options);
            return;
        }
        System.out.println("in  " + in);

        // 默认txt格式
        String intype = commandLine.getOptionValue("intype", "txt");
        System.out.println("intype  " + intype);

        String output = commandLine.getOptionValue("out");
        if(output.endsWith("/")){
            output = output.substring(0, output.length()-1);
        }
        System.out.println("output  " + output);

    }

}

 

或者使用commandline

package com.babytree.ask.conf;

import com.beust.jcommander.Parameter;

import java.util.List;

public class CmdConfig {
    @Parameter(names = {"-h", "--help"}, help = true)
    private boolean help;
    @Parameter(names = "--dev", description = "enable dev mode")
    private boolean dev;
    @Parameter(names = {"-c", "--config"}, description = "config file path")
    private String configFile;
    @Parameter(names = "-m", description = "启动的模块", converter = ModuleInfoConverter.class)
    private List<ModuleInfo> moduleInfos;

    public boolean isHelp() {
        return help;
    }

    public CmdConfig setHelp(boolean help) {
        this.help = help;
        return this;
    }

    public boolean isDev() {
        return dev;
    }

    public CmdConfig setDev(boolean dev) {
        this.dev = dev;
        return this;
    }

    public String getConfigFile() {
        return configFile;
    }

    public CmdConfig setConfigFile(String configFile) {
        this.configFile = configFile;
        return this;
    }

    @Override
    public String toString() {
        return "CmdConfig{" +
                ", help=" + help +
                ", dev=" + dev +
                ", configFile='" + configFile + '\'' +
                '}';
    }
}

 

package com.babytree.ask.conf;

import com.beust.jcommander.IStringConverter;
import org.apache.commons.lang.StringUtils;

public class ModuleInfoConverter implements IStringConverter<ModuleInfo> {
    @Override
    public ModuleInfo convert(String value) {
        if (StringUtils.isBlank(value)) {
            throw new IllegalArgumentException("module info is empty");
        }
        String name = value;
        String configPath = null;

        int index = value.indexOf(':');
        if (index == -1) {
            name = value.substring(0, index);
            configPath = value.substring(index + 1);
        }

        ModuleInfo info = new ModuleInfo();
        info.setConfigPath(configPath);
        info.setName(name);
        return info;
    }
}

 

CommandInfo main = new CommandInfo();
        JCommander jCommander = JCommander.newBuilder()
                .addObject(main)
                .build();
        jCommander.parse(args);

        if (main.isHelp()) {
            jCommander.usage();
            System.exit(0);
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值