英文:
How to parse some of the command line arguments to a single option in Java
问题
我了解如何使用args4j直接解析命令行字符串以及带选项的情况。但是我找不到如何解析多个参数传递给单个选项的方法。我的意思是像这样的命令行:
java -jar myprog.jar -i firstFile secondFile thirdFile -o outputFile
所以基本上我想为输入选项传递不确定数量的参数。
谢谢。
英文:
I understand how I can parse command line strings directly and with options using args4j. But I couldn't find how to parse several arguments to a single options. What I mean is a command line like that:
java -jar myprog.jar -i firstFile secondFile thirdFile -o outputFile
So basically I want to pass undefined number of arguments for the input option.
Thx.
答案1
得分: 3
args4j无法做到这一点,我认为。我不会使用args4j,它已经过时了。
尝试使用jcommander。它的思想与args4j完全相同,但与args4j不同的是,你可以在List<T>
上使用jcommander中的@Option
。请参阅文档的第2.2节(点击链接,转到2.2节。:P)
然而:请注意,如果我照字面意思理解你的问题,你正在问的是非常不可取的!
在类似posix风格的命令行参数中,你的字符串被解析为:
-i firstFile
- 好的,firstFile是-i选项的参数。secondFile
- 好的,这与-i无关,这是一个文件参数。thirdFile
- 同样。-o outFile
- outFile是-o开关的参数值。
你可能想要的,以及jcommander的工作方式,是你需要编写:
java -jar myprog.jar -i firstFile -i secondFile -i thirdFile -o outFile
如果你真的想让-i firstFile secondFile thirdFile
起作用,那有点奇怪/非标准,当你想要非标准的东西时,标准库可能无法为你提供支持,所以你必须手动编写。我建议你稍微改变一下你的想法,使之符合人们的期望,从而获得使用库的好处。
然而,深入探讨命令行设计,使用-i in1 -i in2 -i in3
会更好一些,但仍然不符合惯用法,也不方便。通常的规则是输入通过无开关方式提供。编写你的命令行的正确方式是:
java -jar myprog.jar -o outFile firstFile secondFile thirdFile
这是zip
、curl
、ffmpeg
、java
(可执行文件本身)等的工作方式。
args4j和jcommander都可以做到这一点;args4j通过@Arguments List<String> args;
,jcommander通过@Parameter
。
英文:
args4j can't do that, I think. I wouldn't use args4j, it's.. outdated.
Try jcommander. It's the exact same idea, but unlike with args4j, you can stick the jcommander take on @Option
on a List<T>
. See section 2.2 of the docs (click the link, go to 2.2. :P)
HOWEVER: Note that if I take your question literally, what you're asking is highly inadvisable!
In posix-style command line arguments, your string is parsed as:
-i firstFile
- okay, firstFile is the argument for the -i option.secondFile
- okay, this is completely unrelated to the -i, this is a file argument.thirdFile
- same deal.-o outFile
- outFile is the argvalue for the -o switch.
What you presumably want, and how jcommander works, is that you'd have to write:
java -jar myprog.jar -i firstFile -i secondFile -i thirdFile -o outFile
if you really want -i firstFile secondFile thirdFile
to work, that's a bit weird / non-standard, and naturally, when want nonstandard things, standard libraries are unlikely to be capable of delivering for you, so you'd have to write it all by hand. I'd advise you.. change what you want, a little, to fall in line with what people expect, and thus, gain the benefit of using a library to get it for you.
However, going further down this rabbithole of command line design, going with -i in1 -i in2 -i in3
is better, but still non idiomatic and unwieldy. The usual rule is that inputs are fielded via no-switch. The proper way to write your command line would be:
java -jar myprog.jar -o outFile firstFile secondFile thirdFile
This is how zip
, curl
, ffmpeg
, java
(the executable) itself, etc all work.
both args4j and jcommander can do this; args4j via @Arguments List<String> args;
, jcommander via @Parameter
.
答案2
得分: 1
使用 arg4j,如果你想获取与选项“i”相关联的列表,你可以使用StringArrayOptionHandler
。
使用以下命令行:
java -jar myprog.jar -i firstFile secondFile thirdFile -o outputFile
实现这一功能的源代码如下:
class Main {
@Option(name = "-i", handler = StringArrayOptionHandler.class)
private List<String> files;
@Option(name = "-o")
private String outputFile;
public static void main(String[] args) throws Exception {
Main bean = new Main();
CmdLineParser parser = new CmdLineParser(bean);
parser.parseArgument(args);
bean.files.stream().forEach(entry -> System.out.println(entry));
System.out.println(bean.outputFile);
}
}
英文:
With arg4j, If you want to get a list linked to "i" option you can use the StringArrayOptionHandler
.
With this command line:
java -jar myprog.jar -i firstFile secondFile thirdFile -o outputFile
The source code to do that is:
class Main {
@Option(name = "-i", handler = StringArrayOptionHandler.class)
private List<String> files;
@Option(name = "-o")
private String outputFile;
public static void main(String[] args) throws Exception {
Main bean = new Main();
CmdLineParser parser = new CmdLineParser(bean);
parser.parseArgument(args);
bean.files.stream().forEach(entry -> System.out.println(entry));
System.out.println(bean.outputFile);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论