-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#761] API: Added method
ParseResult.matchedArgs()
that returns all…
… matched options and positional params * Add `ParseResult.matchedArgs()` method to return all matched arguments in order; * change `ParseResult.matchedOptions()` and `ParseResult.matchedPositionals()` to return the full list of matched options and positional parameters, including duplicates if the option or positional parameter was matched multiple times. * Add new `ParseResult.matchedOptionSet()` and `ParseResult.matchedPositionalSet()` methods that return a `Set`.
- Loading branch information
Showing
4 changed files
with
128 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package picocli; | ||
|
||
import org.junit.Test; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Model.ArgSpec; | ||
import picocli.CommandLine.Model.CommandSpec; | ||
import picocli.CommandLine.Model.OptionSpec; | ||
import picocli.CommandLine.Option; | ||
import picocli.CommandLine.ParseResult; | ||
import picocli.CommandLine.Spec; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
// https://github.com/remkop/picocli/issues/761 | ||
public class OrderedOptionsTest { | ||
@Command(name = "rsync") | ||
static class Rsync implements Runnable { | ||
|
||
@Option(names = "--include") | ||
List<String> includes; | ||
|
||
@Option(names = "--exclude") | ||
List<String> excludes; | ||
|
||
@Spec CommandSpec spec; | ||
|
||
public void run() { | ||
ParseResult pr = spec.commandLine().getParseResult(); | ||
List<ArgSpec> optionSpecs = pr.matchedArgs(); | ||
// do something | ||
} | ||
} | ||
|
||
@Test | ||
public void testOrderWithParseResult() { | ||
CommandLine cmd = new CommandLine(new Rsync()); | ||
ParseResult parseResult = cmd.parseArgs("--include", "a", "--exclude", "b", "--include", "c", "--exclude", "d"); | ||
List<ArgSpec> argSpecs = parseResult.matchedArgs(); | ||
assertEquals(4, argSpecs.size()); | ||
assertEquals("--include", ((OptionSpec) argSpecs.get(0)).longestName()); | ||
assertEquals("--exclude", ((OptionSpec) argSpecs.get(1)).longestName()); | ||
assertEquals("--include", ((OptionSpec) argSpecs.get(2)).longestName()); | ||
assertEquals("--exclude", ((OptionSpec) argSpecs.get(3)).longestName()); | ||
|
||
List<OptionSpec> matchedOptions = parseResult.matchedOptions(); | ||
assertEquals(4, matchedOptions.size()); | ||
|
||
assertEquals("--include", matchedOptions.get(0).longestName()); | ||
assertSame(matchedOptions.get(0), matchedOptions.get(2)); | ||
assertEquals(Arrays.asList("a"), matchedOptions.get(0).typedValues().get(0)); | ||
assertEquals(Arrays.asList("c"), matchedOptions.get(2).typedValues().get(1)); | ||
|
||
assertEquals("--exclude", matchedOptions.get(1).longestName()); | ||
assertSame(matchedOptions.get(1), matchedOptions.get(3)); | ||
assertEquals(Arrays.asList("b"), matchedOptions.get(1).typedValues().get(0)); | ||
assertEquals(Arrays.asList("d"), matchedOptions.get(3).typedValues().get(1)); | ||
} | ||
} |