public abstract class JSAPArgsParsingCommandPlugin extends AbstractCommandPlugin implements CommandPlugin
CommandPlugin
which uses JSAP
to parse command arguments and provide helpful error messages.
The parser can be configured when @{link configureArgsParser(JSAP)
is called.
JSAP has a 'fluid' API where methods are chained together to configure a specific parameter.
To register a parameter:
argsParser.registerParameter(new UnflaggedOption("my-option").setRequired(true).setHelp("a helpful message about this arg"));
To set a 'port' flag which is used via the command arguments as either '-port XYZ' or '-p XYZ' use:
argsParser.registerParameter(new FlaggedOption("port").setShortFlag('p').setLongFlag("port").setRequired(false).setHelp("the port number"));
Command arguments will be automatically parsed and provided when doCommand(JSAPResult)
is called. Argument values are accessed
by name, for example:
parsedArgs.getString("some-arg");
parsedArgs.getBoolean("some-arg");
Constructor and Description |
---|
JSAPArgsParsingCommandPlugin() |
Modifier and Type | Method and Description |
---|---|
protected abstract void |
configureArgsParser(com.martiansoftware.jsap.JSAP argsParser) |
protected abstract int |
doCommand(com.martiansoftware.jsap.JSAPResult parsedArgs) |
int |
doCommand(java.lang.String... args)
Runs the command using the provided user arguments.
|
java.lang.String |
getCommandHelp()
Returns a detailed help message that describes the various parameters the command provides, and how they are used.
|
java.lang.String |
getCommandUsage()
Returns a usage message that shows the user an example of how the command parameters are used.
|
castTo, close, equals, getPluginClass, instanceOf
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getCommandDescription, getCommandName
castTo, close, getPluginClass, instanceOf, setBRJS
protected abstract void configureArgsParser(com.martiansoftware.jsap.JSAP argsParser) throws com.martiansoftware.jsap.JSAPException
com.martiansoftware.jsap.JSAPException
protected abstract int doCommand(com.martiansoftware.jsap.JSAPResult parsedArgs) throws CommandArgumentsException, CommandOperationException
public final java.lang.String getCommandUsage()
CommandPlugin
Note: This method doesn't need to be implemented if you've chosen to extend JSAPArgsParsingCommandPlugin
.
getCommandUsage
in interface CommandPlugin
public java.lang.String getCommandHelp()
CommandPlugin
Note: This method doesn't need to be implemented if you've chosen to extend JSAPArgsParsingCommandPlugin
.
getCommandHelp
in interface CommandPlugin
public final int doCommand(java.lang.String... args) throws CommandArgumentsException, CommandOperationException
CommandPlugin
doCommand
in interface CommandPlugin
args
- The list of arguments provided by the user.CommandArgumentsException
- if any invalid arguments were provided.CommandOperationException
- if a problem was encountered while running the command.