首页 > 编程语言 > 详细

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

时间:2018-02-10 23:52:27      阅读:321      评论:0      收藏:0      [点我收藏+]

使用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);

    }

}

 

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

原文:https://www.cnblogs.com/wenbronk/p/8440037.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!