Class CommandLineArguments
- java.lang.Object
-
- de.bernd_michaely.common.cli.parser.CommandLineArguments
-
public class CommandLineArguments extends Object
Simple reimplementation of a GNU getopt style command line argument parsing. The command line arguments must be given as follows:- Each command line option has a mandatory long form, prefixed with '--', and optionally an additional short form, prefixed with '-'.
- Each command line option can optionally have a parameter, which can be optional or mandatory.
- Each command line option parameter can optionally be described by a regular expression it has to match.
- For options with optional parameters, the parameter must immediately
follow the option, e.g.
'
-o param
' or '--option=param
'. - Non-option arguments are first assigned to command line options still
missing a required parameter in the order of their appearance. E.g. if a, b,
c are options with required parameters,
and-a -b -c one two three four
and-abc one two three four
have the same meaning, where 'four' remains as a non-option argument, returned by the-a one -b two -c three four
parse(String...)
method. - The command line argument '-' ends the parsing of options so that all following command line arguments are considered to be non-option arguments. This allows non-option arguments to start with '-' characters without being confused with options.
-
-
Constructor Summary
Constructors Constructor Description CommandLineArguments(BiConsumer<String,String> callback)
Constructs a command line argument parser.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CommandLineArguments
addFlagOption(String longOption, char shortOption, String... description)
Adds a command line option which does not expect a parameter.CommandLineArguments
addFlagOption(String longOption, String... description)
Adds a command line option which does not expect a parameter.CommandLineArguments
addParameterOption(String longOption, boolean paramRequired, String... description)
Adds a command line option which expects a parameter.CommandLineArguments
addParameterOption(String longOption, char shortOption, boolean paramRequired, String... description)
Adds a command line option which expects a parameter.CommandLineArguments
addParameterOption(String longOption, char shortOption, String paramRegExpr, boolean paramRequired, String... description)
Adds a command line option which expects a parameter.CommandLineArguments
addParameterOption(String longOption, String paramRegExpr, boolean paramRequired, String... description)
Adds a command line option which expects a parameter.List<String>
getFormattedDescription(boolean withHeader)
Returns a formatted description of the defined options.List<String>
parse(String... args)
Parse command line arguments.void
printFormattedDescription(PrintStream printStream, boolean withHeader)
Prints a formatted description of the defined options.
-
-
-
Constructor Detail
-
CommandLineArguments
public CommandLineArguments(BiConsumer<String,String> callback)
Constructs a command line argument parser.- Parameters:
callback
- aBiConsumer
to call for each command line argument, in the order of appearance of the options. The first string passes the long form of the option, which is never null. The second string passes the parameter given with the option, which may be null, if an optional parameter is not given or the argument is a flag option.
-
-
Method Detail
-
addFlagOption
public CommandLineArguments addFlagOption(String longOption, String... description) throws OptionDefinitionException
Adds a command line option which does not expect a parameter.- Parameters:
longOption
- the long form of the optiondescription
- a multi line description for the option- Returns:
- this CommandLineArguments
- Throws:
OptionDefinitionException
- if the same option is defined twice
-
addFlagOption
public CommandLineArguments addFlagOption(String longOption, char shortOption, String... description) throws OptionDefinitionException
Adds a command line option which does not expect a parameter.- Parameters:
longOption
- the long form of the optionshortOption
- the short form of the optiondescription
- a multi line description for the option- Returns:
- this CommandLineArguments
- Throws:
OptionDefinitionException
- for duplicate option definitions
-
addParameterOption
public CommandLineArguments addParameterOption(String longOption, boolean paramRequired, String... description) throws OptionDefinitionException
Adds a command line option which expects a parameter.- Parameters:
longOption
- the long form of the optionparamRequired
- true, if the parameter is mandatory, false, if the parameter is optionaldescription
- a multi line description for the option- Returns:
- this CommandLineArguments
- Throws:
OptionDefinitionException
- for duplicate option definitions
-
addParameterOption
public CommandLineArguments addParameterOption(String longOption, char shortOption, boolean paramRequired, String... description) throws OptionDefinitionException
Adds a command line option which expects a parameter.- Parameters:
longOption
- the long form of the optionshortOption
- the short form of the optionparamRequired
- true, if the parameter is mandatory, false, if the parameter is optionaldescription
- a multi line description for the option- Returns:
- this CommandLineArguments
- Throws:
OptionDefinitionException
- for duplicate option definitions
-
addParameterOption
public CommandLineArguments addParameterOption(String longOption, String paramRegExpr, boolean paramRequired, String... description) throws OptionDefinitionException
Adds a command line option which expects a parameter.- Parameters:
longOption
- the long form of the optionparamRegExpr
- a regular expression the parameter must matchparamRequired
- true, if the parameter is mandatory, false, if the parameter is optionaldescription
- a multi line description for the option- Returns:
- this CommandLineArguments
- Throws:
OptionDefinitionException
- for duplicate option definitions
-
addParameterOption
public CommandLineArguments addParameterOption(String longOption, char shortOption, String paramRegExpr, boolean paramRequired, String... description) throws OptionDefinitionException
Adds a command line option which expects a parameter.- Parameters:
longOption
- the long form of the optionshortOption
- the short form of the optionparamRegExpr
- a regular expression the parameter must matchparamRequired
- true, if the parameter is mandatory, false, if the parameter is optionaldescription
- a multi line description for the option- Returns:
- this CommandLineArguments
- Throws:
OptionDefinitionException
- for duplicate option definitions
-
parse
public List<String> parse(String... args) throws InvalidCommandLineParametersException
Parse command line arguments.- Parameters:
args
- command line arguments passed by the main method- Returns:
- an unmodifiable list of parsed command line non option arguments
- Throws:
InvalidCommandLineParametersException
- if invalid command line options or parameters are detected
-
getFormattedDescription
public List<String> getFormattedDescription(boolean withHeader)
Returns a formatted description of the defined options. The list is sorted by long options.- Parameters:
withHeader
- true to output a header- Returns:
- a formatted description of the defined option
-
printFormattedDescription
public void printFormattedDescription(PrintStream printStream, boolean withHeader)
Prints a formatted description of the defined options.- Parameters:
printStream
- the stream to print towithHeader
- true to output a header
-
-