-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
706 additions
and
1,010 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,94 +12,83 @@ | |
* The facade class that parses arguments and sets values to given bean | ||
* | ||
* @author <a href="mailto:[email protected]">Jiaqi Guo</a> | ||
* @param <T> | ||
* Type of bean it processes. | ||
* @param <T> Type of bean it processes. | ||
*/ | ||
public abstract class ArgumentProcessor<T> { | ||
/** | ||
* Create new instance with default parser, a {@link GnuParser} | ||
* | ||
* @param <T> | ||
* Type of the bean | ||
* @param beanType | ||
* Type of the bean | ||
* @return Instance of an implementation of argument processor | ||
*/ | ||
public static <T> ArgumentProcessor<T> forType(Class<T> beanType) { | ||
return newInstance(beanType, new GnuParser()); | ||
} | ||
/** | ||
* Create new instance with default parser, a {@link GnuParser} | ||
* | ||
* @param <T> Type of the bean | ||
* @param beanType Type of the bean | ||
* @return Instance of an implementation of argument processor | ||
*/ | ||
public static <T> ArgumentProcessor<T> forType(Class<T> beanType) { | ||
return newInstance(beanType, new GnuParser()); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T> ArgumentProcessor<T> forTypeOf(T bean) { | ||
return forType((Class<T>) bean.getClass()); | ||
} | ||
@SuppressWarnings("unchecked") | ||
public static <T> ArgumentProcessor<T> forTypeOf(T bean) { | ||
return forType((Class<T>) bean.getClass()); | ||
} | ||
|
||
/** | ||
* Create new instance with given bean type and command line parser that | ||
* describes command line sytnax. | ||
* | ||
* @param <T> | ||
* type of bean | ||
* @param beanType | ||
* Type of bean | ||
* @param parser | ||
* command line parser that is aware of command line syntax | ||
* @return instance of an implementation of argument processor. | ||
*/ | ||
public static <T> ArgumentProcessor<T> newInstance( | ||
Class<? extends T> beanType, CommandLineParser parser) { | ||
return ArgumentProcessorFactory.getInstance().newProcessor(beanType, | ||
parser); | ||
} | ||
/** | ||
* Create new instance with given bean type and command line parser that describes command line | ||
* sytnax. | ||
* | ||
* @param <T> type of bean | ||
* @param beanType Type of bean | ||
* @param parser command line parser that is aware of command line syntax | ||
* @return instance of an implementation of argument processor. | ||
*/ | ||
public static <T> ArgumentProcessor<T> newInstance(Class<? extends T> beanType, | ||
CommandLineParser parser) { | ||
return ArgumentProcessorFactory.getInstance().newProcessor(beanType, parser); | ||
} | ||
|
||
/** | ||
* @deprecated Use {@link #forType(Class)} instead. | ||
*/ | ||
public static <T> ArgumentProcessor<T> newInstance(Class<T> beanType) { | ||
return forType(beanType); | ||
} | ||
/** | ||
* @param beanType type of the bean. | ||
* @param <T> type of bean class. | ||
* @return instance of argument processor for given type of bean. | ||
* @deprecated Use {@link #forType(Class)} instead. | ||
*/ | ||
public static <T> ArgumentProcessor<T> newInstance(Class<T> beanType) { | ||
return forType(beanType); | ||
} | ||
|
||
/** | ||
* @return Implementation of parsing context | ||
*/ | ||
public abstract ParsingContext createParsingContext(); | ||
/** | ||
* @return Implementation of parsing context | ||
*/ | ||
public abstract ParsingContext createParsingContext(); | ||
|
||
/** | ||
* @param out | ||
* Output to print help message to | ||
* @throws IOException | ||
* Allows IO errors | ||
*/ | ||
public abstract void printHelp(PrintWriter out) throws IOException; | ||
/** | ||
* @param out Output to print help message to | ||
* @throws IOException Allows IO errors | ||
*/ | ||
public abstract void printHelp(PrintWriter out) throws IOException; | ||
|
||
/** | ||
* Process argument list and pass values to given bean | ||
* | ||
* @param arguments | ||
* List of arguments | ||
* @param bean | ||
* Bean to pass values to | ||
*/ | ||
public abstract void process(List<String> arguments, T bean); | ||
/** | ||
* Process argument list and pass values to given bean | ||
* | ||
* @param arguments List of arguments | ||
* @param bean Bean to pass values to | ||
*/ | ||
public abstract void process(List<String> arguments, T bean); | ||
|
||
/** | ||
* Process argument array and pass values to given bean | ||
* | ||
* @param arguments | ||
* Arary of arguments | ||
* @param bean | ||
* Bean to pass values to | ||
*/ | ||
public void process(String[] arguments, T bean) { | ||
process(Arrays.asList(arguments), bean); | ||
} | ||
/** | ||
* Process argument array and pass values to given bean | ||
* | ||
* @param arguments Arary of arguments | ||
* @param bean Bean to pass values to | ||
*/ | ||
public void process(String[] arguments, T bean) { | ||
process(Arrays.asList(arguments), bean); | ||
} | ||
|
||
/** | ||
* Verify if given arguments meet requirement defined for processor | ||
* | ||
* @param arguments | ||
* Array of command line arguments | ||
* @return | ||
*/ | ||
public abstract ValidationResult validate(String[] arguments); | ||
/** | ||
* Verifies if given arguments meet requirement defined for processor. | ||
* | ||
* @param arguments array of command line arguments. | ||
* @return a result object. | ||
*/ | ||
public abstract ValidationResult validate(String[] arguments); | ||
} |
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
import org.cyclopsgroup.jcli.spi.CommandLineParser; | ||
|
||
/** | ||
* Factory class for {@link ArguemntProcessor} | ||
* Factory class for {@link ArgumentProcessor} | ||
* | ||
* @author <a href="mailto:[email protected]">Jiaqi Guo</a> | ||
*/ | ||
|
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 |
---|---|---|
|
@@ -11,74 +11,47 @@ | |
/** | ||
* @author <a href="mailto:[email protected]">Jiaqi Guo</a> | ||
*/ | ||
public class GnuParser | ||
implements CommandLineParser | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
@Override | ||
public CommandLine parse( List<String> arguments, ParsingContext context ) | ||
{ | ||
CommandLineBuilder builder = new CommandLineBuilder(); | ||
boolean expectingOptionValue = false; | ||
String optionName = null; | ||
boolean shortOption = false; | ||
for ( String arg : arguments ) | ||
{ | ||
if ( expectingOptionValue ) | ||
{ | ||
if ( shortOption ) | ||
{ | ||
builder.withShortOption( optionName, arg ); | ||
} | ||
else | ||
{ | ||
builder.withLongOption( optionName, arg ); | ||
} | ||
expectingOptionValue = false; | ||
} | ||
else if ( arg.startsWith( "--" ) ) | ||
{ | ||
optionName = arg.substring( 2 ); | ||
Option opt = context.optionWithLongName( optionName ); | ||
if ( opt == null ) | ||
{ | ||
builder.withArgument( arg ); | ||
} | ||
else if ( opt.isFlag() ) | ||
{ | ||
builder.withLongFlag( optionName ); | ||
} | ||
else | ||
{ | ||
expectingOptionValue = true; | ||
shortOption = false; | ||
} | ||
} | ||
else if ( arg.startsWith( "-" ) ) | ||
{ | ||
optionName = arg.substring( 1 ); | ||
Option opt = context.optionWithShortName( optionName ); | ||
if ( opt == null ) | ||
{ | ||
builder.withArgument( arg ); | ||
} | ||
else if ( opt.isFlag() ) | ||
{ | ||
builder.withShortFlag( optionName ); | ||
} | ||
else | ||
{ | ||
expectingOptionValue = true; | ||
shortOption = true; | ||
} | ||
} | ||
else | ||
{ | ||
builder.withArgument( arg ); | ||
} | ||
public class GnuParser implements CommandLineParser { | ||
@Override | ||
public CommandLine parse(List<String> arguments, ParsingContext context) { | ||
CommandLineBuilder builder = new CommandLineBuilder(); | ||
boolean expectingOptionValue = false; | ||
String optionName = null; | ||
boolean shortOption = false; | ||
for (String arg : arguments) { | ||
if (expectingOptionValue) { | ||
if (shortOption) { | ||
builder.withShortOption(optionName, arg); | ||
} else { | ||
builder.withLongOption(optionName, arg); | ||
} | ||
return builder.toCommandLine(); | ||
expectingOptionValue = false; | ||
} else if (arg.startsWith("--")) { | ||
optionName = arg.substring(2); | ||
Option opt = context.optionWithLongName(optionName); | ||
if (opt == null) { | ||
builder.withArgument(arg); | ||
} else if (opt.isFlag()) { | ||
builder.withLongFlag(optionName); | ||
} else { | ||
expectingOptionValue = true; | ||
shortOption = false; | ||
} | ||
} else if (arg.startsWith("-")) { | ||
optionName = arg.substring(1); | ||
Option opt = context.optionWithShortName(optionName); | ||
if (opt == null) { | ||
builder.withArgument(arg); | ||
} else if (opt.isFlag()) { | ||
builder.withShortFlag(optionName); | ||
} else { | ||
expectingOptionValue = true; | ||
shortOption = true; | ||
} | ||
} else { | ||
builder.withArgument(arg); | ||
} | ||
} | ||
return builder.toCommandLine(); | ||
} | ||
} |
Oops, something went wrong.