2015-05-08 26 views
1

在这里使用Apache Commons CLI 1.2。我有一个可执行JAR需要2个运行时选项,fizzbuzz;两者都是需要参数/值的字符串。我会像(如果可能的话),我的应用程序可以像这样执行:Commons CLI不尊重我的命令行安装

Java的罐子myapp.jar -fizz “Alrighty的话!” “现在保重,再见!”

在这种情况下,对于fizz选项的值将是 “Alrighty,然后!” 等

这里是我的代码:

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; 
     } 
    } 
} 

当我运行此我得到以下输出:

Fizz是:null

我需要对我的代码做些什么才能以我想要的方式调用我的应用程序?或者我可以最接近它的是什么?

加分点:如果有人能向我解释了OptionBuilderwithArgName(...)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"); 
+1

1)您的代码不编译(一人失踪分号,没有宣布例外)。 2)对我来说它有效。我得到了预期的'Fizz是:好吧,那么!'。 – Seelenvirtuose

+0

谢谢@Seelenvirtuose(+1) - 对分号(复制n粘贴错误)感到抱歉,我添加了它。你说它适合你吗?你能粘贴你正在使用的确切的命令行调用吗?再次感谢! – smeeb

回答

3

首先在你的OptionBuilder上的.hasArg()告诉它你期望在参数标志之后有一个参数。

我得到了它使用此命令行

--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"
  • 注意 模糊的选项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>显示,与描述。

希望这有助于