Skip to content

Commit

Permalink
[#182] added convenience method for annotated interface, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed May 22, 2018
1 parent bd9c27c commit 8ddff98
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,34 @@ public static <T> T populateCommand(T command, String... args) {
return command;
}

/**
* <p>
* Convenience method that derives the command specification from the specified interface class, and returns an
* instance of the specified interface. The interface is expected to have annotated getter methods. Picocli will
* instantiate the interface and the getter methods will return the option and positional parameter values matched on the command line.
* </p><p>
* This is equivalent to
* </p><pre>
* CommandLine cli = new CommandLine(spec);
* cli.parse(args);
* return cli.getCommand();
* </pre>
*
* @param spec the interface that defines the command specification. This object contains getter methods annotated with
* {@code @Option} or {@code @Parameters}.
* @param args the command line arguments to parse
* @param <T> the type of the annotated object
* @return an instance of the specified annotated interface
* @throws InitializationException if the specified command object does not have a {@link Command}, {@link Option} or {@link Parameters} annotation
* @throws ParameterException if the specified command line arguments are invalid
* @since 3.1
*/
public static <T> T populateSpec(Class<T> spec, String... args) {
CommandLine cli = toCommandLine(spec, new DefaultFactory());
cli.parse(args);
return cli.getCommand();
}

/** Parses the specified command line arguments and returns a list of {@code CommandLine} objects representing the
* top-level command and any subcommands (if any) that were recognized and initialized during the parsing process.
* <p>
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/picocli/CommandLineAnnotatedMethodImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ static class Objects {
@Option(names = "-string") void setString(String val) { aString = val; }
@Option(names = "-list") void setList(List<String> val) { aList = val; }

@Option(names = "-map", type = {Integer.class, Double.class})
@Option(names = "-map")
void setMap(Map<Integer, Double> val) { aMap = val; }

@Option(names = "-set", type = Short.class)
@Option(names = "-set")
void setSortedSet(SortedSet<Short> val) { aSet = val; }
}

Expand Down
26 changes: 24 additions & 2 deletions src/test/java/picocli/CommandLineAnnotatedMethodSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ interface Objects {
@Option(names = "-list")
List<String> getList();

@Option(names = "-map", type = {Integer.class, Double.class})
@Option(names = "-map")
Map<Integer, Double> getMap();

@Option(names = "-set", type = Short.class)
@Option(names = "-set")
SortedSet<Short> getSortedSet();
}

Expand Down Expand Up @@ -189,4 +189,26 @@ public void testAnnotatedMutableFieldsOnInterfaceAreValid() {
assertEquals("Invalid picocli annotation on interface field", ok.getMessage());
}
}

@Test
public void testPopulateSpec() {
Objects objects = CommandLine.populateSpec(Objects.class, "-b -y1 -s2 -i3 -l4 -f5 -d6 -bigint=7 -string abc -list a -list b -map 1=2.0 -set 33 -set 22".split(" "));
assertTrue(objects.aBoolean());
assertEquals(Byte.valueOf((byte) 1), objects.aByte());
assertEquals(Short.valueOf((short) 2), objects.aShort());
assertEquals(Integer.valueOf(3), objects.anInt());
assertEquals(Long.valueOf(4), objects.aLong());
assertEquals(5f, objects.aFloat(), 0.0001);
assertEquals(6d, objects.aDouble(), 0.0001);
assertEquals(BigInteger.valueOf(7), objects.aBigInteger());
assertEquals("abc", objects.aString());
assertEquals(Arrays.asList("a", "b"), objects.getList());
Map<Integer, Double> map = new HashMap<Integer, Double>();
map.put(1, 2.0);
assertEquals(map, objects.getMap());
Set<Short> set = new TreeSet<Short>();
set.add((short) 22);
set.add((short) 33);
assertEquals(set, objects.getSortedSet());
}
}

0 comments on commit 8ddff98

Please sign in to comment.