3

Javaコマンドラインユーティリティに渡されたコマンドライン引数を解析するためにApache commons-cliを試しています。

「-r」と「-R」の両方が、パーサーに 2 つのオプションを追加せずに「再帰サブディレクトリ」を意味する方法はありますか (使用状況の出力が台無しになります)。

いくつかのコード:

Options options = new Options();
options.addOption("r", "recurse", false,"recurse subdirectories");
CommandLineParser parser = new BasicParser();
CommandLine cmd = null;

try {
    cmd = parser.parse( options, args);

} catch (ParseException e) {
    e.printStackTrace();
    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp("readfiles", options);
}
4

1 に答える 1

0

このオプションは、現時点では commons-cli の一部として存在しません。
今のところ、これを行う必要があります。

public static void main( String[] args )
{
    Options options = new Options();
    options.addOption("v", "version", false, "Run with verbosity set to high")
           .addOption("h", "help", false, "Print usage")
           .addOption("r", "recurse", false, "Recurse subdirectories")
           .addOption("R", false, "Same as --recurse");

    CommandLine cmd = null;
    CommandLineParser parser = new PosixParser();
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp("cmdline-parser [OPTIONS] [FILES]", options);

}

結果の使用情報は次のとおりです。

使用法: cmdline-parser [オプション] [ファイル]
 -h,--help 印刷の使用法
 -r,--recurse 再帰サブディレクトリ
 -R --recurse と同じ
 -v,--version 冗長性を高く設定して実行
于 2014-07-19T16:19:39.303 に答える