gpt4 book ai didi

java - 如何使用 apache commons cli 指定多个选项?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:23:40 25 4
gpt4 key购买 nike

我想要这样的东西:

java programName  -jobs1 -C 10 -W 20
java programName -job2
java programName -job3

内容:

Option o1 = new Option("job2", "some desc");
Option o2 = new Option("job3" , "(some desc")

Option o3 = OptionBuilder.hasArgs(2).withArgName( "W" ).withArgName("C").withDescription( "Some desc" ).create("job1")
Option o4 = new Option("help");

Options os = new Options().addOption(o1).addOption(o2).addOption(o3).addOption(o4).

HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "ProgramName", options );

...输出是:

Usage ProgramName
-job1 <c> Some Desc
-job2 Some desc
-job3 Some desc
-help Print this message

我期待 -job1它应该打印 -job1 -C <> -W <>

我错过了什么吗?它不适用于多个参数。顺便说一句,我用了commons-cli 1.2 .

最佳答案

你不能有上下文相关的参数。您可以拥有参数:job1、job2、job3、C & W,但您不能(通过库)说 C & W 仅对 job1 有效。

如果job1/2/3互斥,创建一个OptionGroup。然后在代码中,确保只为 job1 提供 C & W。

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.HelpFormatter;

public class StackOverflowExample
{
public static final String JOB1 = "job1";
public static final Option job1 =
OptionBuilder.hasArg(false)
.isRequired(false)
.withDescription("description of job1")
.create(JOB1);

public static final String JOB2 = "job2";
public static final Option job2 =
OptionBuilder.hasArg(false)
.isRequired(false)
.withDescription("description of job2")
.create(JOB2);

public static final String JOB3 = "job3";
public static final Option job3 =
OptionBuilder.hasArg(false)
.isRequired(false)
.withDescription("description of job3")
.create(JOB3);

public static final String MY_C = "C";
public static final Option my_c =
OptionBuilder.hasArg(true)
.withArgName("count")
.isRequired(false)
.withDescription("description of C")
.create(MY_C);

public static final String MY_W = "W";
public static final Option my_w =
OptionBuilder.hasArg(true)
.withArgName("width")
.isRequired(false)
.withDescription("description of W")
.create(MY_W);


public static void main(String[] args)
{
Options options = new Options();

OptionGroup optgrp = new OptionGroup();
optgrp.addOption(job1);
optgrp.addOption(job2);
optgrp.addOption(job3);

options.addOptionGroup(optgrp);
options.addOption(my_c);
options.addOption(my_w);

try {
CommandLineParser parser = new GnuParser();
CommandLine cmdline = parser.parse(options, args);

if (((cmdline.hasOption(MY_C)) || (cmdline.hasOption(MY_W))) &&
(! cmdline.hasOption(JOB1))) {
HelpFormatter help = new HelpFormatter();
help.printHelp("cmdname", options);
System.exit(-1);
}

System.out.println("OK");
}
catch (Exception ex) {
ex.printStackTrace();
System.exit(-1);
}
}
}

产生以下输出:

<~/sandbox/CoreUtils/scratch> $ javac -d . -cp ~/sandbox/CoreUtils/lib/commons-cli-1.2.jar:. StackOverflowExample.java
<~/sandbox/CoreUtils/scratch> $ java -cp ~/sandbox/CoreUtils/lib/commons-cli-1.2.jar:. StackOverflowExample -C foo -job1
OK

<~/sandbox/CoreUtils/scratch> $ java -cp ~/sandbox/CoreUtils/lib/commons-cli-1.2.jar:. StackOverflowExample -C foo -job2
usage: cmdname
-C <count> description of C
-job1 description of job1
-job2 description of job2
-job3 description of job3
-W <width> description of W

关于java - 如何使用 apache commons cli 指定多个选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9304431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com