【问题标题】:How to get console-app argument with no option name in Commons cli?如何在 Commons cli 中获取没有选项名称的控制台应用程序参数?
【发布时间】:2016-11-09 16:18:48
【问题描述】:

这是我的控制台输入:

java TCPPing –p –port 9900 –mps 30 –size 1000 pcB

pcB 是无名称选项的参数。

我如何获得没有选项的 pcB 参数?

小代码部分:

Options options = new Options();
options.addOption("", "hostname", true, "Hostname"); //no option name
options.addOption("port", "port", true, "TCP socket used for connection");

CommandLineParser parser = new DefaultParser();
CommandLine commandLine = parser.parse(options, args);

Integer port = Integer.parseInt(cmd.getOptionValue("port")); //works fine
String hostname = cmd.getOptionValue("hostname"); //doesn't work

【问题讨论】:

    标签: java console-application apache-commons-cli


    【解决方案1】:

    看看CommandLine.getArgList(),或CommandLine.getArgs()。他们应该做您正在寻找的事情,即他们返回任何未作为其他选项的一部分解析的“剩余”参数。

    List<String> args = cmd.getArgList();
    

    请参阅https://commons.apache.org/proper/commons-cli/javadocs/api-release/org/apache/commons/cli/CommandLine.html#getArgList-- 了解完整说明。

    【讨论】:

    • 谢谢!我希望有一种方法可以用空字符串解析该参数,但我想没有。
    猜你喜欢
    • 2018-01-21
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 2015-05-04
    • 1970-01-01
    • 2011-05-22
    相关资源
    最近更新 更多