本文整理汇总了Java中picocli.CommandLine.Option类的典型用法代码示例。如果您正苦于以下问题:Java Option类的具体用法?Java Option怎么用?Java Option使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Option类属于picocli.CommandLine包,在下文中一共展示了Option类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: testParentInjectedWhenAddedAsSubcommand
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testParentInjectedWhenAddedAsSubcommand() {
class Top1 {
@Option(names = {"-d", "--directory"}, description = "this option applies to all subcommands")
private File baseDirectory;
}
class Sub1 {
@ParentCommand private Top1 parent;
@Parameters private int count;
}
List<CommandLine> result = new CommandLine(new Top1())
.addSubcommand("sub1", new Sub1())
.parse("-d/tmp/blah", "sub1", "3");
Top1 top = result.get(0).getCommand();
assertEquals(new File("/tmp/blah"), top.baseDirectory);
Sub1 sub = result.get(1).getCommand();
assertEquals(3, sub.count);
assertSame(top, sub.parent);
}
开发者ID:remkop,项目名称:picocli,代码行数:20,代码来源:ParentCommandTest.java
示例2: testInitializationExceptionForTypeMismatch
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testInitializationExceptionForTypeMismatch() {
class Top1 {
@Option(names = {"-d", "--directory"}, description = "this option applies to all subcommands")
private File baseDirectory;
}
class Sub1 {
@ParentCommand private String parent;
@Parameters private int count;
}
Top1 top1 = new Top1();
Sub1 sub1 = new Sub1();
try {
new CommandLine(top1).addSubcommand("sub1", sub1);
fail("expected failure");
} catch (InitializationException ex) {
String prefix = "Unable to initialize @ParentCommand field: java.lang.IllegalArgumentException";
if (prefix.equals(ex.getMessage())) { return; }
String expected = prefix + ": Can not set java.lang.String field " + sub1.getClass().getName() + ".parent to " + top1.getClass().getName();
assertEquals(expected, ex.getMessage());
}
}
开发者ID:remkop,项目名称:picocli,代码行数:23,代码来源:ParentCommandTest.java
示例3: testArrayOptionArity2_nConsumesAllArgumentsUpToClusteredOption
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testArrayOptionArity2_nConsumesAllArgumentsUpToClusteredOption() {
class ArrayOptionsArity2_nAndParameters {
@Parameters String[] stringParams;
@Option(names = "-s", arity = "2..*") String[] stringOptions;
@Option(names = "-v") boolean verbose;
@Option(names = "-f") File file;
}
ArrayOptionsArity2_nAndParameters
params = CommandLine.populateCommand(new ArrayOptionsArity2_nAndParameters(), "-s 1.1 2.2 3.3 4.4 -vfFILE 5.5".split(" "));
assertArrayEquals(Arrays.toString(params.stringOptions),
new String[] {"1.1", "2.2", "3.3", "4.4"}, params.stringOptions);
assertTrue(params.verbose);
assertEquals(new File("FILE"), params.file);
assertArrayEquals(new String[] {"5.5"}, params.stringParams);
}
开发者ID:remkop,项目名称:picocli,代码行数:17,代码来源:CommandLineArityTest.java
示例4: testArrayOptionArity2_nConsumesAllArgumentIncludingQuotedSimpleOption
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testArrayOptionArity2_nConsumesAllArgumentIncludingQuotedSimpleOption() {
class ArrayOptionArity2_nAndParameters {
@Parameters String[] stringParams;
@Option(names = "-s", arity = "2..*") String[] stringOptions;
@Option(names = "-v") boolean verbose;
@Option(names = "-f") File file;
}
ArrayOptionArity2_nAndParameters
params = CommandLine.populateCommand(new ArrayOptionArity2_nAndParameters(), "-s 1.1 2.2 3.3 4.4 \"-v\" \"-f\" \"FILE\" 5.5".split(" "));
assertArrayEquals(Arrays.toString(params.stringOptions),
new String[] {"1.1", "2.2", "3.3", "4.4", "-v", "-f", "FILE", "5.5"}, params.stringOptions);
assertFalse("verbose", params.verbose);
assertNull("file", params.file);
assertArrayEquals(null, params.stringParams);
}
开发者ID:remkop,项目名称:picocli,代码行数:17,代码来源:CommandLineArityTest.java
示例5: testArrayOptionArity2_nConsumesAllArgumentIncludingQuotedClusteredOption
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testArrayOptionArity2_nConsumesAllArgumentIncludingQuotedClusteredOption() {
class ArrayOptionArity2_nAndParameters {
@Parameters String[] stringParams;
@Option(names = "-s", arity = "2..*") String[] stringOptions;
@Option(names = "-v") boolean verbose;
@Option(names = "-f") File file;
}
ArrayOptionArity2_nAndParameters
params = CommandLine.populateCommand(new ArrayOptionArity2_nAndParameters(), "-s 1.1 2.2 3.3 4.4 \"-vfFILE\" 5.5".split(" "));
assertArrayEquals(Arrays.toString(params.stringOptions),
new String[] {"1.1", "2.2", "3.3", "4.4", "-vfFILE", "5.5"}, params.stringOptions);
assertFalse("verbose", params.verbose);
assertNull("file", params.file);
assertArrayEquals(null, params.stringParams);
}
开发者ID:remkop,项目名称:picocli,代码行数:17,代码来源:CommandLineArityTest.java
示例6: testArrayOptionArity2_nConsumesAllArgumentsUpToNextSimpleOption
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testArrayOptionArity2_nConsumesAllArgumentsUpToNextSimpleOption() {
class ArrayOptionArity2_nAndParameters {
@Parameters double[] doubleParams;
@Option(names = "-s", arity = "2..*") String[] stringOptions;
@Option(names = "-v") boolean verbose;
@Option(names = "-f") File file;
}
ArrayOptionArity2_nAndParameters
params = CommandLine.populateCommand(new ArrayOptionArity2_nAndParameters(), "-s 1.1 2.2 3.3 4.4 -v -f=FILE 5.5".split(" "));
assertArrayEquals(Arrays.toString(params.stringOptions),
new String[] {"1.1", "2.2", "3.3", "4.4"}, params.stringOptions);
assertTrue(params.verbose);
assertEquals(new File("FILE"), params.file);
assertArrayEquals(new double[] {5.5}, params.doubleParams, 0.000001);
}
开发者ID:remkop,项目名称:picocli,代码行数:17,代码来源:CommandLineArityTest.java
示例7: testArrayOptionArity2_nConsumesAllArgumentsUpToNextOptionWithAttachment
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testArrayOptionArity2_nConsumesAllArgumentsUpToNextOptionWithAttachment() {
class ArrayOptionArity2_nAndParameters {
@Parameters double[] doubleParams;
@Option(names = "-s", arity = "2..*") String[] stringOptions;
@Option(names = "-v") boolean verbose;
@Option(names = "-f") File file;
}
ArrayOptionArity2_nAndParameters
params = CommandLine.populateCommand(new ArrayOptionArity2_nAndParameters(), "-s 1.1 2.2 3.3 4.4 -f=FILE -v 5.5".split(" "));
assertArrayEquals(Arrays.toString(params.stringOptions),
new String[] {"1.1", "2.2", "3.3", "4.4"}, params.stringOptions);
assertTrue(params.verbose);
assertEquals(new File("FILE"), params.file);
assertArrayEquals(new double[] {5.5}, params.doubleParams, 0.000001);
}
开发者ID:remkop,项目名称:picocli,代码行数:17,代码来源:CommandLineArityTest.java
示例8: testNoMissingRequiredParamErrorIfHelpOptionSpecified
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testNoMissingRequiredParamErrorIfHelpOptionSpecified() {
class App {
@Parameters(hidden = true) // "hidden": don't show this parameter in usage help message
List<String> allParameters; // no "index" attribute: captures _all_ arguments (as Strings)
@Parameters(index = "0") InetAddress host;
@Parameters(index = "1") int port;
@Parameters(index = "2..*") File[] files;
@Option(names = "-?", help = true) boolean help;
}
CommandLine.populateCommand(new App(), new String[] {"-?"});
try {
CommandLine.populateCommand(new App(), new String[0]);
fail("Should not accept missing mandatory parameter");
} catch (MissingParameterException ex) {
assertEquals("Missing required parameters: <host>, <port>", ex.getMessage());
}
}
开发者ID:remkop,项目名称:picocli,代码行数:21,代码来源:CommandLineArityTest.java
示例9: testNoMissingRequiredParamErrorWithLabelIfHelpOptionSpecified
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testNoMissingRequiredParamErrorWithLabelIfHelpOptionSpecified() {
class App {
@Parameters(hidden = true) // "hidden": don't show this parameter in usage help message
List<String> allParameters; // no "index" attribute: captures _all_ arguments (as Strings)
@Parameters(index = "0", paramLabel = "HOST") InetAddress host;
@Parameters(index = "1", paramLabel = "PORT") int port;
@Parameters(index = "2..*", paramLabel = "FILES") File[] files;
@Option(names = "-?", help = true) boolean help;
}
CommandLine.populateCommand(new App(), new String[] {"-?"});
try {
CommandLine.populateCommand(new App(), new String[0]);
fail("Should not accept missing mandatory parameter");
} catch (MissingParameterException ex) {
assertEquals("Missing required parameters: HOST, PORT", ex.getMessage());
}
}
开发者ID:remkop,项目名称:picocli,代码行数:21,代码来源:CommandLineArityTest.java
示例10: testArrayOptionWithArity1Consumes1Argument
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testArrayOptionWithArity1Consumes1Argument() {
class Options1ArityAndParameters {
@Parameters double[] doubleParams;
@Option(names = "-doubles", arity = "1") double[] doubleOptions;
}
Options1ArityAndParameters
params = CommandLine.populateCommand(new Options1ArityAndParameters(), "-doubles 1.1 2.2 3.3 4.4".split(" "));
assertArrayEquals(Arrays.toString(params.doubleOptions),
new double[] {1.1}, params.doubleOptions, 0.000001);
assertArrayEquals(new double[]{2.2, 3.3, 4.4}, params.doubleParams, 0.000001);
// repeated occurrence
params = CommandLine.populateCommand(new Options1ArityAndParameters(), "-doubles 1.1 -doubles 2.2 -doubles 3.3 4.4".split(" "));
assertArrayEquals(Arrays.toString(params.doubleOptions),
new double[] {1.1, 2.2, 3.3}, params.doubleOptions, 0.000001);
assertArrayEquals(new double[]{4.4}, params.doubleParams, 0.000001);
}
开发者ID:remkop,项目名称:picocli,代码行数:20,代码来源:CommandLineArityTest.java
示例11: testArrayOptionWithoutArityConsumesOneArgument
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testArrayOptionWithoutArityConsumesOneArgument() { // #192
class OptionsNoArityAndParameters {
@Parameters char[] charParams;
@Option(names = "-chars") char[] charOptions;
}
OptionsNoArityAndParameters
params = CommandLine.populateCommand(new OptionsNoArityAndParameters(), "-chars a b c d".split(" "));
assertArrayEquals(Arrays.toString(params.charOptions),
new char[] {'a', }, params.charOptions);
assertArrayEquals(Arrays.toString(params.charParams), new char[] {'b', 'c', 'd'}, params.charParams);
// repeated occurrence
params = CommandLine.populateCommand(new OptionsNoArityAndParameters(), "-chars a -chars b c d".split(" "));
assertArrayEquals(Arrays.toString(params.charOptions),
new char[] {'a', 'b', }, params.charOptions);
assertArrayEquals(Arrays.toString(params.charParams), new char[] {'c', 'd'}, params.charParams);
try {
CommandLine.populateCommand(new OptionsNoArityAndParameters(), "-chars".split(" "));
fail("expected MissingParameterException");
} catch (MissingParameterException ok) {
assertEquals("Missing required parameter for option '-chars' (<charOptions>)", ok.getMessage());
}
}
开发者ID:remkop,项目名称:picocli,代码行数:26,代码来源:CommandLineArityTest.java
示例12: testMixPositionalParamsWithOptions_ParamsUnboundedArity_isGreedy
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testMixPositionalParamsWithOptions_ParamsUnboundedArity_isGreedy() {
class Arg {
@Parameters(arity = "1..*") List<String> parameters;
@Option(names = "-o") List<String> options;
}
Arg result = CommandLine.populateCommand(new Arg(), "-o", "v1", "p1", "p2", "-o", "v2", "p3", "p4");
assertEquals(Arrays.asList("p1", "p2", "-o", "v2", "p3", "p4"), result.parameters);
assertEquals(Arrays.asList("v1"), result.options);
Arg result2 = CommandLine.populateCommand(new Arg(), "-o", "v1", "p1", "-o", "v2", "p3");
assertEquals(Arrays.asList("p1", "-o", "v2", "p3"), result2.parameters);
assertEquals(Arrays.asList("v1"), result2.options);
try {
CommandLine.populateCommand(new Arg(), "-o", "v1", "-o", "v2");
fail("Expected MissingParameterException");
} catch (MissingParameterException ex) {
assertEquals("Missing required parameters at positions 0..*: <parameters>", ex.getMessage());
}
}
开发者ID:remkop,项目名称:picocli,代码行数:22,代码来源:CommandLineArityTest.java
示例13: testSystemPropertiesOverrideDefaultColorScheme
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testSystemPropertiesOverrideDefaultColorScheme() {
@CommandLine.Command(separator = "=") class App {
@Option(names = {"--verbose", "-v"}) boolean verbose;
@Option(names = {"--count", "-c"}) int count;
@Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested;
@Parameters(paramLabel = "FILE", arity = "1..*") File[] files;
}
Help.Ansi ansi = Help.Ansi.ON;
// default color scheme
assertEquals(ansi.new Text("@|bold <main class>|@ [@|yellow -v|@] [@|yellow -c|@[email protected]|italic <count>|@] @|yellow FILE|@..." + LINESEP),
new Help(new App(), ansi).synopsis(0));
System.setProperty("picocli.color.commands", "blue");
System.setProperty("picocli.color.options", "green");
System.setProperty("picocli.color.parameters", "cyan");
System.setProperty("picocli.color.optionParams", "magenta");
assertEquals(ansi.new Text("@|blue <main class>|@ [@|green -v|@] [@|green -c|@[email protected]|magenta <count>|@] @|cyan FILE|@..." + LINESEP),
new Help(new App(), ansi).synopsis(0));
}
开发者ID:remkop,项目名称:picocli,代码行数:21,代码来源:CommandLineHelpTest.java
示例14: testAnnotatedCustomConverterDoesNotConflictWithExistingType
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testAnnotatedCustomConverterDoesNotConflictWithExistingType() {
class App {
@Parameters(index = "0", converter = SqlTypeConverter.class)
int sqlTypeParam;
@Parameters(index = "1")
int normalIntParam;
@Option(names = "-t", converter = SqlTypeConverter.class)
int sqlTypeOption;
@Option(names = "-x")
int normalIntOption;
}
App app = new App();
CommandLine commandLine = new CommandLine(app);
String[] args = {"-x", "1234", "-t", "BLOB", "CLOB", "5678"};
commandLine.parse(args);
assertEquals(1234, app.normalIntOption);
assertEquals(Types.BLOB, app.sqlTypeOption);
assertEquals(Types.CLOB, app.sqlTypeParam);
assertEquals(5678, app.normalIntParam);
}
开发者ID:remkop,项目名称:picocli,代码行数:26,代码来源:CommandLineTypeConversionTest.java
示例15: testUsageParamLabels
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testUsageParamLabels() throws Exception {
@Command()
class ParamLabels {
@Option(names = "-P", paramLabel = "KEY=VALUE", type = {String.class, String.class},
description = "Project properties (key-value pairs)") Map<String, String> props;
@Option(names = "-f", paramLabel = "FILE", description = "files") File[] f;
@Option(names = "-n", description = "a number option") int number;
@Parameters(index = "0", paramLabel = "NUM", description = "number param") int n;
@Parameters(index = "1", description = "the host parameter") InetAddress host;
}
String result = usageString(new ParamLabels(), Help.Ansi.OFF);
assertEquals(format("" +
"Usage: <main class> [-n=<number>] [-f=FILE]... [-P=KEY=VALUE]... NUM <host>%n" +
" NUM number param%n" +
" <host> the host parameter%n" +
" -f= FILE files%n" +
" -n= <number> a number option%n" +
" -P= KEY=VALUE Project properties (key-value pairs)%n",
""), result);
}
开发者ID:remkop,项目名称:picocli,代码行数:22,代码来源:CommandLineHelpTest.java
示例16: testUsageParamLabelsWithLongMapOptionName
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testUsageParamLabelsWithLongMapOptionName() throws Exception {
@Command()
class ParamLabels {
@Option(names = {"-P", "--properties"},
paramLabel = "KEY=VALUE", type = {String.class, String.class},
description = "Project properties (key-value pairs)") Map<String, String> props;
@Option(names = "-f", paramLabel = "FILE", description = "a file") File f;
@Option(names = "-n", description = "a number option") int number;
@Parameters(index = "0", paramLabel = "NUM", description = "number param") int n;
@Parameters(index = "1", description = "the host parameter") InetAddress host;
}
String result = usageString(new ParamLabels(), Help.Ansi.OFF);
assertEquals(format("" +
"Usage: <main class> [-f=FILE] [-n=<number>] [-P=KEY=VALUE]... NUM <host>%n" +
" NUM number param%n" +
" <host> the host parameter%n" +
" -f= FILE a file%n" +
" -n= <number> a number option%n" +
" -P, --properties=KEY=VALUE Project properties (key-value pairs)%n",
""), result);
}
开发者ID:remkop,项目名称:picocli,代码行数:23,代码来源:CommandLineHelpTest.java
示例17: testUsageVariableArityRequiredShortOptionArray
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testUsageVariableArityRequiredShortOptionArray() throws UnsupportedEncodingException {
// if option is required at least once and can be specified multiple times:
// -f=ARG [-f=ARG]...
class Args {
@Option(names = "-a", required = true, paramLabel = "ARG") // default
String[] a;
@Option(names = "-b", required = true, paramLabel = "ARG", arity = "0..*")
List<String> b;
@Option(names = "-c", required = true, paramLabel = "ARG", arity = "1..*")
String[] c;
@Option(names = "-d", required = true, paramLabel = "ARG", arity = "2..*")
List<String> d;
}
String expected = String.format("" +
"Usage: <main class> -a=ARG [-a=ARG]... -b=[ARG]... [-b=[ARG]...]... -c=ARG...%n" +
" [-c=ARG...]... -d=ARG ARG... [-d=ARG ARG...]...%n" +
" -a= ARG%n" +
" -b= [ARG]...%n" +
" -c= ARG...%n" +
" -d= ARG ARG...%n");
//CommandLine.usage(new Args(), System.out);
assertEquals(expected, usageString(new Args(), Help.Ansi.OFF));
}
开发者ID:remkop,项目名称:picocli,代码行数:25,代码来源:CommandLineHelpTest.java
示例18: testUsageVariableArityShortOptionArray
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testUsageVariableArityShortOptionArray() throws UnsupportedEncodingException {
class Args {
@Option(names = "-a", paramLabel = "ARG") // default
List<String> a;
@Option(names = "-b", paramLabel = "ARG", arity = "0..*")
String[] b;
@Option(names = "-c", paramLabel = "ARG", arity = "1..*")
List<String> c;
@Option(names = "-d", paramLabel = "ARG", arity = "2..*")
String[] d;
}
String expected = String.format("" +
"Usage: <main class> [-a=ARG]... [-b=[ARG]...]... [-c=ARG...]... [-d=ARG%n" +
" ARG...]...%n" +
" -a= ARG%n" +
" -b= [ARG]...%n" +
" -c= ARG...%n" +
" -d= ARG ARG...%n");
//CommandLine.usage(new Args(), System.out);
assertEquals(expected, usageString(new Args(), Help.Ansi.OFF));
}
开发者ID:remkop,项目名称:picocli,代码行数:23,代码来源:CommandLineHelpTest.java
示例19: testUsageRangeArityRequiredShortOptionArray
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testUsageRangeArityRequiredShortOptionArray() throws UnsupportedEncodingException {
// if option is required at least once and can be specified multiple times:
// -f=ARG [-f=ARG]...
class Args {
@Option(names = "-a", required = true, paramLabel = "ARG", arity = "0..1")
List<String> a;
@Option(names = "-b", required = true, paramLabel = "ARG", arity = "1..2")
String[] b;
@Option(names = "-c", required = true, paramLabel = "ARG", arity = "1..3")
String[] c;
@Option(names = "-d", required = true, paramLabel = "ARG", arity = "2..4")
String[] d;
}
String expected = String.format("" +
"Usage: <main class> -a[=ARG] [-a[=ARG]]... -b=ARG [ARG] [-b=ARG [ARG]]...%n" +
" -c=ARG [ARG [ARG]] [-c=ARG [ARG [ARG]]]... -d=ARG ARG [ARG%n" +
" [ARG]] [-d=ARG ARG [ARG [ARG]]]...%n" +
" -a= [ARG]%n" +
" -b= ARG [ARG]%n" +
" -c= ARG [ARG [ARG]]%n" +
" -d= ARG ARG [ARG [ARG]]%n");
//CommandLine.usage(new Args(), System.out);
assertEquals(expected, usageString(new Args(), Help.Ansi.OFF));
}
开发者ID:remkop,项目名称:picocli,代码行数:26,代码来源:CommandLineHelpTest.java
示例20: testUsageRangeArityShortOptionArray
import picocli.CommandLine.Option; //导入依赖的package包/类
@Test
public void testUsageRangeArityShortOptionArray() throws UnsupportedEncodingException {
class Args {
@Option(names = "-a", paramLabel = "ARG", arity = "0..1")
List<String> a;
@Option(names = "-b", paramLabel = "ARG", arity = "1..2")
String[] b;
@Option(names = "-c", paramLabel = "ARG", arity = "1..3")
String[] c;
@Option(names = "-d", paramLabel = "ARG", arity = "2..4")
String[] d;
}
String expected = String.format("" +
"Usage: <main class> [-a[=ARG]]... [-b=ARG [ARG]]... [-c=ARG [ARG [ARG]]]...%n" +
" [-d=ARG ARG [ARG [ARG]]]...%n" +
" -a= [ARG]%n" +
" -b= ARG [ARG]%n" +
" -c= ARG [ARG [ARG]]%n" +
" -d= ARG ARG [ARG [ARG]]%n");
//CommandLine.usage(new Args(), System.out);
assertEquals(expected, usageString(new Args(), Help.Ansi.OFF));
}
开发者ID:remkop,项目名称:picocli,代码行数:23,代码来源:CommandLineHelpTest.java
注:本文中的picocli.CommandLine.Option类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论