1. 程式人生 > >java-構建jar帶喲參數提示的

java-構建jar帶喲參數提示的

dir true final form clas nal pos tid direct

使用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帶喲參數提示的