picocli.CommandLine.Model.PositionalParamSpec Java Examples

The following examples show how to use picocli.CommandLine.Model.PositionalParamSpec. 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: ModelPositionalParamSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testPositionalInteractiveNotSupportedForMultiValue() {
    PositionalParamSpec.Builder[] options = new PositionalParamSpec.Builder[]{
            PositionalParamSpec.builder().arity("1").interactive(true),
            PositionalParamSpec.builder().arity("2").interactive(true),
            PositionalParamSpec.builder().arity("3").interactive(true),
            PositionalParamSpec.builder().arity("1..2").interactive(true),
            PositionalParamSpec.builder().arity("1..*").interactive(true),
            PositionalParamSpec.builder().arity("0..*").interactive(true),
    };
    for (PositionalParamSpec.Builder opt : options) {
        try {
            opt.build();
            fail("Expected exception");
        } catch (CommandLine.InitializationException ex) {
            assertEquals("Interactive options and positional parameters are only supported for arity=0 and arity=0..1; not for arity=" + opt.arity(), ex.getMessage());
        }
    }
    // no errors
    PositionalParamSpec.builder().arity("0").interactive(true).build();
    PositionalParamSpec.builder().arity("0..1").interactive(true).build();
}
 
Example #2
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseResetsRawAndOriginalStringValues() {
    CommandSpec spec = CommandSpec.create()
            .addOption(OptionSpec.builder("-x").type(String.class).build())
            .addPositional(PositionalParamSpec.builder().build());
    CommandLine cmd = new CommandLine(spec);
    ParseResult parseResult = cmd.parseArgs("-x", "XVAL", "POSITIONAL");
    assertEquals("XVAL", parseResult.matchedOption('x').getValue());
    assertEquals(Arrays.asList("XVAL"), parseResult.matchedOption('x').stringValues());
    assertEquals(Arrays.asList("XVAL"), parseResult.matchedOption('x').originalStringValues());
    assertEquals("POSITIONAL", parseResult.matchedPositional(0).getValue());
    assertEquals(Arrays.asList("POSITIONAL"), parseResult.matchedPositional(0).stringValues());
    assertEquals(Arrays.asList("POSITIONAL"), parseResult.matchedPositional(0).originalStringValues());

    ParseResult parseResult2 = cmd.parseArgs("-x", "222", "$$$$");
    assertEquals("222", parseResult2.matchedOption('x').getValue());
    assertEquals(Arrays.asList("222"), parseResult2.matchedOption('x').stringValues());
    assertEquals(Arrays.asList("222"), parseResult2.matchedOption('x').originalStringValues());
    assertEquals("$$$$", parseResult2.matchedPositional(0).getValue());
    assertEquals(Arrays.asList("$$$$"), parseResult2.matchedPositional(0).stringValues());
    assertEquals(Arrays.asList("$$$$"), parseResult2.matchedPositional(0).originalStringValues());

}
 
Example #3
Source File: RangeTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultRange() {
    class R1 {
        @Parameters int a;
    }
    PositionalParamSpec positional = new CommandLine(new R1()).getCommandSpec().positionalParameters().get(0);
    CommandLine.Range range = positional.index();
    assertEquals(0, range.min());
    assertEquals(0, range.max());
    assertEquals(false, range.isUnspecified());
    assertEquals(true, range.isRelative());
    assertEquals(false, range.isVariable());
    assertEquals(false, range.isUnresolved());
    assertEquals("0", range.toString());
    assertEquals("0+ (0)", range.internalToString());
}
 
Example #4
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testPositionalParamSpec_defaultValue_overwritesInitialValue() {
    class Params {
        @Parameters int num = 12345;
    }
    CommandLine cmd = new CommandLine(new Params());
    PositionalParamSpec x = cmd.getCommandSpec().positionalParameters().get(0).toBuilder().defaultValue("54321").build();

    cmd = new CommandLine(CommandSpec.create().add(x));
    ParseResult parseResult = cmd.parseArgs();

    // default not in the parse result
    assertFalse(parseResult.hasMatchedPositional(0));
    assertEquals(Integer.valueOf(-1), parseResult.matchedPositionalValue(0, -1));

    // but positional spec does have the default value
    assertEquals(Integer.valueOf(54321), parseResult.commandSpec().positionalParameters().get(0).getValue());

}
 
Example #5
Source File: AutoCompleteTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddCandidatesForArgsFollowingObject() throws Exception {
    Method m = AutoComplete.class.getDeclaredMethod("addCandidatesForArgsFollowing", Object.class, List.class);
    m.setAccessible(true);
    List<String> candidates = new ArrayList<String>();
    m.invoke(null, null, candidates);
    assertTrue("null Object adds no candidates", candidates.isEmpty());

    m.invoke(null, new Object(), candidates);
    assertTrue("non-PicocliModelObject Object adds no candidates", candidates.isEmpty());

    List<String> completions = Arrays.asList("x", "y", "z");
    PositionalParamSpec positional = PositionalParamSpec.builder().completionCandidates(completions).build();
    m.invoke(null, positional, candidates);
    assertEquals("PositionalParamSpec adds completion candidates", completions, candidates);
}
 
Example #6
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testPositionalClearCustomSetterBeforeParse() {
    CommandSpec cmd = CommandSpec.create();
    final List<Object> values = new ArrayList<Object>();
    ISetter setter = new ISetter() {
        public <T> T set(T value) {
            values.add(value);
            return null;
        }
    };
    cmd.add(PositionalParamSpec.builder().type(String.class).setter(setter).build());

    CommandLine cl = new CommandLine(cmd);
    assertTrue(values.isEmpty());
    cl.parseArgs("1");
    assertEquals(2, values.size());
    assertEquals(null, values.get(0));
    assertEquals("1", values.get(1));

    values.clear();
    cl.parseArgs("2");
    assertEquals(2, values.size());
    assertEquals(null, values.get(0));
    assertEquals("2", values.get(1));
}
 
Example #7
Source File: ModelArgSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgSpecBuilderCompletionCandidates() {
    List<String> candidates = Arrays.asList("a", "b");
    PositionalParamSpec.Builder positional = PositionalParamSpec.builder()
            .completionCandidates(candidates);

    assertEquals(candidates, positional.completionCandidates());
}
 
Example #8
Source File: ModelArgSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
@Deprecated public void testArgSpecRenderedDescriptionInitial() {
    PositionalParamSpec positional = PositionalParamSpec.builder().build();
    assertArrayEquals(new String[0], positional.renderedDescription());

    PositionalParamSpec positional2 = PositionalParamSpec.builder().description(new String[0]).build();
    assertArrayEquals(new String[0], positional2.renderedDescription());
}
 
Example #9
Source File: AutoComplete.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
private static CommandSpec findCommandFor(PositionalParamSpec positional, CommandSpec commandSpec) {
    for (PositionalParamSpec defined : commandSpec.positionalParameters()) {
        if (defined == positional) { return commandSpec; }
    }
    for (CommandLine sub : commandSpec.subcommands().values()) {
        CommandSpec result = findCommandFor(positional, sub.getCommandSpec());
        if (result != null) { return result; }
    }
    return null;
}
 
Example #10
Source File: InheritedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testProgrammaticAddPositionalParamAfterSub() {
    PositionalParamSpec positional = PositionalParamSpec.builder().scopeType(INHERIT).build();
    CommandSpec spec = CommandSpec.create();
    CommandSpec sub = CommandSpec.create();
    spec.addSubcommand("sub", sub);
    spec.add(positional);
    assertFalse(positional.inherited());

    assertFalse(spec.positionalParameters().isEmpty());
    assertFalse(sub.positionalParameters().isEmpty());

    assertFalse(spec.positionalParameters().get(0).inherited());
    assertTrue(sub.positionalParameters().get(0).inherited());
}
 
Example #11
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 #12
Source File: ModelArgSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgSpecEquals() {
    PositionalParamSpec.Builder positional = PositionalParamSpec.builder()
            .arity("1")
            .hideParamSyntax(true)
            .required(true)
            .splitRegex(";")
            .description("desc")
            .descriptionKey("key")
            .type(Map.class)
            .auxiliaryTypes(Integer.class, Double.class)
            .inherited(true)
            .scopeType(CommandLine.ScopeType.INHERIT);

    PositionalParamSpec p1 = positional.build();
    assertEquals(p1, p1);
    assertEquals(p1, positional.build());
    assertNotEquals(p1, positional.arity("2").build());
    assertNotEquals(p1, positional.arity("1").hideParamSyntax(false).build());
    assertNotEquals(p1, positional.hideParamSyntax(true).required(false).build());
    assertNotEquals(p1, positional.required(true).splitRegex(",").build());
    assertNotEquals(p1, positional.splitRegex(";").description("xyz").build());
    assertNotEquals(p1, positional.description("desc").descriptionKey("XX").build());
    assertNotEquals(p1, positional.descriptionKey("key").type(List.class).build());
    assertNotEquals(p1, positional.type(Map.class).auxiliaryTypes(Short.class).build());
    assertEquals(p1, positional.auxiliaryTypes(Integer.class, Double.class).build());
    assertNotEquals(p1, positional.inherited(false).build());
    assertEquals(p1, positional.inherited(true).build());
    assertNotEquals(p1, positional.scopeType(CommandLine.ScopeType.LOCAL).build());
    assertEquals(p1, positional.scopeType(CommandLine.ScopeType.INHERIT).build());
}
 
Example #13
Source File: ModelArgSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgSpecSetter2WrapNonPicocliException() {
    final Exception expected = new Exception("boom");
    ISetter setter = new ISetter() {
        public <T> T set(T value) throws Exception { throw expected; }
    };
    PositionalParamSpec positional = PositionalParamSpec.builder().setter(setter).build();
    try {
        positional.setValue("abc");
    } catch (CommandLine.PicocliException ex) {
        assertSame(expected, ex.getCause());
    }
}
 
Example #14
Source File: ModelArgSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgSpecSetterWrapNonPicocliException() {
    final Exception expected = new Exception("boom");
    ISetter setter = new ISetter() {
        public <T> T set(T value) throws Exception { throw expected; }
    };
    PositionalParamSpec positional = PositionalParamSpec.builder().setter(setter).build();
    try {
        positional.setValue("abc");
    } catch (CommandLine.PicocliException ex) {
        assertSame(expected, ex.getCause());
    }
}
 
Example #15
Source File: ModelArgSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgSpecGetter() {
    IGetter getter = new IGetter() {
        public <T> T get() { return null; }
    };
    PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build();
    assertSame(getter, positional.getter());
}
 
Example #16
Source File: ModelArgSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgSpecGetterRethrowsPicocliException() {
    final CommandLine.PicocliException expected = new CommandLine.PicocliException("boom");
    IGetter getter = new IGetter() {
        public <T> T get() { throw expected; }
    };
    PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build();
    try {
        positional.getValue();
    } catch (CommandLine.PicocliException ex) {
        assertSame(expected, ex);
    }
}
 
Example #17
Source File: ModelArgSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgSpecGetterWrapNonPicocliException() {
    final Exception expected = new Exception("boom");
    IGetter getter = new IGetter() {
        public <T> T get() throws Exception { throw expected; }
    };
    PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build();
    try {
        positional.getValue();
    } catch (CommandLine.PicocliException ex) {
        assertSame(expected, ex.getCause());
    }
}
 
Example #18
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testPositionalParamSpec_DefaultValue_map_replacedByCommandLineValue() {
    CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec
            .builder().defaultValue("1=A,2=B,3=C").splitRegex(",").type(Map.class).auxiliaryTypes(Integer.class, String.class).build());

    ParseResult parseResult = new CommandLine(cmd).parseArgs("4=X,5=Y,6=Z");
    Map<Integer, String> expected = new HashMap<Integer, String>();
    expected.put(4, "X");
    expected.put(5, "Y");
    expected.put(6, "Z");
    assertEquals(expected, parseResult.matchedPositionalValue(0, Collections.emptyMap()));
}
 
Example #19
Source File: ModelArgSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Test
public void testArgSpecSetValueWithCommandLineCallsSetter() {
    final Object[] newVal = new Object[1];
    ISetter setter = new ISetter() {
        public <T> T set(T value) { newVal[0] = value; return null; }
    };
    PositionalParamSpec positional = PositionalParamSpec.builder().setter(setter).build();
    positional.setValue("abc", new CommandLine(CommandSpec.create()));
    assertEquals("abc", newVal[0]);
}
 
Example #20
Source File: ArgSplitTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgSpecSplitUnbalancedQuotedValueDebug() {
    PositionalParamSpec positional = PositionalParamSpec.builder().type(String[].class).splitRegex(";").build();

    System.setProperty("picocli.trace", "DEBUG");
    String value = "\"abc\\\";def";
    String[] values = positional.splitValue(value, new CommandLine.Model.ParserSpec().splitQuotedStrings(false), CommandLine.Range.valueOf("1"), 1);
    System.clearProperty("picocli.trace");

    assertArrayEquals(new String[] {"\"abc\\\"", "def"}, values);
}
 
Example #21
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 #22
Source File: ArgSplitTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgSpecSplitWithEscapedBackslashOutsideQuote() {
    PositionalParamSpec positional = PositionalParamSpec.builder().type(String[].class).splitRegex(";").build();

    System.setProperty("picocli.trace", "DEBUG");
    String value = "\\\\\"abc\\\";def\";\\\"a\\";
    String[] values = positional.splitValue(value, new CommandLine.Model.ParserSpec().splitQuotedStrings(false), CommandLine.Range.valueOf("1"), 1);
    System.clearProperty("picocli.trace");

    assertArrayEquals(new String[] {"\\\\\"abc\\\";def\"", "\\\"a\\"}, values);
}
 
Example #23
Source File: ArgSplitTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgSpecSplitWithEscapedBackslashInsideQuote() {
    PositionalParamSpec positional = PositionalParamSpec.builder().type(String[].class).splitRegex(";").build();

    System.setProperty("picocli.trace", "DEBUG");
    String value = "\"abc\\\\\\\";def\"";
    String[] values = positional.splitValue(value, new CommandLine.Model.ParserSpec().splitQuotedStrings(false), CommandLine.Range.valueOf("1"), 1);
    System.clearProperty("picocli.trace");

    assertArrayEquals(new String[] {"\"abc\\\\\\\";def\""}, values);
}
 
Example #24
Source File: AutoComplete.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
private static void addCandidatesForArgsFollowing(Object obj, List<CharSequence> candidates) {
    if (obj == null) { return; }
    if (obj instanceof CommandSpec) {
        addCandidatesForArgsFollowing((CommandSpec) obj, candidates);
    } else if (obj instanceof OptionSpec) {
        addCandidatesForArgsFollowing((OptionSpec) obj, candidates);
    } else if (obj instanceof PositionalParamSpec) {
        addCandidatesForArgsFollowing((PositionalParamSpec) obj, candidates);
    }
}
 
Example #25
Source File: ArgSplitTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgSpecSplitValue_MultipleQuotedValues_QuotesTrimmedIfRequested() {
    ParserSpec parser = new ParserSpec().trimQuotes(true);
    ArgSpec spec = PositionalParamSpec.builder().type(String[].class).splitRegex(",").build();
    String[] actual = spec.splitValue("a,b,\"c,d,e\",f,\"xxx,yyy\"", parser, Range.valueOf("0"), 0);
    assertArrayEquals(new String[]{"a", "b", "c,d,e", "f", "xxx,yyy"}, actual);
}
 
Example #26
Source File: ModelArgSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgSpecBuilderSplitRegexSynopsisLabel() {
    Builder builder = OptionSpec.builder("-x").splitRegexSynopsisLabel(";");
    assertEquals("split regex synopsis label", ";", builder.splitRegexSynopsisLabel());
    assertEquals("split regex synopsis label", ";", builder.build().splitRegexSynopsisLabel());

    PositionalParamSpec.Builder pb = PositionalParamSpec.builder().splitRegexSynopsisLabel(";");
    assertEquals("split regex synopsis label", ";", pb.splitRegexSynopsisLabel());
    assertEquals("split regex synopsis label", ";", pb.build().splitRegexSynopsisLabel());
}
 
Example #27
Source File: ArgSplitTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgSpecSplitValue_RespectsQuotedValuesByDefault() {
    ParserSpec parser = new ParserSpec();
    ArgSpec spec = PositionalParamSpec.builder().type(String[].class).splitRegex(",").build();
    String[] actual = spec.splitValue("a,b,\"c,d,e\",f", parser, Range.valueOf("0"), 0);
    assertArrayEquals(new String[]{"a", "b", "\"c,d,e\"", "f"}, actual);
}
 
Example #28
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 #29
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 #30
Source File: ModelPositionalParamSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testPositionalWithArityHasDefaultTypeString() {
    assertEquals(String.class, CommandLine.Model.PositionalParamSpec.builder().arity("0").build().type());
    assertEquals(String.class, CommandLine.Model.PositionalParamSpec.builder().arity("1").build().type());
    assertEquals(String.class, CommandLine.Model.PositionalParamSpec.builder().arity("0..1").build().type());
    assertEquals(String[].class, CommandLine.Model.PositionalParamSpec.builder().arity("2").build().type());
    assertEquals(String[].class, CommandLine.Model.PositionalParamSpec.builder().arity("0..2").build().type());
    assertEquals(String[].class, CommandLine.Model.PositionalParamSpec.builder().arity("*").build().type());
}