如何为CLI选项提供类型 - 例如int或Integer?(稍后,如何通过单个函数调用获取解析后的值?)
如何为CLI Option提供默认值?这样CommandLine.getOptionValue()或上面提到的函数调用返回该值,除非在命令行中指定了一个值?
我正在用Java编写命令行应用程序,我选择了Apache Commons CLI来解析输入参数.
假设我有两个必需的选项(即-input和-output).我创建新的Option对象并设置所需的标志.现在一切都很好.但我有第三个,不是必需的选项,即.-救命.使用我提到的设置,当用户想要显示帮助时(使用-help选项),它表示需要"-input和-output".有没有办法实现这个(通过Commons CLI API,而不是简单的if(!hasOption)抛出新的XXXException()).
java command-line-interface apache-commons apache-commons-cli
我创建了一个可执行jar并使用commons-cli为用户提供了在启动客户端时指定命令行参数的能力.一切正常.但是,当我打印jar的用法语句时,我想显示以下内容:
usage: java -jar myprog.jar <options> <file>
--help Display the help message
--debug Enable debugging
....
Run Code Online (Sandbox Code Playgroud)
使用commons-cli可以轻松打印所有选项.然而,"使用"线是头部刮擦.我似乎无法找到从args []获取传递给应用程序的"myprog.jar"名称的方法.
这有什么简单的方法吗?我可以使用一个非常复杂的方法从我的类的类加载器返回跟踪并找出它是否包含在jar中,但这似乎是一个相当难看的答案,应该是一个非常简单的问题.
private String getPath(Class cls) {
String cn = cls.getName();
String rn = cn.replace('.', '/') + ".class";
String path =
getClass().getClassLoader().getResource(rn).getPath();
int ix = path.indexOf("!");
if(ix >= 0) {
return path.substring(0, ix);
} else {
return path;
}
}
Run Code Online (Sandbox Code Playgroud) 我即将迁移某些遗留代码含有较少的过时的警告,第三次三方库.对于Apache commons-cli库(版本:1.3.1),我在官方JavaDoc中检测到GnuParser已弃用,DefaultParser应该使用:
@deprecated自1.3以来,使用
{@link DefaultParser}而不是
但是,以下代码段停止按预期方式工作:
Options options = new Options();
Option optionGSTypes = new Option(
"gst","gs-types", true,
"the supported types, comma-separated: article, category, template, all");
optionGSTypes.setArgs(3);
optionGSTypes.setValueSeparator(',');
options.addOption(optionGSTypes);
// ... other options
// parsed option values are correct, yet this is deprecated
CommandLineParser parser = new GnuParser();
CommandLine commands = parser.parse(options, args);
// ... interpret parsed 'commands' and related actual values via CLI
Run Code Online (Sandbox Code Playgroud)
请注意,setValueSeparator(',')此处用于定义自定义分隔符char , …
java command-line-interface apache-commons apache-commons-cli
我正在使用Apache Commons CLI来处理Java中的命令行参数.
我已经声明了a和b选项,我可以使用它来访问该值CommandLine.getOptionValue().
Usage: myapp [OPTION] [DIRECTORY]
Options:
-a Option A
-b Option B
Run Code Online (Sandbox Code Playgroud)
如何声明和访问DIRECTORY变量?
我正在编写一个Java应用程序,它接受使用Apache Commons CLI和GnuParser处理的命令行参数.由于没有兴趣进入的原因,我希望它默默地忽略未知的命令行选项而不是抛出ParseException,但我没有看到这样做的方法.我看到GnuParser.parse()上有一个stopAtNonOption布尔选项,但我想要的更像ignoreAtNonOption,它会在遇到未知令牌后继续处理选项.
我可以实现我自己的解析器来完成这个,但我很惊讶没有内置这个功能,所以我想我会在走这条路之前检查一下.
我正在谈论的示例代码:
try {
CommandLine commandLine = parser.parse(options, args);
// stopAtNonOption set to true (below) is also not what I want
// CommandLine commandLine = parser.parse(options, args, true);
} catch (ParseException e) {
LOG.error("error parsing arguments", e);
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud) IntelliJ显示在此示例代码中不推荐使用OptionBuilder来自http://commons.apache.org/proper/commons-cli/usage.html.
我应该用什么作为替代品?
import org.apache.commons.cli.*;
Options options = new Options();
options.addOption(OptionBuilder.withLongOpt( "block-size" )
.withDescription( "use SIZE-byte blocks" )
.hasArg()
.withArgName("SIZE")
.create());
Run Code Online (Sandbox Code Playgroud) 我曾经使用过Apache Commons Cli 1.2.我希望解析器忽略参数,如果它们是未知的(不添加到Options-Object).
示例(伪代码):
Options specialOptions;
specialOptions.addOption(null, "help", false, "shows help");
specialOptions.addOption(null, "version", false, "show version");
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args); //no third argument, since i dont want the program to stop parsing.
// run program with args: --help --unknown --version
// program shall parse --help AND --version, but ignore --unknown
Run Code Online (Sandbox Code Playgroud)
我使用了PascalSchäfer的解决方案: Apache Commons CLI选项解析器可以忽略未知的命令行选项吗?
这对我来说在1.2上工作正常,它在1.3.1上也可以正常工作.但它被弃用了.我使用的解析器被替换为DefaultParser.我查看了功能,但没有这样的方法processOptions.
我真的想使用在以后的版本中不会被删除的代码.有谁知道如何解决这个问题?
如何使选项仅接受某些指定值,如下例所示:
$ java -jar Mumu.jar -a foo
OK
$ java -jar Mumu.jar -a bar
OK
$ java -jar Mumu.jar -a foobar
foobar is not a valid value for -a
Run Code Online (Sandbox Code Playgroud) java command-line command-line-interface apache-commons apache-commons-cli
如果我有2个选项定义为所需,例如:
public static void main(String [] args){
Options options= new Options();
Option inputFileOp=Option.builder("i").longOpt("input").hasArg().desc("Input file").argName("file").required().build();
options.addOption(inputFileOp);
Option outputFileOp=Option.builder("o").longOpt("output").hasArg().desc("Output file").argName("file").required().build();
options.addOption(outputFileOp);
Run Code Online (Sandbox Code Playgroud)
和帮助选项
Option helpOp =new Option("h",false,"Show Help");
helpOp.setLongOpt("help");
helpOptions.addOption(helpOp);
Run Code Online (Sandbox Code Playgroud)
和解析器
DefaultParser parser = new DefaultParser();
CommandLine cmd=parser.parse(options,args);
if(cmd.hasOption(helpOp.getOpt())){
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "MyApp.sh", options );
System.exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
当用户输入例如myApp -h ..在解析步骤中引发了一个异常,即缺少必需的选项,而我想打印帮助数据.
如何在保持按要求声明这些选项的同时调用帮助?