1

ここではApache Commons CLI 1.2を使用します。2 つのランタイム オプションを必要とする実行可能 JAR がfizzありbuzzます。どちらも引数/値を必要とする文字列です。私は(可能限り)私のアプリを次のように実行したいと思います:

java -jar myapp.jar -fizz "よし、じゃあ!" -バズ「さようなら、さようなら!」

この場合、fizzオプションの値は「Alrighty, then!」などになります。

これが私のコードです:

public class MyApp {
    private Options cmdLineOpts = new Options();
    private CommandLineParser cmdLineParser = new GnuParser();
    private HelpFormatter helpFormatter = new HelpFormatter();

    public static void main(String[] args) {
        MyApp myapp = new MyApp();
        myapp.processArgs(args);
    }

    private void processArgs(String[] args) {
        Option fizzOpt = OptionBuilder
            .withArgName("fizz")
            .withLongOpt("fizz")
            .hasArg()
            .withDescription("The fizz argument.")
            .create("fizz");

        Option buzzOpt = OptionBuilder
            .withArgName("buzz")
            .withLongOpt("buzz")
            .hasArg()
            .withDescription("The buzz argument.")
            .create("buzz");

        cmdLineOpts.addOption(fizzOpt);
        cmdLineOpts.addOption(buzzOpt);

        CommandLine cmdLine;

        try {
            cmdLine = cmdLineParser.parse(cmdLineOpts, args);

            // Expecting to get a value of "Alright, then!"
            String fizz = cmdLine.getOptionValue("fizz");
            System.out.println("Fizz is: " + fizz);
        } catch(ParseException parseExc) {
            helpFormatter.printHelp("myapp", cmdLineOpts, true);
            throw parseExc;
        }
    }
}

これを実行すると、次の出力が得られます。

フィズは: null

アプリを思い通りに起動できるようにするには、コードをどうする必要がありますか? または、私がそれに到達できる最も近いものは何ですか?

ボーナスポイント:と引数OptionBuilderの違いを誰かが説明してくれたら、withArgName(...)次のように同じ値を渡しているので:withLongOpt(...)create(...)

Option fizzOpt = OptionBuilder
    .withArgName("fizz")
    .withLongOpt("fizz")    }   Why do I have to pass the same value in 3 times to make this work?!?
    .create("fizz");
4

1 に答える 1

3

最初に.hasArg()OptionBuilder の は、paramter フラグの後に引数が必要であることを伝えます。

このコマンドラインで動作するようになりました

--fizz "VicFizz is good for you" -b "VicBuzz is also good for you"

次のコードを使用して-これをコンストラクターに入れました

Option fizzOpt = OptionBuilder
        .withArgName("Fizz")
        .withLongOpt("fizz")
        .hasArg()
        .withDescription("The Fizz Option")
        .create("f");

cmdLineOpts.addOption(fizzOpt);
cmdLineOpts.addOption("b", true, "The Buzz Option");

壊す

オプションの設定は、コマンド ラインでの使いやすさを向上させるために必要であり、使用方法のメッセージも表示されます (以下を参照)。

  • .withArgName("Fizz"): 使用法で引数に適切なタイトルを付けます (以下を参照)。
  • .withLongOpt("fizz"): 可能--fizz "VicFizz is good for you"
  • .create("f"): はメイン パラメータで、コマンド ラインを使用できます-f "VicFizz is good for you"
  • fuzz のオプション b は、使用中の可読性を犠牲にして、はるかに単純に構築されていることに注意してください。

使用上のメッセージ

個人的には、素敵な使い方を出力する CLI プログラムが大好きです。でこれを行うことができますHelpFormatter。例えば:

private void processArgs(String[] args) {
  if (args == null || args.length == ) {
    helpFormatter.printHelp("Don't you know how to call the Fizz", cmdLineOpts);
    ...

これにより、次のような便利なものが出力されます。

usage: Don't you know how to call the Fizz
  -b <arg>          The Buzz Option
  -f,--fizz <Fizz>  The Fizz Option

説明とともに、短いオプション-f、長いオプション--fizz、および名前<Fizz>がどのように表示されるかに注意してください。

お役に立てれば

于 2015-05-08T15:22:54.070 に答える