@Test public void testModelParse() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-V", "--version").versionHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build()); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "33"); assertEquals(Integer.valueOf(33), spec.optionsMap().get("-c").getValue()); } // TODO parse method should return an object offering only the options/positionals that were matched
@Test public void testMultipleVersionHelpOptions() { setTraceLevel("WARN"); CommandSpec cmd = CommandSpec.create() .add(OptionSpec.builder("-x").type(boolean.class).versionHelp(true).build()) .add(OptionSpec.builder("-V").type(boolean.class).versionHelp(true).build()); assertEquals("", systemErrRule.getLog()); systemErrRule.clearLog(); new CommandLine(cmd); assertEquals("", systemOutRule.getLog()); assertEquals(String.format("[picocli WARN] Multiple options [-x, -V] are marked as 'versionHelp=true'. Usually a command has only one --version option that triggers display of the version information. Alternatively, consider using @Command(mixinStandardHelpOptions = true) on your command instead.%n"), systemErrRule.getLog()); }
@Test public void testModelUsageHelp() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-V", "--version").versionHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build()); spec.addOption(OptionSpec.builder("-f", "--fix").paramLabel("FIXED(BOOLEAN)").arity("1").hideParamSyntax(true).required(true).description("run with fixed option").build()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [-hV] [-c=COUNT] -f=FIXED(BOOLEAN)%n" + " -c, --count=COUNT number of times to execute%n" + " -f, --fix=FIXED(BOOLEAN) run with fixed option%n" + " -h, --help show help and exit%n" + " -V, --version show help and exit%n"); assertEquals(expected, actual); }
.arity("1") .help(true) .versionHelp(true) .usageHelp(true) .order(123) assertEquals(Range.valueOf("1"), builder.arity()); assertTrue(builder.help()); assertTrue(builder.versionHelp()); assertTrue(builder.usageHelp()); assertEquals(123, builder.order());
.help(true) .usageHelp(true) .versionHelp(true) .order(123); assertNotEquals(p1, option.usageHelp(true).versionHelp(false).build()); assertNotEquals(p1, option.versionHelp(true).order(999).build()); assertNotEquals(p1, option.order(123).names("-a", "-b", "-c").build()); assertEquals(p1, option.names("-x").build());
static OptionSpec extractOptionSpec(TypedMember member, IFactory factory) { Option option = member.getAnnotation(Option.class); OptionSpec.Builder builder = OptionSpec.builder(option.names()); initCommon(builder, member); builder.order(option.order()); builder.help(option.help()); builder.usageHelp(option.usageHelp()); builder.versionHelp(option.versionHelp()); builder.showDefaultValue(option.showDefaultValue()); if (!NoCompletionCandidates.class.equals(option.completionCandidates())) { builder.completionCandidates(DefaultFactory.createCompletionCandidates(factory, option.completionCandidates())); } builder.arity(Range.optionArity(member)); builder.required(option.required()); builder.interactive(option.interactive()); Class<?>[] elementTypes = inferTypes(member.getType(), option.type(), member.getGenericType()); builder.auxiliaryTypes(elementTypes); builder.paramLabel(inferLabel(option.paramLabel(), member.name(), member.getType(), elementTypes)); builder.hideParamSyntax(option.hideParamSyntax()); builder.description(option.description()); builder.descriptionKey(option.descriptionKey()); builder.splitRegex(option.split()); builder.hidden(option.hidden()); builder.defaultValue(option.defaultValue()); builder.converters(DefaultFactory.createConverter(factory, option.converter())); return builder.build(); } static PositionalParamSpec extractPositionalParamSpec(TypedMember member, IFactory factory) {
@Test public void testModelUsageHelpWithCustomSeparator() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-V", "--version").versionHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build()); spec.addOption(OptionSpec.builder("-f", "--fix").paramLabel("FIXED(=BOOLEAN)").arity("1").hideParamSyntax(true).required(true).description("run with fixed option").build()); CommandLine commandLine = new CommandLine(spec).setSeparator(" "); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [-hV] [-c COUNT] -f FIXED(=BOOLEAN)%n" + " -c, --count COUNT number of times to execute%n" + " -f, --fix FIXED(=BOOLEAN)%n" + " run with fixed option%n" + " -h, --help show help and exit%n" + " -V, --version show help and exit%n"); assertEquals(expected, actual); }
@Test public void testModelUsageHelp() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-V", "--version").versionHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build()); spec.addOption(OptionSpec.builder("-f", "--fix").paramLabel("FIXED(BOOLEAN)").arity("1").hideParamSyntax(true).required(true).description("run with fixed option").build()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [-hV] [-c=COUNT] -f=FIXED(BOOLEAN)%n" + " -c, --count=COUNT number of times to execute%n" + " -f, --fix=FIXED(BOOLEAN) run with fixed option%n" + " -h, --help show help and exit%n" + " -V, --version show help and exit%n"); assertEquals(expected, actual); }
@Test public void testBooleanObjectVersionHelpOptions() { CommandSpec cmd = CommandSpec.create().add(OptionSpec.builder("-x").type(Boolean.class).versionHelp(true).build()); assertTrue(new CommandLine(cmd).parseArgs("-x").isVersionHelpRequested()); }
@Test public void testModelParse() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-V", "--version").versionHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build()); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "33"); assertEquals(Integer.valueOf(33), spec.optionsMap().get("-c").getValue()); } // TODO parse method should return an object offering only the options/positionals that were matched
@Test public void testMultipleVersionHelpOptions() { setTraceLevel("WARN"); CommandSpec cmd = CommandSpec.create() .add(OptionSpec.builder("-x").type(boolean.class).versionHelp(true).build()) .add(OptionSpec.builder("-V").type(boolean.class).versionHelp(true).build()); assertEquals("", systemErrRule.getLog()); systemErrRule.clearLog(); new CommandLine(cmd); assertEquals("", systemOutRule.getLog()); assertEquals(String.format("[picocli WARN] Multiple options [-x, -V] are marked as 'versionHelp=true'. Usually a command has only one --version option that triggers display of the version information. Alternatively, consider using @Command(mixinStandardHelpOptions = true) on your command instead.%n"), systemErrRule.getLog()); }
@Test public void testNonBooleanVersionHelpOptions() { CommandSpec cmd = CommandSpec.create().add(OptionSpec.builder("-x").type(int.class).versionHelp(true).build()); try { new CommandLine(cmd); } catch (InitializationException ex) { assertEquals("Non-boolean options like [-x] should not be marked as 'versionHelp=true'. Usually a command has one --version boolean flag that triggers display of the version information. Alternatively, consider using @Command(mixinStandardHelpOptions = true) on your command instead.", ex.getMessage()); } }
.arity("1") .help(true) .versionHelp(true) .usageHelp(true) .order(123) assertEquals(Range.valueOf("1"), builder.arity()); assertTrue(builder.help()); assertTrue(builder.versionHelp()); assertTrue(builder.usageHelp()); assertEquals(123, builder.order());
.help(true) .usageHelp(true) .versionHelp(true) .order(123); assertNotEquals(p1, option.usageHelp(true).versionHelp(false).build()); assertNotEquals(p1, option.versionHelp(true).order(999).build()); assertNotEquals(p1, option.order(123).names("-a", "-b", "-c").build()); assertEquals(p1, option.names("-x").build());
@Test public void testModelUsageHelpWithCustomSeparator() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-V", "--version").versionHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build()); spec.addOption(OptionSpec.builder("-f", "--fix").paramLabel("FIXED(=BOOLEAN)").arity("1").hideParamSyntax(true).required(true).description("run with fixed option").build()); CommandLine commandLine = new CommandLine(spec).setSeparator(" "); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [-hV] [-c COUNT] -f FIXED(=BOOLEAN)%n" + " -c, --count COUNT number of times to execute%n" + " -f, --fix FIXED(=BOOLEAN)%n" + " run with fixed option%n" + " -h, --help show help and exit%n" + " -V, --version show help and exit%n"); assertEquals(expected, actual); }
@Test public void testBooleanObjectVersionHelpOptions() { CommandSpec cmd = CommandSpec.create().add(OptionSpec.builder("-x").type(Boolean.class).versionHelp(true).build()); assertTrue(new CommandLine(cmd).parseArgs("-x").isVersionHelpRequested()); }
@Test public void testNonBooleanVersionHelpOptions() { CommandSpec cmd = CommandSpec.create().add(OptionSpec.builder("-x").type(int.class).versionHelp(true).build()); try { new CommandLine(cmd); } catch (InitializationException ex) { assertEquals("Non-boolean options like [-x] should not be marked as 'versionHelp=true'. Usually a command has one --version boolean flag that triggers display of the version information. Alternatively, consider using @Command(mixinStandardHelpOptions = true) on your command instead.", ex.getMessage()); } }