Skip to content

Commit

Permalink
#130 improved tracing for positional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Oct 12, 2017
1 parent faf8311 commit 7a6ae6b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,7 @@ private void processPositionalParameter(Collection<Field> required, Set<Field> i
// remove processed args from the stack
for (int i = 0; i < consumed; i++) { args.pop(); }
position += consumed;
if (tracer.isDebug()) {tracer.debug("Consumed %d arguments, moving position to index %d.%n", consumed, position);}
if (consumed == 0 && !args.isEmpty()) {
handleUnmatchedArguments(args.pop());
}
Expand Down
54 changes: 52 additions & 2 deletions src/test/java/picocli/CommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1163,13 +1163,60 @@ public void testShortOptionsWithSeparatorButNoValueAssignsEmptyStringIfLast() {
verifyCompact(compact, true, true, "", null);
}


@Test
public void testDoubleDashSeparatesPositionalParameters() {
CompactFields compact = CommandLine.populateCommand(new CompactFields(), "-oout -- -r -v p1 p2".split(" "));
verifyCompact(compact, false, false, "out", fileArray("-r", "-v", "p1", "p2"));
}

@Test
public void testDebugOutputForDoubleDashSeparatesPositionalParameters() throws UnsupportedEncodingException {
PrintStream originalErr = System.err;
ByteArrayOutputStream baos = new ByteArrayOutputStream(2500);
System.setErr(new PrintStream(baos));
final String PROPERTY = "picocli.trace";
String old = System.getProperty(PROPERTY);
System.setProperty(PROPERTY, "DEBUG");
CommandLine.populateCommand(new CompactFields(), "-oout -- -r -v p1 p2".split(" "));
System.setErr(originalErr);
if (old == null) {
System.clearProperty(PROPERTY);
} else {
System.setProperty(PROPERTY, old);
}
String expected = String.format("" +
"[picocli INFO] Parsing 6 command line args [-oout, --, -r, -v, p1, p2]%n" +
"[picocli DEBUG] Initializing picocli.CommandLineTest$CompactFields: 3 options, 1 positional parameters, 0 required, 0 subcommands.%n" +
"[picocli DEBUG] Processing argument '-oout'. Remainder=[--, -r, -v, p1, p2]%n" +
"[picocli DEBUG] '-oout' cannot be separated into <option>=<option-parameter>%n" +
"[picocli DEBUG] Trying to process '-oout' as clustered short options%n" +
"[picocli DEBUG] Found option '-o' in -oout: field java.io.File picocli.CommandLineTest$CompactFields.outputFile, arity=1%n" +
"[picocli DEBUG] Trying to process 'out' as option parameter%n" +
"[picocli INFO] Setting File field 'CompactFields.outputFile' to 'out' (was 'null') for option -o%n" +
"[picocli DEBUG] Processing argument '--'. Remainder=[-r, -v, p1, p2]%n" +
"[picocli INFO] Found end-of-options delimiter '--'. Treating remainder as positional parameters.%n" +
"[picocli DEBUG] Processing next arg as a positional parameter at index=0. Remainder=[-r, -v, p1, p2]%n" +
"[picocli DEBUG] Position 0 is in index range 0..*. Trying to assign args to java.io.File[] picocli.CommandLineTest$CompactFields.inputFiles, arity=0..1%n" +
"[picocli INFO] Adding [-r] to File[] field 'CompactFields.inputFiles' for args[0..*]%n" +
"[picocli DEBUG] Consumed 1 arguments, moving position to index 1.%n" +
"[picocli DEBUG] Processing next arg as a positional parameter at index=1. Remainder=[-v, p1, p2]%n" +
"[picocli DEBUG] Position 1 is in index range 0..*. Trying to assign args to java.io.File[] picocli.CommandLineTest$CompactFields.inputFiles, arity=0..1%n" +
"[picocli INFO] Adding [-v] to File[] field 'CompactFields.inputFiles' for args[0..*]%n" +
"[picocli DEBUG] Consumed 1 arguments, moving position to index 2.%n" +
"[picocli DEBUG] Processing next arg as a positional parameter at index=2. Remainder=[p1, p2]%n" +
"[picocli DEBUG] Position 2 is in index range 0..*. Trying to assign args to java.io.File[] picocli.CommandLineTest$CompactFields.inputFiles, arity=0..1%n" +
"[picocli INFO] Adding [p1] to File[] field 'CompactFields.inputFiles' for args[0..*]%n" +
"[picocli DEBUG] Consumed 1 arguments, moving position to index 3.%n" +
"[picocli DEBUG] Processing next arg as a positional parameter at index=3. Remainder=[p2]%n" +
"[picocli DEBUG] Position 3 is in index range 0..*. Trying to assign args to java.io.File[] picocli.CommandLineTest$CompactFields.inputFiles, arity=0..1%n" +
"[picocli INFO] Adding [p2] to File[] field 'CompactFields.inputFiles' for args[0..*]%n" +
"[picocli DEBUG] Consumed 1 arguments, moving position to index 4.%n",
new File("/home/rpopma/picocli"));
String actual = new String(baos.toByteArray(), "UTF8");
//System.out.println(actual);
assertEquals(expected, actual);
}

private File[] fileArray(final String ... paths) {
File[] result = new File[paths.length];
for (int i = 0; i < result.length; i++) {
Expand Down Expand Up @@ -2681,12 +2728,15 @@ public void testTracingDebugWithSubCommands() throws Exception {
"[picocli DEBUG] Processing next arg as a positional parameter at index=0. Remainder=[src1.java, src2.java, src3.java]%n" +
"[picocli DEBUG] Position 0 is in index range 0..*. Trying to assign args to java.util.List picocli.Demo$GitCommit.files, arity=0..1%n" +
"[picocli INFO] Adding [src1.java] to List<File> field 'GitCommit.files' for args[0..*]%n" +
"[picocli DEBUG] Consumed 1 arguments, moving position to index 1.%n" +
"[picocli DEBUG] Processing next arg as a positional parameter at index=1. Remainder=[src2.java, src3.java]%n" +
"[picocli DEBUG] Position 1 is in index range 0..*. Trying to assign args to java.util.List picocli.Demo$GitCommit.files, arity=0..1%n" +
"[picocli INFO] Adding [src2.java] to List<File> field 'GitCommit.files' for args[0..*]%n" +
"[picocli DEBUG] Consumed 1 arguments, moving position to index 2.%n" +
"[picocli DEBUG] Processing next arg as a positional parameter at index=2. Remainder=[src3.java]%n" +
"[picocli DEBUG] Position 2 is in index range 0..*. Trying to assign args to java.util.List picocli.Demo$GitCommit.files, arity=0..1%n" +
"[picocli INFO] Adding [src3.java] to List<File> field 'GitCommit.files' for args[0..*]%n",
"[picocli INFO] Adding [src3.java] to List<File> field 'GitCommit.files' for args[0..*]%n" +
"[picocli DEBUG] Consumed 1 arguments, moving position to index 3.%n",
new File("/home/rpopma/picocli"));
String actual = new String(baos.toByteArray(), "UTF8");
//System.out.println(actual);
Expand Down

0 comments on commit 7a6ae6b

Please sign in to comment.