2

我正在使用 Common-CLI api 来显示命令的帮助。以下函数显示命令的帮助。

public static void printHelp(final Options options, final int width, final String cmdLineSyntax,
        final String header, final String footer, final int leftPad, final int descPad, final boolean autoUsage,
        final OutputStream out) {
    PrintWriter writer = new PrintWriter(out);
    final HelpFormatter helpFormatter = new HelpFormatter();
    helpFormatter.printHelp(writer, width, cmdLineSyntax, header, options, leftPad, descPad, footer, autoUsage);
    writer.flush();
}

我按以下顺序添加了选项。

Option option1 = Option.builder("A").longOpt("almost-all").desc("do not list implied . and ..").hasArg(false)
                .build();

        Option option2 = Option.builder("b").longOpt("block-size").argName("SIZE> <CAPACITY> <LINE").numberOfArgs(3)
                .desc("use SIZE-byte blocks").hasArg(true).build();

        Option option3 = Option.builder("c")
                .desc("with -lt: sort by, and show, ctime (time of last modification of file status information) with -l:show ctime and sort by name otherwise: sort by ctime")
                .hasArg(false).build();

        Options options = new Options();
        options.addOption(option1);
        options.addOption(option2);
        options.addOption(Option.builder().longOpt("escape").desc("print octal escapes for nongraphic characters")
                .hasArg(false).build());

        options.addOption(option3);

当我在选项对象上调用 printHelp 函数时,我得到以下输出。

usage: ls [-A] [-b <SIZE> <CAPACITY> <LINE>] [-c] [--escape]
     -A,--almost-all                              do not list implied . and ..
     -b,--block-size <SIZE> <CAPACITY> <LINE>     use SIZE-byte blocks
     -c                                           with -lt: sort by, and show, ctime (time of last
                                                  modification of file status information) with
                                                  -l:show ctime and sort by name otherwise: sort by
                                                  ctime
        --escape                                  print octal escapes for nongraphic characters

但我期待以下方式。

usage: ls [-A] [-b <SIZE> <CAPACITY> <LINE>] [-c] [--escape]
     -A,--almost-all                              do not list implied . and ..
     -b,--block-size <SIZE> <CAPACITY> <LINE>     use SIZE-byte blocks
        --escape                                  print octal escapes for nongraphic characters
     -c                                           with -lt: sort by, and show, ctime (time of last
                                                  modification of file status information) with
                                                  -l:show ctime and sort by name otherwise: sort by
                                                  ctime

谁能告诉我,我怎样才能得到预期的输出?

4

1 回答 1

2

我相信就是您正在寻找的。

public void setOptionComparator(Comparator compare) 设置用于在帮助文本中输出选项时对选项进行排序的比较器。传入一个空比较器将使选项保持它们被声明的顺序。参数:comparator - 用于对选项进行排序的比较器

参考:https ://commons.apache.org/proper/commons-cli/javadocs/api-release/org/apache/commons/cli/HelpFormatter.html

于 2016-12-15T13:29:09.863 回答