@Retention(value=RUNTIME) @Target(value=FIELD) @Repeatable(value=Args.class) public @interface Arg
yourProgram --tail)
This assumes that you have written a program called yourProgram
and that it will need to do something special if the user also enters the
--tail argument. "argument" is a library that makes handling
this type of command line interaction very easy to implement. All parsers are
equipped with a few basic arguments; --help, -?,
-!, and @(filename).
In the main method in your program, or somewhere similar,
public static void main (final String[] args) {
MyProgram instance = new MyProgram();
and process the command line arguments into the instance variables of your
program.
CmdLine.load(instance, args);Decorate your instance variables with this @Arg annotation to tell the parser what to do. Maybe something like this (to follow on the
--tail example) in the MyProgram class.
@Arg private boolean tail;You can decorate instance variables of many types (including all primitives) and also those of any POJO as long as the POJO is decorated with the @Arg annotation. Arrays and java.util.List of these same types are also valid. For instance, you may want to allow the user of your program to specify multiple files for processing
@Arg private File[] inputFiles;Then the user could enter this on the command line and the
inputFiles instance variable would contain the File objects.
>myProgram --inputFiles oneFile.txt anotherFile.txt
| Modifier and Type | Fields and Description |
|---|---|
static String |
SELF_REFERENCING_ARGNAME |
| Modifier and Type | Optional Element and Description |
|---|---|
boolean |
allowCamelCaps
Indicates that the user can specify the argument on the command-line
using the
Camel Caps version of the long name. |
boolean |
allowMetaphone
Indicates that the user can specify the argument on the command-line with
misspellings as long as it evaluates to the
Metaphone equivalent of the long name. |
boolean |
caseSensitive
Indicates that all values associated with this argument are case
sensitive.
|
String[] |
defaultValues
Indicates the default value(s) for this argument.
|
String[] |
excludeArgs
Indicates the arguments from a pojo that should be excluded from the
parser.
|
String |
factoryArgName
Indicates the argument from an embedded parser that is to be used as a
String parameter to the factory method.
|
String |
factoryMethod
Indicates the factory method to be used when creating an instance of this
embedded parser.
|
String |
format
Indicates the date format for types of Date.
|
String |
help
Indicates the message that will be included in the usage for this
argument.
|
String |
inEnum
Indicates an enum, implying a list of values that will be allowed for
this argument.
|
String[] |
inList
Indicates a list of values that will be allowed for this argument.
|
String |
instanceClass
Indicates the class that will be used to create an object for this
argument.
|
String |
longName
Indicates the single word, long name of this argument.
|
String |
matches
Indicates a verification pattern to be run against the user input before
it will be accepted.
|
int |
multimax
Indicates the maximum allowed number of values that will be accepted for
this argument.
|
int |
multimin
Indicates the minimum allowed number of values that will be accepted for
this argument.
|
boolean |
positional
Indicates that a name is not allowed for this argument.
|
String[] |
range
Indicates a criteria to be applied to the user's input.
|
boolean |
required
Indicates that this argument is required - the user must provide the
argument.
|
char |
shortName
Indicates the single character, short name of this argument.
|
int |
uniqueId
Indicates a non-used integer value that will be kept with the argument in
internal memory.
|
String |
variable
Indicates that a variable in the complex class of the previous @Arg
annotation (without the variable parameter) is to be annotated.
|
public static final String SELF_REFERENCING_ARGNAME
public abstract boolean allowCamelCaps
Camel Caps version of the long name.public abstract boolean allowMetaphone
Metaphone equivalent of the long name.public abstract boolean caseSensitive
public abstract String[] defaultValues
public abstract String[] excludeArgs
public abstract String factoryArgName
"type") for this value.public abstract String factoryMethod
package.class.method. If an inner class is involved then use
package.class$innerClass.methodpublic abstract String format
SimpleDateFormat parameters.public abstract String help
public abstract String inEnum
public abstract String[] inList
public abstract String instanceClass
@Arg(instanceClass = "com.codetaco.argument.VariableTest$MyGroup") private Object testObjectGroup;
public abstract String longName
The default for the long name is the actual name of the variable. In the example below it would not be necessary to specify the long name for the confirm variable since that is the default.
If you prefer not to have a long name at all then set this value to be an empty string.
Examples:
@Arg(longName = 'confirm') private boolean confirm; @Arg(longName = 'file') private File inputFile;With the user then running the program as...
>myProgram --file aFile.txt --confirm
public abstract String matches
Pattern to write the value for this
parameter. This pattern will be applied to the toString()
result on the user input. This typically only makes sense on a type of
String.public abstract int multimax
public abstract int multimin
public abstract boolean positional
public abstract String[] range
public abstract boolean required
@Arg(required=true}. This
will automatically require the user to enter --confirm or
the parser will throw an exception.public abstract char shortName
For a boolean type of argument there would be no supplied value after the
-c. For any other type of instance variable the value would
follow.
Examples:
@Arg(shortName = 'c') private boolean confirm; @Arg(shortName = 'f') private File inputFile;With the user then running the program as...
>myProgram -f aFile.txt -c
public abstract int uniqueId
public abstract String variable