如果我想解析这个:
java MyProgram -r opt1 -S opt2 arg1 arg2 arg3 arg4 --test -A opt3
我想要的程序结果是:
regular Java args[] of size=4
org.apache.commons.cli.Options[] of size=3
org.apache.commons.cli.Options[] #2 of size=1
我更喜欢使用 Apache Commons CLI ,但是文档对于我上面介绍的情况有点不清楚。具体来说,文档没有告诉您如何处理我在下面指定的第三种类型的选项:
<前><代码>1。带有“-”字符的选项
2.带有“--”字符的选项
3. 没有任何标记的选项,或“裸参数”
我希望 Apache Commons CLI 能够工作,但如果这些参数没有选项前缀,仍然能够将常规参数传递给程序。也许确实如此,但当我阅读它时,文档并没有这么说......
What if I wanted to parse this:
java MyProgram -r opt1 -S opt2 arg1 arg2 arg3 arg4 --test -A opt3
And the result I want in my program is:
regular Java args[] of size=4
org.apache.commons.cli.Options[] of size=3
org.apache.commons.cli.Options[] #2 of size=1
I would prefer to use Apache Commons CLI, but the documentation is a little unclear about the case I present above. Specifically, the documentation doesn't tell you how to handle options of the 3rd type I specify below:
1. options with a "-" char
2. options with a "--" char
3. options without any marker, or "bare args"
I wish that Apache Commons CLI would work but STILL be able to pass regular args to the program if those args didn't have a option prefix. Maybe it does but the documentation doesnt say so as I read through it...
发布评论
评论(10)
使用 Apache Commons CLI 库 commandline.getArgs() 获取 arg1、arg2、arg3和arg4。这是一些代码:
Use the Apache Commons CLI library commandline.getArgs() to get arg1, arg2, arg3, and arg4. Here is some code:
你可以手动完成。
注意: 对于 opts 使用 HashMap 而不是内部类可能会更好。
You could just do it manually.
NB: might be better to use a HashMap instead of an inner class for the opts.
我喜欢这个。很简单,每个参数可以有多个参数:
例如:
I like this one. Simple, and you can have more than one parameter for each argument:
For example:
我意识到这个问题提到了对 Commons CLI 的偏好,但我猜想当这个问题被问到时,在 Java 命令行解析库方面没有太多选择。但九年后,即 2020 年,你难道不愿意编写如下代码吗?
通过运行问题中的命令来执行:
我喜欢这段代码的原因在于它是:
call
方法中的业务逻辑没有解析相关的逻辑--help
和--version
选项进行自动使用和版本帮助上述功能只是您使用 picocli 时获得的部分功能(https://picocli.info) 库。
现在,请记住,作为 picocli 的作者,我完全、完全、完全有偏见。 :-)
但我确实相信,到 2020 年,我们有比 Commons CLI 更好的替代方案来构建命令行应用程序。
I realize that the question mentions a preference for Commons CLI, but I guess that when this question was asked, there was not much choice in terms of Java command line parsing libraries. But nine years later, in 2020, would you not rather write code like the below?
Execute by running the command in the question:
What I like about this code is that it is:
call
method is free of parsing-related logic--help
and--version
optionsThe above functionality is only part of what you get when you use the picocli (https://picocli.info) library.
Now, bear in mind that I am totally, completely, and utterly biased, being the author of picocli. :-)
But I do believe that in 2020 we have better alternatives for building a command line apps than Commons CLI.
这是升级到 Commons CLI 1.3.1 合规性的 @DwB 解决方案(替换了已弃用的组件 OptionBuilder 和 GnuParser)。 Apache 文档使用的示例在现实生活中具有未标记/裸参数,但会忽略它们。感谢@DwB 展示它是如何工作的。
输出:
Here is @DwB solution upgraded to Commons CLI 1.3.1 compliance (replaced deprecated components OptionBuilder and GnuParser). The Apache documentation uses examples that in real life have unmarked/bare arguments but ignores them. Thanks @DwB for showing how it works.
Output:
您可以使用 https://github.com/jankroken/commandline ,具体操作方法如下
:这个例子有效,我必须对参数的含义做出假设 - 只需在这里选择一些东西......
然后可以这样设置:
然后在 main 方法中,你可以这样做:
You could use https://github.com/jankroken/commandline , here's how to do that:
To make this example work, I must make assumptions about what the arguments means - just picking something here...
this can then be set up this way:
and then in the main method, you can just do:
您可以在 refcodes-console 处使用
refcodes-console
工件在 REFCODES.ORG 上:使用您的根条件创建参数解析器
ArgsParserImpl
:上面定义语法,下面调用解析器:
如果您提供了一些好的描述,
theArgsParser.printUsage()
甚至会向您显示漂亮的打印用法:theRoot = new AndConditionImpl( r , s, a, arg1, arg2, arg3, arg4, newOptionalImpl( test ) );
那么您的语法如下所示:
您可以在 StackOverFlowExamle。您可以使用 AND、OR、XOR 条件和任何类型的嵌套...希望这会有所帮助。
You could use the
refcodes-console
artifact at refcodes-console on REFCODES.ORG:Create your arguments parser
ArgsParserImpl
with your root condition:Above you define your syntax, below you invoke the parser:
In case you provided some good descriptions,
theArgsParser.printUsage()
will even show you the pretty printed usage:theRoot = new AndConditionImpl( r, s, a, arg1, arg2, arg3, arg4, new OptionalImpl( test ) );
Then your syntax looks as follows:
The full example for your case you find in the StackOverFlowExamle. You can use AND, OR, XOR conditions and any kind of nesting ... hope this helps.
好的,感谢查尔斯·古德温提出的概念。答案如下:
Ok, thanks to Charles Goodwin for the concept. Here is the answer:
您也可以使用 ParameterTool 来完成
Also you can do it with ParameterTool
java中命令行的简单代码:
Simple code for command line in java: