Commons CLI 打印帮助信息

CLI
本文将介绍怎样利用 Apache Commons CLI 工具打印程序命令行使用帮助信息。

我们平常在 Windows DOS 或者 Linux 系统中,我们在使用某个命令进行一些操作时,如果命令参数输入错误。此时,这个命令将自动输出帮助信息(或则自己借助 --help 或 -h 指令手动打印帮助信息),例如:使用 ping 命令,但是 ping 命令后面不跟 IP 地址或域名

Windows

C:\Users\Administrator>ping

用法: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
            [-r count] [-s count] [[-j host-list] | [-k host-list]]
            [-w timeout] [-R] [-S srcaddr] [-c compartment] [-p]
            [-4] [-6] target_name

选项:
    -t             Ping 指定的主机,直到停止。
                   若要查看统计信息并继续操作,请键入 Ctrl+Break;
                   若要停止,请键入 Ctrl+C。
    -a             将地址解析为主机名。
...

Linux

hxstrive@ubuntu:~/apache-tomcat-7.0.64$ ping
ping: usage error: Destination address required

hxstrive@ubuntu:~/apache-tomcat-7.0.64$ ping -h
Usage
  ping [options] <destination>

Options:
  <destination>      dns name or ip address
  -a                 use audible ping
  -A                 use adaptive ping
...

如果我们自己写了一个命令行程序,该怎么打印这些帮助信息呢?我们首先想到的就是,定义一个方法,专门打印命令帮助信息,然后进行字符串拼接,逐行打印出来即可。其实,还有更好的方式,就是借助 Apache Commons CLI 工具,该工具还可以帮助我们分析命令行参数。

示例代码

import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;

/**
 * 使用 Commons CLI 库输出指定程序的使用帮助信息
 * @author hxstrive.com 2021/11/15
 */
public class Demo2 {

    public static void main(String[] args) {
        // 声明程序选项
        Options options = new Options();
        // 格式:addOption(
        //          选项的简短表示,例如:-t,
        //          选项是否拥有值,true-有值,false-没有值,
        //          选项描述信息)
        options.addOption(new Option("b",
                false, "display current time(boolean)"));

        // 格式:addOption(
        //          选项的简单表示,例如:-t,
        //          选项的繁琐表示,例如:-time,
        //          选项是否拥有值,true-有值,false-没有值,
        //          选项描述信息)
        options.addOption(new Option("t", "text",
                true, "use given information(String)"));
        options.addOption(new Option("s", "size",
                true, "use given size(Integer)"));
        options.addOption(new Option("f", "file",
                true, "use given file(File)"));

        // 定义一个属性格式的选项,如: -D name=hxstrive.com
        Option property = OptionBuilder.withArgName("property=value").hasArgs(2)
                .withValueSeparator()
                .withDescription("use value for given property(property=value)").create("D");
        property.setRequired(true);
        options.addOption(property);

        // 输出帮助信息
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("Demo2", options);
    }

}

执行 “java Demo2” 命令,输出如下:

usage: Demo2
 -b                    display current time(boolean)
 -D <property=value>   use value for given property(property=value)
 -f,--file <arg>       use given file(File)
 -s,--size <arg>       use given size(Integer)
 -t,--text <arg>       use given information(String)
一寸光阴一寸金,寸金难买寸光阴。——《增广贤文》
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号