Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customized ClassParsers #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions args4j/src/org/kohsuke/args4j/ClassOverridableParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.kohsuke.args4j;

import org.kohsuke.args4j.spi.MethodSetter;
import org.kohsuke.args4j.spi.Setters;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashSet;


/**
* Parser for analyzing Args4J annotations in the class hierarchy, allowing annotation declarations on overridden
* members to override the annotations on the parents. The value of the overriding annotation is uses as-is; it is
* not merged with parent bindings.
*
* This can be used to feed option bindings that span across multiple instances.
*
* @author Alex Lapins
*/
public class ClassOverridableParser extends ClassParser {
@Override
public void parse(Object bean, CmdLineParser parser) {
HashSet<String> sf = new HashSet<>();
HashSet<String> sm = new HashSet<>();
for( Class c=bean.getClass(); c!=null; c=c.getSuperclass()) {
for( Method m : c.getDeclaredMethods() ) {
if (sf.contains(m.getName())) continue;
Option o = m.getAnnotation(Option.class);
if(o!=null) {
parser.addOption(new MethodSetter(parser,bean,m), o);
}
Argument a = m.getAnnotation(Argument.class);
if(a!=null) {
parser.addArgument(new MethodSetter(parser,bean,m), a);
}
sf.add(m.getName());
}

for( Field f : c.getDeclaredFields() ) {
if (sf.contains(f.getName())) continue;
Option o = f.getAnnotation(Option.class);
if(o!=null) {
parser.addOption(Setters.create(f,bean),o);
}
Argument a = f.getAnnotation(Argument.class);
if(a!=null) {
parser.addArgument(Setters.create(f,bean), a);
}
sf.add(f.getName());
}
}
}
}
44 changes: 42 additions & 2 deletions args4j/src/org/kohsuke/args4j/CmdLineParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class CmdLineParser {
*/
public CmdLineParser(Object bean) {
// for display purposes, we like the arguments in argument order, but the options in alphabetical order
this(bean, ParserProperties.defaults());
this(bean, new ClassParser(), ParserProperties.defaults());
}

/**
Expand All @@ -87,13 +87,53 @@ public CmdLineParser(Object bean) {
* if the option bean class is using args4j annotations incorrectly.
*/
public CmdLineParser(Object bean, ParserProperties parserProperties) {
this(bean, new ClassParser(), ParserProperties.defaults());
}

/**
* Creates a new command line owner that
* parses arguments/options and set them into
* the given object.
*
* @param bean
* instance of a class annotated by {@link Option} and {@link Argument}.
* this object will receive values. If this is {@code null}, the processing will
* be skipped, which is useful if you'd like to feed metadata from other sources.
*
* @param classParser class which parses annotations from given the bean
*
* @throws IllegalAnnotationError
* if the option bean class is using args4j annotations incorrectly.
*/
public CmdLineParser(Object bean, ClassParser classParser) {
this(bean, classParser, ParserProperties.defaults());
}

/**
* Creates a new command line owner that
* parses arguments/options and set them into
* the given object.
*
* @param bean
* instance of a class annotated by {@link Option} and {@link Argument}.
* this object will receive values. If this is {@code null}, the processing will
* be skipped, which is useful if you'd like to feed metadata from other sources.
*
* @param classParser class which parses annotations from given the bean
*
* @param parserProperties various settings for this class
*
* @throws IllegalAnnotationError
* if the option bean class is using args4j annotations incorrectly.
*/
public CmdLineParser(Object bean, ClassParser classParser, ParserProperties parserProperties) {
this.parserProperties = parserProperties;
// A 'return' in the constructor just skips the rest of the implementation
// and returns the new object directly.
if (bean==null) return;

// Parse the metadata and create the setters
new ClassParser().parse(bean,this);
classParser.parse(bean,this);

if (parserProperties.getOptionSorter()!=null) {
Collections.sort(options, parserProperties.getOptionSorter());
Expand Down
7 changes: 7 additions & 0 deletions args4j/test/org/kohsuke/args4j/InheritanceOverride.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.kohsuke.args4j;

public class InheritanceOverride extends Inheritance {

@Option(name="-m", required = true)
public String me;
}
37 changes: 37 additions & 0 deletions args4j/test/org/kohsuke/args4j/InheritanceOverrideTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.kohsuke.args4j;

public class InheritanceOverrideTest extends Args4JTestBase<InheritanceOverride> {
@Override
public InheritanceOverride getTestObject() {
return new InheritanceOverride();
}

@Override
protected CmdLineParser createParser() {
return new CmdLineParser(testObject, new ClassOverridableParser());
}

public void testMyselfRequired() {
args = new String[]{"-f", "My father"};
InheritanceOverride bo = testObject;
try {
parser.parseArgument(args);
System.out.println("ParsedCorrectly");
assertEquals("Value for class itself not arrived", "Thats me", bo.me);
} catch (CmdLineException e) {
System.out.println("ParseFailed");
assertEquals("Option \"-m\" is required", e.getMessage());
}
}

public void testMyself() {
args = new String[]{"-m", "Thats me"};
InheritanceOverride bo = testObject;
try {
parser.parseArgument(args);
assertEquals("Value for class itself not arrived", "Thats me", bo.me);
} catch (CmdLineException e) {
fail("This exception should not occur");
}
}
}