Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 命令行参数不符合顺序_Java_Apache Commons Cli - Fatal编程技术网

Java 命令行参数不符合顺序

Java 命令行参数不符合顺序,java,apache-commons-cli,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,

我正在使用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);
Option option1=Option.builder(“A”).longOpt(“几乎所有”).desc(“不列出隐含的和…”)。hasArg(false)
.build();
Option option2=Option.builder(“b”).longOpt(“块大小”).argName(“大小>我相信这就是您要找的

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


Ref:

我不熟悉“apache commons cli”"但是,创建选项的顺序似乎与打印顺序相关。因此,您必须在上面的一步中创建最后一个选项。我正在将选项添加到order to options对象中。是的,但是您正在以另一个顺序创建选项,而不是将其放入options对象中。答案显然对此进行了解释。感谢您的回复。下面是选项对象的创建顺序在这里并不重要。
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