public class CommandLineArguments extends Object
This implementation recognizes two types of command-line switches: options, which have arguments, and flags, which do not.
This implementation only recognizes long switches prefixed with "--".
Because MS-DOS batch files substitute a space for the equals sign, this implementation accepts the colon character (':') as a replacement for the equals sign ('=') in options, although the equals sign is preferred when possible.
| Modifier and Type | Class and Description |
|---|---|
static class |
CommandLineArguments.Switch
Common command-line parameters.
|
| Modifier and Type | Field and Description |
|---|---|
static Pattern |
FLAG_PATTERN
The pattern for matching flags.
|
static String |
LONG_SWITCH_DELIMITER
The long delimiter that introduces a switch.
|
static Pattern |
OPTION_PATTERN
The pattern for matching options.
|
static Pattern |
SWITCH_PATTERN
The pattern for matching switches in general.
|
| Modifier | Constructor and Description |
|---|---|
protected |
CommandLineArguments()
This class can only be instantiated if a class is derived from it.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
configureLog(String[] arguments)
Sets default program debugging based upon the presence of the "log" flag and the logging switches.
|
static String |
createSwitch(String switchString)
Deprecated.
|
static <O extends Enum<O> & com.globalmentor.lex.Identifier> |
getOption(String[] arguments,
O option)
Searches the given arguments for the last occurrence of a particular option.
|
static <O extends Enum<O> & com.globalmentor.lex.Identifier,V extends Enum<V> & com.globalmentor.lex.Identifier> |
getOption(String[] arguments,
O option,
Class<V> valueType)
Searches the given arguments for the last occurrence of a particular option.
|
static String |
getOption(String[] arguments,
String optionName)
Searches the given arguments for the last occurrence of a particular option.
|
static <V extends Enum<V> & com.globalmentor.lex.Identifier> |
getOption(String[] arguments,
String optionName,
Class<V> valueType)
Searches the given arguments for the last occurrence of a particular option.
|
static <O extends Enum<O> & com.globalmentor.lex.Identifier> |
getOptions(String[] arguments,
O option)
Returns the given arguments for all occurrences a particular option.
|
static List<String> |
getOptions(String[] arguments,
String optionName)
Returns the given arguments for all occurrences a particular option.
|
static <F extends Enum<F> & com.globalmentor.lex.Identifier> |
hasFlag(String[] arguments,
F flag)
Searches the argument array to see if a particular flag is defined.
|
static boolean |
hasFlag(String[] arguments,
String flagName)
Searches the argument array to see if a particular flag is defined.
|
static boolean |
hasHelpFlag(String[] arguments)
Returns whether the help flag is turned on.
|
static boolean |
isSwitch(String argument)
Checks to see if a particular argument is a switch, without regard to whether it is an option or a flag.
|
public static final String LONG_SWITCH_DELIMITER
public static final Pattern SWITCH_PATTERN
public static final Pattern FLAG_PATTERN
public static final Pattern OPTION_PATTERN
protected CommandLineArguments()
public static void configureLog(String[] arguments)
arguments - The array of command line arguments.CommandLineArguments.Switch.LOG_LEVEL,
CommandLineArguments.Switch.VERBOSE,
CommandLineArguments.Switch.QUIET,
CommandLineArguments.Switch.LOG_FILEpublic static boolean hasHelpFlag(String[] arguments)
arguments - The array of command line arguments.true if the help flag is defined.CommandLineArguments.Switch.HELPpublic static <F extends Enum<F> & com.globalmentor.lex.Identifier> boolean hasFlag(String[] arguments, F flag)
This implementation delegates to #getFlag(String[], String) using the serialization form of the given enum.
F - The type of flag.arguments - The array of command line arguments.flag - The name of the flag which may be defined.true if the flag is defined, else false.Enums.getSerializationName(Enum),
hasFlag(String[], String)public static boolean hasFlag(String[] arguments, String flagName)
arguments - The array of command line arguments.flagName - The name of the flag which may be defined.true if the flag is defined, else false.FLAG_PATTERNpublic static <O extends Enum<O> & com.globalmentor.lex.Identifier,V extends Enum<V> & com.globalmentor.lex.Identifier> V getOption(String[] arguments, O option, Class<V> valueType)
This implementation delegates to getOption(String[], String, Class) using the serialization form of the given enum.
O - The type of option.V - The type of value.arguments - The array of command line arguments.option - The option.valueType - The type of value to expect.null if the option is not defined.IllegalArgumentException - if the given value is not valid for the expected type.Enums.getSerializationName(Enum),
Enums.getSerializedEnum(Class, String),
getOption(String[], String, Class)public static <O extends Enum<O> & com.globalmentor.lex.Identifier> String getOption(String[] arguments, O option)
This implementation delegates to getOption(String[], String) using the serialization form of the given enum.
O - The type of option.arguments - The array of command line arguments.option - The option.null if the option is not defined.Enums.getSerializationName(Enum),
getOption(String[], String)public static <V extends Enum<V> & com.globalmentor.lex.Identifier> V getOption(String[] arguments, String optionName, Class<V> valueType)
This implementation converts the value, if any, to the given value type, interpreting the value as the serialized form of an enum.
V - The type of value.arguments - The array of command line arguments.optionName - The name of the option.valueType - The type of value to expect.null if the option is not defined.IllegalArgumentException - if the given value is not valid for the expected type.OPTION_PATTERN,
Enums.getSerializedEnum(Class, String)public static String getOption(String[] arguments, String optionName)
arguments - The array of command line arguments.optionName - The name of the option.null if the option is not defined.OPTION_PATTERNpublic static <O extends Enum<O> & com.globalmentor.lex.Identifier> List<String> getOptions(String[] arguments, O option)
This implementation delegates to getOptions(String[], String) using the serialization form of the given enum.
arguments - The array of command line arguments.option - The option.null list of arguments of the defined options, if any, in the order encountered.Enums.getSerializationName(Enum),
getOptions(String[], String)public static List<String> getOptions(String[] arguments, String optionName)
arguments - The array of command line arguments.optionName - The name of the option.null list of arguments of the defined options, if any, in the order encountered.OPTION_PATTERNpublic static boolean isSwitch(String argument)
argument - The argument to check.true if the argument is a switch.SWITCH_PATTERN@Deprecated public static String createSwitch(String switchString)
-switchString.switchString - The string to make into a switch.Copyright © 1996–2016 GlobalMentor, Inc.. All rights reserved.