Package openbook.tools
Class CommandProcessor
- java.lang.Object
-
- openbook.tools.CommandProcessor
-
public class CommandProcessor extends java.lang.Object
Processes options.
User can register a set of command options. Then this processor will parse a set of Strings to store the values for each of the registered options as well as optionally any unrecognized option values.- Author:
- Pinaki Poddar
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CommandProcessor.Option
A simple immutable object represents meta-data about a command option.
-
Constructor Summary
Constructors Constructor Description CommandProcessor()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CommandProcessor.Option
findCommand(java.lang.String option)
Finds a command with the given name.boolean
getAllowsUnregisteredOption()
java.util.Set<CommandProcessor.Option>
getUnregisteredCommands()
Gets all the unrecognized command options.java.lang.String
getValue(java.lang.String alias)
Gets value of the option matching the given alias.int
lastIndex(java.lang.String[] args)
Gets the last index in the given array that can be processed as an option.CommandProcessor.Option
register(boolean requiresValue, java.lang.String... aliases)
Register the given aliases as a command option.void
setAllowsUnregisteredOption(boolean allowsUnregisteredOption)
java.lang.String[]
setFrom(java.lang.String[] args)
Set the option values from the given arguments.java.lang.String[]
setFrom(java.lang.String[] args, int from, int to)
Set the option values from the given arguments between the given indices.
-
-
-
Method Detail
-
setFrom
public java.lang.String[] setFrom(java.lang.String[] args)
Set the option values from the given arguments. All elements of the given array is not consumed, only till the index that appears to be a valid option.- Parameters:
args
- an array of arguments.- Returns:
- the array elements that are not consumed.
- See Also:
lastIndex(String[])
-
setFrom
public java.lang.String[] setFrom(java.lang.String[] args, int from, int to)
Set the option values from the given arguments between the given indices.- Parameters:
args
- an array of arguments.- Returns:
- the array elements that are not consumed.
- See Also:
lastIndex(String[])
-
lastIndex
public int lastIndex(java.lang.String[] args)
Gets the last index in the given array that can be processed as an option. The array elements are sequentially tested if they are a valid option name (i.e. starts with - character) and if valid then the next element is consumed as value, if the option requires a value. The search ends when either the array is exhausted or encounters elements that are not options.- Parameters:
args
- an array of arguments- Returns:
- the last index that will/can be consumed by this processor.
-
register
public CommandProcessor.Option register(boolean requiresValue, java.lang.String... aliases)
Register the given aliases as a command option.- Parameters:
requiresValue
- if true then the option must be specified with a value.aliases
- strings to recognize this option. Each must begin with a dash character.- Returns:
- the command that is registered
-
findCommand
public CommandProcessor.Option findCommand(java.lang.String option)
Finds a command with the given name. If no command has been registered with the given name, but this processor allows unrecognized options, then as a result of this call, the unknown name is registered as an option.- Parameters:
option
- a command alias.- Returns:
- null if the given String is not a valid command option name.
-
getUnregisteredCommands
public java.util.Set<CommandProcessor.Option> getUnregisteredCommands()
Gets all the unrecognized command options.- Returns:
- empty set if no commands are unrecognized.
-
getValue
public java.lang.String getValue(java.lang.String alias)
Gets value of the option matching the given alias.- Parameters:
alias
- an alias.- Returns:
- value of the given option.
-
getAllowsUnregisteredOption
public boolean getAllowsUnregisteredOption()
- Returns:
- the allowsUnregisteredOption
-
setAllowsUnregisteredOption
public void setAllowsUnregisteredOption(boolean allowsUnregisteredOption)
- Parameters:
allowsUnregisteredOption
- the allowsUnregisteredOption to set
-
-