【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"));
        }


    }


}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM