Java Code Examples for picocli.CommandLine.Model.CommandSpec#addPositional()

The following examples show how to use picocli.CommandLine.Model.CommandSpec#addPositional() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommandSpecFactory.java    From milkman with MIT License 6 votes vote down vote up
private CommandSpec addPositionalParameter(CommandSpec cmdSpec, Parameter parameter, int idx) {
	return cmdSpec.addPositional(PositionalParamSpec.builder()
			.paramLabel(parameter.getName())
			.arity("1")
			.index(""+idx)
			.required(parameter.isRequired())
			.setter(new ISetter() {

				@Override
				public <T> T set(T value) throws Exception {
					parameter.setValue((String)value);
					return null;
				}
				
			})
			.description(parameter.getDescription())
			.completionCandidates(parameter.getCompletion())
			.build());
}
 
Example 2
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValuePositionalParamArityAloneIsInsufficient() {
    CommandSpec spec = CommandSpec.create();
    PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(int.class).build();
    assertFalse(positional.isMultiValue());

    spec.addPositional(positional);
    CommandLine commandLine = new CommandLine(spec);
    try {
        commandLine.parseArgs("1", "2", "3");
        fail("Expected exception");
    } catch (UnmatchedArgumentException ex) {
        assertEquals("Unmatched arguments from index 1: '2', '3'", ex.getMessage());
    }
}
 
Example 3
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValuePositionalParamWithArray() {
    CommandSpec spec = CommandSpec.create();
    PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(int[].class).build();
    assertTrue(positional.isMultiValue());

    spec.addPositional(positional);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("1", "2", "3");
    assertArrayEquals(new int[] {1, 2, 3}, (int[]) spec.positionalParameters().get(0).getValue());
}
 
Example 4
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValuePositionalParamWithListAndAuxTypes() {
    CommandSpec spec = CommandSpec.create();
    PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(List.class).auxiliaryTypes(Integer.class).build();
    assertTrue(positional.isMultiValue());

    spec.addPositional(positional);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("1", "2", "3");
    assertEquals(Arrays.asList(1, 2, 3), spec.positionalParameters().get(0).getValue());
}
 
Example 5
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValuePositionalParamWithListWithoutAuxTypes() {
    CommandSpec spec = CommandSpec.create();
    PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(List.class).build();
    assertTrue(positional.isMultiValue());

    spec.addPositional(positional);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("1", "2", "3");
    assertEquals(Arrays.asList("1", "2", "3"), spec.positionalParameters().get(0).getValue());
}
 
Example 6
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValuePositionalParamWithMapAndAuxTypes() {
    CommandSpec spec = CommandSpec.create();
    PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(Map.class).auxiliaryTypes(Integer.class, Double.class).build();
    assertTrue(positional.isMultiValue());

    spec.addPositional(positional);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("1=1.0", "2=2.0", "3=3.0");
    Map<Integer, Double> expected = new LinkedHashMap<Integer, Double>();
    expected.put(1, 1.0);
    expected.put(2, 2.0);
    expected.put(3, 3.0);
    assertEquals(expected, spec.positionalParameters().get(0).getValue());
}
 
Example 7
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValuePositionalParamWithMapWithoutAuxTypes() {
    CommandSpec spec = CommandSpec.create();
    PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(Map.class).build();
    assertTrue(positional.isMultiValue());

    spec.addPositional(positional);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("1=1.0", "2=2.0", "3=3.0");
    Map<String, String> expected = new LinkedHashMap<String, String>();
    expected.put("1", "1.0");
    expected.put("2", "2.0");
    expected.put("3", "3.0");
    assertEquals(expected, spec.positionalParameters().get(0).getValue());
}
 
Example 8
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testPositionalConvertersOverridesRegisteredTypeConverter() {
    CommandSpec spec = CommandSpec.create();
    spec.addPositional(PositionalParamSpec.builder().paramLabel("COUNT").index("0").type(int.class).description("number of times to execute").build());
    spec.addPositional(PositionalParamSpec.builder().paramLabel("SQLTYPE").index("1").type(int.class).converters(
            new TypeConversionTest.SqlTypeConverter()).description("sql type converter").build());
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("33", "BLOB");
    assertEquals(Integer.valueOf(33), spec.positionalParameters().get(0).getValue());
    assertEquals(Integer.valueOf(Types.BLOB), spec.positionalParameters().get(1).getValue());
}
 
Example 9
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsageMessageFromProgrammaticCommandSpec() {
    CommandSpec spec = CommandSpec.create();
    spec.addOption(OptionSpec.builder("-x").splitRegex("").build());
    spec.addPositional(PositionalParamSpec.builder().splitRegex("").build());
    spec.mixinStandardHelpOptions(true);
    String actual = new CommandLine(spec).getUsageMessage(Ansi.OFF);
    String expected = String.format("" +
            "Usage: <main class> [-hVx] PARAM%n" +
            "      PARAM%n" +
            "  -h, --help      Show this help message and exit.%n" +
            "  -V, --version   Print version information and exit.%n" +
            "  -x%n");
    assertEquals(expected, actual);
}
 
Example 10
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsageMessageFromProgrammaticCommandSpecWithSplitRegexSynopsisLabel() {
    CommandSpec spec = CommandSpec.create();
    spec.addOption(OptionSpec.builder("-x").type(String[].class).splitRegex("").splitRegexSynopsisLabel(";").build());
    spec.addPositional(PositionalParamSpec.builder().type(String[].class).splitRegex("").splitRegexSynopsisLabel(";").build());
    spec.mixinStandardHelpOptions(true);
    String actual = new CommandLine(spec).getUsageMessage(Ansi.OFF);
    String expected = String.format("" +
            "Usage: <main class> [-hV] [-x=PARAM]... PARAM...%n" +
            "      PARAM...%n" +
            "  -h, --help      Show this help message and exit.%n" +
            "  -V, --version   Print version information and exit.%n" +
            "  -x=PARAM%n");
    assertEquals(expected, actual);
}
 
Example 11
Source File: ModelUsageMessageSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsageHelpPositional_empty() {
    CommandSpec spec = CommandSpec.create();
    spec.addPositional(PositionalParamSpec.builder().build());
    CommandLine commandLine = new CommandLine(spec);
    String actual = usageString(commandLine, CommandLine.Help.Ansi.OFF);
    String expected = String.format("" +
            "Usage: <main class> PARAM%n" +
            "      PARAM%n");
    assertEquals(expected, actual);
}
 
Example 12
Source File: ModelUsageMessageSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsageHelpPositional_withDescription() {
    CommandSpec spec = CommandSpec.create();
    spec.addPositional(PositionalParamSpec.builder().description("positional param").build());
    CommandLine commandLine = new CommandLine(spec);
    String actual = usageString(commandLine, CommandLine.Help.Ansi.OFF);
    String expected = String.format("" +
            "Usage: <main class> PARAM%n" +
            "      PARAM   positional param%n");
    assertEquals(expected, actual);
}
 
Example 13
Source File: ModelUsageMessageSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsageHelp_CustomizedUsageMessage() {
    CommandSpec spec = CommandSpec.create().addMixin("auto", CommandSpec.forAnnotatedObject(new CommandLine.AutoHelpMixin()));
    spec.name("the awesome util");
    spec.usageMessage()
            .descriptionHeading("Description heading%n")
            .description("description line 1", "description line 2")
            .footerHeading("Footer heading%n")
            .footer("footer line 1", "footer line 2")
            .headerHeading("Header heading%n")
            .header("header line 1", "header line 2")
            .optionListHeading("Options%n")
            .parameterListHeading("Positional Parameters%n");
    spec.addPositional(PositionalParamSpec.builder().description("positional param").build());
    CommandLine commandLine = new CommandLine(spec);
    String actual = usageString(commandLine, CommandLine.Help.Ansi.OFF);
    String expected = String.format("" +
            "Header heading%n" +
            "header line 1%n" +
            "header line 2%n" +
            "Usage: the awesome util [-hV] PARAM%n" +
            "Description heading%n" +
            "description line 1%n" +
            "description line 2%n" +
            "Positional Parameters%n" +
            "      PARAM       positional param%n" +
            "Options%n" +
            "  -h, --help      Show this help message and exit.%n" +
            "  -V, --version   Print version information and exit.%n" +
            "Footer heading%n" +
            "footer line 1%n" +
            "footer line 2%n");
    assertEquals(expected, actual);
}
 
Example 14
Source File: ModelUsageMessageSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@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, CommandLine.Help.Ansi.OFF);
    String expected = String.format("" +
            "Usage: <main class> [OPTIONS] POSITIONAL%n" +
            "!     POSITIONAL   positional%n" +
            "! -x               required%n");
    assertEquals(expected, actual);
}
 
Example 15
Source File: BasicResultProcessing.java    From picocli with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) {

        CommandSpec spec = CommandSpec.create();
        spec.mixinStandardHelpOptions(true);
        spec.addOption(OptionSpec.builder("-c", "--count")
                .paramLabel("COUNT")
                .type(int.class)
                .description("number of times to execute").build());
        spec.addPositional(PositionalParamSpec.builder()
                .paramLabel("FILES")
                .type(List.class)
                .auxiliaryTypes(File.class)
                .description("The files to process").build());
        CommandLine commandLine = new CommandLine(spec);

        try {
            ParseResult pr = commandLine.parseArgs(args);
            if (CommandLine.printHelpIfRequested(pr)) { return; }

            int count = pr.matchedOptionValue('c', 1);
            List<File> files = pr.matchedPositionalValue(0, Collections.<File>emptyList());
            for (File f : files) {
                for (int i = 0; i < count; i++) {
                    System.out.println(i + " " + f.getName());
                }
            }

        } catch (ParameterException ex) {
            System.err.println(ex.getMessage());
            ex.getCommandLine().usage(System.err);
        }
    }
 
Example 16
Source File: ExecutionStrategyWithExecutionResult.java    From picocli with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) {

        CommandSpec spec = CommandSpec.create();
        spec.mixinStandardHelpOptions(true);
        spec.addOption(OptionSpec.builder("-c", "--count")
                .paramLabel("COUNT")
                .type(int.class)
                .description("number of times to execute").build());
        spec.addPositional(PositionalParamSpec.builder()
                .paramLabel("FILES")
                .type(List.class)
                .auxiliaryTypes(File.class)
                .description("The files to process").build());
        CommandLine commandLine = new CommandLine(spec);

        class Handler implements IExecutionStrategy {
            @Override
            public int execute(ParseResult pr) {
                int count = pr.matchedOptionValue('c', 1);
                List<File> files = pr.matchedPositionalValue(0, Collections.<File>emptyList());
                for (File f : files) {
                    for (int i = 0; i < count; i++) {
                        System.out.println(i + " " + f.getName());
                    }
                }
                pr.commandSpec().commandLine().setExecutionResult(files.size());
                return ExitCode.OK;
            }
        }

        commandLine.setExecutionStrategy(new Handler());
        commandLine.execute(args);
        int processed = commandLine.getExecutionResult();
    }