2

Java プログラムでコマンドライン引数を解析するために apache-commons-cli を使用しています。

現在、一部の機密オプションまたはデバッグオプションの表示を使用法ヘルプから除外する方法を見つけようとしています。ちなみに私はHelpFormatterヘルプに使用しています。

Option first = Option.builder("f").hasArg().desc("First argument").build();
Option second = Option.builder("s").hasArg().desc("Second argument").build();
Option debug = Option.builder("d").hasArg().desc("Debug argument. Shouldn't be displayed in help").build();

commandOptions.addOption(first).addOption(second).addOption(debug);

HelpFormatter help = new HelpFormatter();
help.printHelp("Test App", commandOptions);

これはすべてのオプションを印刷しています。しかし、3番目のオプションを印刷したくありません。

実際の出力:

usage: Test App
 -d <arg>   Debug argument. Shouldn't be displayed in help // This shouldn't be displayed.
 -f <arg>   First argument
 -s <arg>   Second argument

期待される出力:

usage: Test App
 -f <arg>   First argument
 -s <arg>   Second argument

このようにして、デバッグ引数は、デバッグのためにそれについて実際に知る必要がある人だけに知られます。

ヘルプ出力のみから特定のオプションを無効にする方法はありますか。しかし、他のオプションと同じように解析しますか?

ちなみに使っcommons-cli-1.3.1.jarています。

4

1 に答える 1

4

私が見る限り、HelpFormatterこのようなもののためにサブクラス化されることは意図されておらず、特にappendOption()プライベートであるため、オプションを除外することはできません。

したがって、単純に 2 つのOptionsオブジェクトを作成します。1 つはコマンドライン オプションの実際の解析用で、もう 1 つはヘルプの出力用です。つまり、

Option first = Option.builder("f").hasArg().desc("First argument").build();
Option second = Option.builder("s").hasArg().desc("Second argument").build();
Option debug = Option.builder("d").hasArg().desc("Debug argument. Shouldn't be displayed in help").build();

commandOptions.addOption(first).addOption(second).addOption(debug);

helpOptions.addOption(first).addOption(second);
HelpFormatter help = new HelpFormatter();
help.printHelp("Test App", helpOptions);
于 2016-03-12T06:48:53.320 に答える