【JAVA】Java 命令行参数解析

jar 包maven依赖

 <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.2</version>
        </dependency>
package pres.ndz.simple;


import org.apache.commons.cli.*;

/**
 * Hello world!
 *
 */
public class App {



    public static void main(String[] args) throws ParseException {


        CommandLineParser parser = new BasicParser();
        Options options = new Options();

        // 使用 $ java -jar App.jar -h
        options.addOption("h","help",false,"help info");
        // $java -jar App.jar --file test.txt
        options.addOption("f","file",true,"file output");


        CommandLine commandLine = parser.parse(options,args);

        if (commandLine.hasOption("h")){
            System.out.println("Help Message");
            System.exit(0);
        }

        if (commandLine.hasOption("f")){
            System.out.println(commandLine.getOptionValue("f"));
        }


    }


}

posted @ 2019-05-26 18:34  加州水果  阅读(2356)  评论(0编辑  收藏  举报