命令行参数不按顺序排列

时间:2016-12-15 13:21:08

标签: java apache-commons-cli

我使用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);

当我在options对象上调用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

任何人都可以告诉我,我怎样才能获得预期的输出?

1 个答案:

答案 0 :(得分:2)

我相信this正是您所寻找的。

  

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

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