2016-12-15 22 views
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); 

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 

どのようにすれば、期待される出力を得ることができますか?

+0

私は「アパッチ・コモンズ-CLI」に精通していないですが、オプションを作成するためにはに関連すると思われるがプリント注文。したがって、上記の最後のオプションを1つ作成する必要があります。 – Seelenvirtuose

+0

私はオプションのオブジェクトに順番にオプションを追加しています。 –

+0

はい、オプションオブジェクトに入れるよりも別の順序で作成しています。答えはこれを明らかに説明しています。 – Seelenvirtuose

答えて

2

私はthisがあなたが探しているものだと信じています。

ます。public void setOptionComparator(コンパレータコンパレータは)オプションをソートするために使用 コンパレータを設定したときに彼らのヘルプテキストで出力します。 ヌルコンパレータを渡すと、 が宣言された順にオプションが保持されます。パラメータ:コンパレータ - コンパレータはオプションに

参考ソート に使用する:https://commons.apache.org/proper/commons-cli/javadocs/api-release/org/apache/commons/cli/HelpFormatter.html