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));

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值