RocketMQ源碼分析準備知識之CommonsCli

RocketMQ4.7.0版本使用了

<dependency>

    <groupId>commons-cli</groupId>

    <artifactId>commons-cli</artifactId>

    <version>1.2</version>

 </dependency>

即採用了apache的命令行工具包

比如:org.apache.rocketmq.namesrv.NamesrvStartup類中

入門例子

import org.apache.commons.cli.*;

/**

* 以前寫過一些命令行程序,在需要帶參數的時候都是自己來判斷args,導致程序光解析*  args都佔了好大一堆,而且解析代碼也不美觀。

  * 偶然間發現了apache公共庫中的cli庫,在這裏分享給大家。

  *  commons-cli中把解釋參數分爲三種狀態,分別是定義、解釋和詢問交互。

  */

public class CLI001 {
    public static void main(String[] args) throws ParseException {
        //定義:在Java代碼中定義Optin參數,定義參數、是否需要輸入值、簡單的描述等
        Options options = new Options();
//      本質還是調用new Option(opt, longOpt, hasArg, description),
//        第一個參數:參數的簡單形式 opt
//        第二個參數:參數的複雜形式 longOpt
//        第三個參數:是否需要額外的輸入
//        第四個參數:對參數的描述信息
        options.addOption("h",false,"list help");//false代表不強制有
        options.addOption("t",true,"set time on system"); //true 代表

        //解析:應用程序傳入參數後,CLI進行解析
        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(options,args);

        //查詢交互:通過查詢CommandLine詢問進入到哪個程序分支中
        //在引用此jar包,你的程序應當寫在這裏(根據模塊不同編寫不同邏輯),從這裏啓動
        if (cmd.hasOption("h")){
            String formatstr = "CLI  cli  test";
            HelpFormatter hf = new HelpFormatter();
            hf.printHelp(formatstr, "", options, "");
            return;
        }

        if (cmd.hasOption("t")){
            System.out.printf("system time has setted  %s \n",cmd.getOptionValue("t"));
            return;
        }

        System.out.println("error");
    }
}

測試類:

public class Cl002 {

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

        Long now = System.currentTimeMillis();

//        String argss[]={"-t  " + now};

        String argss[]={"-h"};

        CLI001.main(argss);

    }

}

RocketMQ的例子

import org.apache.commons.cli.*;

import java.util.Properties;

public class ServerUtil {

    public static Options buildCommandlineOptions(final Options options) {

        Option opt = new Option("h", "help", false, "Print help");

        opt.setRequired(false);

        options.addOption(opt);

        opt =

            new Option("n", "namesrvAddr", true,

                "Name server address list, eg: 192.168.0.1:9876;192.168.0.2:9876");

        opt.setRequired(false);

        options.addOption(opt);

        return options;

    }

 

    public static CommandLine parseCmdLine(final String appName, String[] args, Options options,

        CommandLineParser parser) {

        HelpFormatter hf = new HelpFormatter();

        hf.setWidth(110);

        CommandLine commandLine = null;

        try {

            commandLine = parser.parse(options, args);

            if (commandLine.hasOption('h')) {

                hf.printHelp(appName, options, true);

                System.exit(0);

            }

        } catch (ParseException e) {

            hf.printHelp(appName, options, true);

            System.exit(1);

        }

        return commandLine;

    }

 

    public static void printCommandLineHelp(final String appName, final Options options) {

        HelpFormatter hf = new HelpFormatter();

        hf.setWidth(110);

        hf.printHelp(appName, options, true);

    }

 

    public static Properties commandLine2Properties(final CommandLine commandLine) {

        Properties properties = new Properties();

        Option[] opts = commandLine.getOptions();

        if (opts != null) {

            for (Option opt : opts) {

                String name = opt.getLongOpt();

                String value = commandLine.getOptionValue(name);

                if (value != null) {

                    properties.setProperty(name, value);

                }

            }

        }

        return properties;

    }

 

    public static void main(String[] args) {

        Options options = buildCommandlineOptions(new Options());

        printCommandLineHelp("allen's appName",options);

//        String argss[]={"h"};

        String argss[]={"-n 152.168.1.88:9876"};

        CommandLineParser parser = new PosixParser();

//        CommandLine cmd = parser.parse(options,args);

        CommandLine commandLine = parseCmdLine("ALLEN'S App", argss, options, parser);

        System.out.println(commandLine);

//        {namesrvAddr= 152.168.1.88:9876}

        System.out.println(commandLine2Properties(commandLine));

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章