Skip to content

Commit

Permalink
support of argument fields in superclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
viphe committed Jun 28, 2013
1 parent 86ee326 commit 2323da8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/java/com/sampullara/cli/Args.java
Expand Up @@ -39,9 +39,15 @@ public static List<String> parse(Object target, String[] args) {
// If its not a JavaBean we ignore it
}
}
for (Field field : clazz.getDeclaredFields()) {
processField(target, field, arguments);

// Check fields of 'target' class and its superclasses
while (clazz != null) {
for (Field field : clazz.getDeclaredFields()) {
processField(target, field, arguments);
}
clazz = clazz.getSuperclass();
}

for (String argument : arguments) {
if (argument.startsWith("-")) {
throw new IllegalArgumentException("Invalid argument: " + argument);
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/sampullara/cli/ArgsTest.java
@@ -1,6 +1,7 @@
package com.sampullara.cli;

import junit.framework.TestCase;
import org.junit.Test;

import java.io.File;
import java.util.List;
Expand Down Expand Up @@ -102,6 +103,14 @@ public void testStaticCommand() {
assertEquals("extra", extra.get(0));
}

public void testDerivedCommand() {
String[] args = { "-help", "-verbose" };
TestCommand6 tc = new TestCommand6();
Args.parse(tc, args);
assertTrue(tc.help);
assertTrue(tc.verbose);
}

public static class TestCommand {
@Argument(value = "input", description = "This is the input file", required = true)
private String inputFilename;
Expand Down Expand Up @@ -266,4 +275,14 @@ public static class TestCommand4 {
@Argument(required = true)
private static String output;
}

public static abstract class TestCommand5 {
@Argument
public boolean help;
}

public static class TestCommand6 extends TestCommand5 {
@Argument
public boolean verbose;
}
}

0 comments on commit 2323da8

Please sign in to comment.