命令行参数不按顺序

Command line arguments are not in order

我正在使用 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

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

我相信 this 就是您要找的。

public void setOptionComparator(Comparator comparator) Set the comparator used to sort the options when they output in help text. Passing in a null comparator will keep the options in the order they were declared. Parameters: comparator - the Comparator to use for sorting the options

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