【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