Class CommandLineParser
- java.lang.Object
-
- com.gengoai.application.CommandLineParser
-
public final class CommandLineParser extends Object
A command line parser that can handle non-specified arguments. Arguments can be specified by manually adding
NamedOption
s via theaddOption(NamedOption)
method or by setting the parser's owner object via the constructor which will look for fields annotated withOption
. All parsers will have help (-h or --help), config (--config), and explain config (--config-explain) options added.The parser accepts long (e.g. --longOption) and short (e.g. -s) arguments. Multiple short (e.g. single character) arguments can be specified at one time (e.g. -xzf would set the x, z, and f options to true). Short arguments may have values (e.g. -f FILENAME).
Values for options will be specified on the corresponding
NamedOption
instance. The value can be retrieved either directly from the NamedOption or by using theget(String)
method. Argument names need not specify the "--" or "-" prefix.- Author:
- David B. Bracewell
-
-
Constructor Summary
Constructors Constructor Description CommandLineParser()
Instantiates a new Command line parser.CommandLineParser(Object owner, String applicationDescription)
Instantiates a new Command line parser.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CommandLineParser
addOption(NamedOption namedOption)
Add option.<T> T
get(String optionName)
Gets the value for the specified optionSet<NamedOption>
getOptions()
Gets the specified options.Set<Map.Entry<String,String>>
getSetEntries()
Gets options and values for everything passed in to the command line including unamed options.boolean
isSet(NamedOption option)
Determines if an option was set or not.boolean
isSet(String optionName)
Determines if an option was set or not.String[]
parse(String[] args)
Parses an array of argumentsvoid
showHelp()
Prints help to standard error showing the application description, if set, and the list of valid command line arguments with those required arguments marked with an asterisk.
-
-
-
Method Detail
-
addOption
public CommandLineParser addOption(NamedOption namedOption)
Add option.- Parameters:
namedOption
- the named option
-
get
public <T> T get(String optionName)
Gets the value for the specified option- Type Parameters:
T
- the type of the value for the option- Parameters:
optionName
- the name of the option whose value we want to retrieve- Returns:
- the value of the option or null if not set
-
getOptions
public Set<NamedOption> getOptions()
Gets the specified options.- Returns:
- the specified options
-
getSetEntries
public Set<Map.Entry<String,String>> getSetEntries()
Gets options and values for everything passed in to the command line including unamed options.- Returns:
- An
Map.Entry
of options and values for everything passed in to the command line including unamed options.
-
isSet
public boolean isSet(String optionName)
Determines if an option was set or not.- Parameters:
optionName
- the option name- Returns:
- True if it was set (boolean options must be true), False otherwise
-
isSet
public boolean isSet(NamedOption option)
Determines if an option was set or not.- Parameters:
option
- the option to check- Returns:
- True if it was set (boolean options must be true), False otherwise
-
parse
public String[] parse(String[] args)
Parses an array of arguments- Parameters:
args
- The command line arguments.- Returns:
- the non-config/option parameters
-
showHelp
public void showHelp()
Prints help to standard error showing the application description, if set, and the list of valid command line arguments with those required arguments marked with an asterisk.
-
-