@Test public void testVersionHelp_helpCommand() { CommandSpec helpCommand = CommandSpec.create().helpCommand(true); assertTrue(helpCommand.helpCommand()); CommandSpec parent = CommandSpec.create().addOption(OptionSpec.builder("-x").required(true).build()); parent.addSubcommand("help", helpCommand); CommandLine commandLine = new CommandLine(parent); commandLine.parse("help"); // no missing param exception try { commandLine.parse(); } catch (MissingParameterException ex) { assertEquals("Missing required option '-x=PARAM'", ex.getMessage()); assertEquals(1, ex.getMissing().size()); assertSame(ex.getMissing().get(0).toString(), parent.posixOptionsMap().get('x'), ex.getMissing().get(0)); } }
@Test public void testUsageHelp_abbreviateSynopsisWithoutPositional() throws UnsupportedEncodingException { CommandSpec spec = CommandSpec.create(); spec.usageMessage().abbreviateSynopsis(true).requiredOptionMarker('!').sortOptions(false); spec.addOption(OptionSpec.builder("-x").required(true).description("required").build()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [OPTIONS]%n" + "! -x required%n"); assertEquals(expected, actual); }
@Test public void testUsageHelp_abbreviateSynopsisWithPositional() throws UnsupportedEncodingException { CommandSpec spec = CommandSpec.create(); spec.usageMessage().abbreviateSynopsis(true).requiredOptionMarker('!').sortOptions(false); spec.addOption(OptionSpec.builder("-x").required(true).description("required").build()); spec.addPositional(PositionalParamSpec.builder().arity("1").paramLabel("POSITIONAL").description("positional").build()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [OPTIONS] POSITIONAL...%n" + "! POSITIONAL... positional%n" + "! -x required%n"); assertEquals(expected, actual); }
.type(Double.TYPE) .splitRegex(",,,") .required(true) .defaultValue("DEF") .description("Description") assertEquals(Double.TYPE, builder.type()); assertEquals(",,,", builder.splitRegex()); assertTrue(builder.required()); assertEquals("DEF", builder.defaultValue()); assertArrayEquals(new String[]{"Description"}, builder.description());
.arity("1") .hideParamSyntax(true) .required(true) .splitRegex(";") .description("desc") assertNotEquals(p1, option.arity("2").build()); assertNotEquals(p1, option.arity("1").hideParamSyntax(false).build()); assertNotEquals(p1, option.hideParamSyntax(true).required(false).build()); assertNotEquals(p1, option.required(true).splitRegex(",").build()); assertNotEquals(p1, option.splitRegex(";").description("xyz").build()); assertNotEquals(p1, option.description("desc").descriptionKey("XX").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 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 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 testUsageHelp_abbreviateSynopsisWithPositional() throws UnsupportedEncodingException { CommandSpec spec = CommandSpec.create(); spec.usageMessage().abbreviateSynopsis(true).requiredOptionMarker('!').sortOptions(false); spec.addOption(OptionSpec.builder("-x").required(true).description("required").build()); spec.addPositional(PositionalParamSpec.builder().arity("1").paramLabel("POSITIONAL").description("positional").build()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [OPTIONS] POSITIONAL...%n" + "! POSITIONAL... positional%n" + "! -x required%n"); assertEquals(expected, actual); }
@Test public void testVersionHelp_helpCommand() { CommandSpec helpCommand = CommandSpec.create().helpCommand(true); assertTrue(helpCommand.helpCommand()); CommandSpec parent = CommandSpec.create().addOption(OptionSpec.builder("-x").required(true).build()); parent.addSubcommand("help", helpCommand); CommandLine commandLine = new CommandLine(parent); commandLine.parse("help"); // no missing param exception try { commandLine.parse(); } catch (MissingParameterException ex) { assertEquals("Missing required option '-x=PARAM'", ex.getMessage()); assertEquals(1, ex.getMissing().size()); assertSame(ex.getMissing().get(0).toString(), parent.posixOptionsMap().get('x'), ex.getMissing().get(0)); } }
@Test public void testUsageHelp_abbreviateSynopsisWithoutPositional() throws UnsupportedEncodingException { CommandSpec spec = CommandSpec.create(); spec.usageMessage().abbreviateSynopsis(true).requiredOptionMarker('!').sortOptions(false); spec.addOption(OptionSpec.builder("-x").required(true).description("required").build()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [OPTIONS]%n" + "! -x required%n"); assertEquals(expected, actual); }
.type(Double.TYPE) .splitRegex(",,,") .required(true) .defaultValue("DEF") .description("Description") assertEquals(Double.TYPE, builder.type()); assertEquals(",,,", builder.splitRegex()); assertTrue(builder.required()); assertEquals("DEF", builder.defaultValue()); assertArrayEquals(new String[]{"Description"}, builder.description());
.arity("1") .hideParamSyntax(true) .required(true) .splitRegex(";") .description("desc") assertNotEquals(p1, option.arity("2").build()); assertNotEquals(p1, option.arity("1").hideParamSyntax(false).build()); assertNotEquals(p1, option.hideParamSyntax(true).required(false).build()); assertNotEquals(p1, option.required(true).splitRegex(",").build()); assertNotEquals(p1, option.splitRegex(";").description("xyz").build()); assertNotEquals(p1, option.description("desc").descriptionKey("XX").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 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); }