简体   繁体   English

如何使Java CLI解析库Jopt Simple解析参数而没有任何选择?

[英]How do I make my Java CLI parsing library Jopt Simple parse an argument without any option?

So I'm building a Java CLI application that will have features similair to Windows's dir. 因此,我正在构建一个Java CLI应用程序,它将具有与Windows目录类似的功能。 I'm using the jopt-simple 4.9 library for my CLI parsing needs, and for acquiring options it seems pretty straightforward... 我将jopt-simple 4.9库用于我的CLI解析需求,并且为了获取选项,这似乎非常简单...

public static void main(String[] args) throws Exception {
    OptionParser parser = new OptionParser("a::b?*");

    OptionSet options = parser.parse(args);

    parser.accepts("a", "Display all");
    parser.accepts("b", "Bare output without metadata");
    parser.accepts("?", "Displays this help prompt");

But what if I want to run my app without any args? 但是,如果我想运行没有任何参数的应用程序怎么办? Like I would run dir or ls to display local contents. 就像我运行dir或ls来显示本地内容一样。 And what if I want to run my app without any options? 如果我想运行我的应用程序而没有任何选择怎么办? Like if I'm just telling dir or ls which directory I want it to print out. 就像我只是告诉dir或ls我要打印出哪个目录一样。

OK, so to achieve this is simply used the fact the JOpt also parses non-option arguments: 好的,因此,只需使用JOpt还会解析非选项参数这一事实即可:

OptionParser parser = new OptionParser("a::b?*");
    parser.allowsUnrecognizedOptions();

//  In case the user specifies a path instead of just running the command 
//  locally, create an array out of the parsed directory strings.
String[] dirStringArray = options.nonOptionArguments().toArray(new String[options.nonOptionArguments().size()]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM