.type(List.class) .auxiliaryTypes(File.class) // so, this option is of type List<File> .description("The files to process").build()); spec.addOption(OptionSpec.builder("-n", "--num") .paramLabel("COUNT") .type(int[].class) .splitRegex(",") .description("Comma-separated list of integers").build()); CommandLine commandLine = new CommandLine(spec);
@Test public void testOptionValueReturnsAllValuesConvertedToType() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-V", "--verbose").build()); spec.addOption(OptionSpec.builder("-f", "--file") .paramLabel("FILES") .type(List.class) .auxiliaryTypes(File.class) // List<File> .description("The files to process").build()); CommandLine commandLine = new CommandLine(spec); String[] args = { "--verbose", "-f", "file1", "--file=file2" }; ParseResult pr = commandLine.parseArgs(args); List<File> expected = Arrays.asList(new File("file1"), new File("file2")); assertEquals(expected, pr.matchedOptionValue('f', Collections.<File>emptyList())); assertEquals(expected, pr.matchedOptionValue("--file", Collections.<File>emptyList())); // for examples in Programmatic API wiki page assert expected.equals(pr.matchedOptionValue('f', Collections.<File>emptyList())); assert expected.equals(pr.matchedOptionValue("--file", Collections.<File>emptyList())); }
.paramLabel("COUNT") .type(int.class) .description("number of times to execute").build()); spec.addPositional(PositionalParamSpec.builder() .paramLabel("FILES")
@Test public void testOptionConvertersOverridesRegisteredTypeConverter() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build()); spec.addOption(OptionSpec.builder("-s", "--sql").paramLabel("SQLTYPE").type(int.class).converters( new CommandLineTypeConversionTest.SqlTypeConverter()).description("sql type converter").build()); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "33", "-s", "BLOB"); assertEquals(Integer.valueOf(33), spec.optionsMap().get("-c").getValue()); assertEquals(Integer.valueOf(Types.BLOB), spec.optionsMap().get("-s").getValue()); } @Test
.required(true) .defaultValue("DEF") .description("Description") .paramLabel("param") .arity("1") assertTrue(builder.required()); assertEquals("DEF", builder.defaultValue()); assertArrayEquals(new String[]{"Description"}, builder.description()); assertEquals("param", builder.paramLabel()); assertEquals(Range.valueOf("1"), builder.arity());
@Test public void testRawOptionValueReturnsFirstValue() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-V", "--verbose").build()); spec.addOption(OptionSpec.builder("-f", "--file") .paramLabel("FILES") .type(List.class) .auxiliaryTypes(File.class) // List<File> .description("The files to process").build()); CommandLine commandLine = new CommandLine(spec); String[] args = { "--verbose", "-f", "file1", "--file=file2" }; ParseResult pr = commandLine.parseArgs(args); assertEquals(Arrays.asList(args), pr.originalArgs()); assertEquals("file1", pr.matchedOption('f').stringValues().get(0)); assertEquals("file1", pr.matchedOption("-f").stringValues().get(0)); assertEquals("file1", pr.matchedOption("--file").stringValues().get(0)); List<String> expected = Arrays.asList("file1", "file2"); assertEquals(expected, pr.matchedOption('f').stringValues()); assertEquals(expected, pr.matchedOption("file").stringValues()); // for examples in Programmatic API wiki page assert expected.equals(pr.matchedOption('f').stringValues()); assert expected.equals(pr.matchedOption("file").stringValues()); assertSame(pr.commandSpec().findOption("-V"), pr.tentativeMatch.get(0)); assertSame(pr.commandSpec().findOption("-f"), pr.tentativeMatch.get(1)); assertSame(pr.originalArgs().get(2), pr.tentativeMatch.get(2)); assertSame(pr.commandSpec().findOption("-f"), pr.tentativeMatch.get(3)); assertEquals(4, pr.tentativeMatch.size()); }
.paramLabel("COUNT") .type(int.class) .description("number of times to execute").build()); spec.addPositional(PositionalParamSpec.builder() .paramLabel("FILES")
.required(true) .splitRegex(";") .description("desc") .descriptionKey("key") .auxiliaryTypes(Integer.class, Double.class) 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()); assertNotEquals(p1, option.descriptionKey("key").auxiliaryTypes(Short.class).build()); assertEquals(p1, option.auxiliaryTypes(Integer.class, Double.class).build());
@Test public void testSubcommandNameNotOverwrittenWhenAddedToParent() { CommandSpec toplevel = CommandSpec.create(); toplevel.addOption(OptionSpec.builder("-o").description("o option").build()); CommandSpec sub = CommandSpec.create().name("SOMECOMMAND"); sub.addOption(OptionSpec.builder("-x").description("x option").build()); CommandLine commandLine = new CommandLine(toplevel); CommandLine subCommandLine = new CommandLine(sub); assertEquals("SOMECOMMAND", sub.name()); assertEquals("SOMECOMMAND", subCommandLine.getCommandName()); commandLine.addSubcommand("sub", subCommandLine); assertEquals("SOMECOMMAND", sub.name()); assertEquals("SOMECOMMAND", subCommandLine.getCommandName()); subCommandLine.usage(System.out); String expected = String.format("" + "Usage: <main class> SOMECOMMAND [-x]%n" + " -x x option%n"); assertEquals(expected, systemOutRule.getLog()); }
@Test public void testShowAbbreviatedSynopsisUsageWithCommandOption() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build()); // using abbreviated synopsis spec.usageMessage().abbreviateSynopsis(true); // adding a subcommand should show "COMMAND" option to the help synopsis spec.addSubcommand("subcommand", CommandSpec.create()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Help.Ansi.OFF); String expected = String.format("" + "Usage: <main class> [OPTIONS] [COMMAND]%n" + " -h, --help show help and exit%n" + "Commands:%n" + " subcommand%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 testSubcommandNameIsInitializedWhenAddedToParent() { CommandSpec toplevel = CommandSpec.create(); toplevel.addOption(OptionSpec.builder("-o").description("o option").build()); CommandSpec sub = CommandSpec.create(); sub.addOption(OptionSpec.builder("-x").description("x option").build()); CommandLine commandLine = new CommandLine(toplevel); CommandLine subCommandLine = new CommandLine(sub); assertEquals("<main class>", sub.name()); assertEquals("<main class>", subCommandLine.getCommandName()); commandLine.addSubcommand("sub", subCommandLine); assertEquals("sub", sub.name()); assertEquals("sub", subCommandLine.getCommandName()); subCommandLine.usage(System.out); String expected = String.format("" + "Usage: <main class> sub [-x]%n" + " -x x option%n"); assertEquals(expected, systemOutRule.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); }
@Test public void test381_NPE_whenAddingSubcommand() { CommandSpec toplevel = CommandSpec.create(); toplevel.addOption(OptionSpec.builder("-o").description("o option").build()); CommandSpec sub = CommandSpec.create(); sub.addOption(OptionSpec.builder("-x").description("x option").build()); CommandLine commandLine = new CommandLine(toplevel); commandLine.addSubcommand("sub", sub); // NPE here commandLine.usage(System.out); String expected = String.format("" + "Usage: <main class> [-o] [COMMAND]%n" + " -o o option%n" + "Commands:%n" + " sub%n"); assertEquals(expected, systemOutRule.getLog()); }
@Test public void testShowSynopsisUsageWithCommandOption() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build()); // adding a subcommand should show "COMMAND" option to the help synopsis spec.addSubcommand("subcommand", CommandSpec.create()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Help.Ansi.OFF); String expected = String.format("" + "Usage: <main class> [-h] [COMMAND]%n" + " -h, --help show help and exit%n" + "Commands:%n" + " subcommand%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 testOptionConvertersOverridesRegisteredTypeConverter() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build()); spec.addOption(OptionSpec.builder("-s", "--sql").paramLabel("SQLTYPE").type(int.class).converters( new CommandLineTypeConversionTest.SqlTypeConverter()).description("sql type converter").build()); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "33", "-s", "BLOB"); assertEquals(Integer.valueOf(33), spec.optionsMap().get("-c").getValue()); assertEquals(Integer.valueOf(Types.BLOB), spec.optionsMap().get("-s").getValue()); } @Test
@Test public void testOptionCopyBuilder() { OptionSpec option = OptionSpec.builder("-a", "--aaa").arity("1").type(int.class).description("abc").paramLabel("ABC").build(); OptionSpec copy = option.toBuilder().build(); assertEquals(option, copy); assertNotSame(option, copy); }
@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 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