Index
Introduction
Adding more than few command line parameters to your application can be pain, and using an application without proper parameter help can be even worse.With this library handling command line parameters should be made easy and robust for the developer while simple to use for the user.
The idea is to have an simple class (POJO), to store the command line parameters. The parameters will be automatically converted to requested values, and the user will be notified if a parameter is missing a value, or the value is invalid.
Some types are handled automatically (int, float, double, string, file) and an extension mechanism is provided to support even more by defining ParamFormat annotation for the field.
Defining the class that holds the values
Create a class (inner class can also be used. HINT: add a static modifier so it can be instantiated in static context). public class Cmd{
}
add a public parameter with proper annotation.
public class Cmd{
@Param(shortHelp="prefix all lines (including blank ones)")
public boolean all;
@Param(shortHelp="max lines prefixed")
public int max;
}
Default values
You can define default values in your class (parser will not change fields for wich a command line parameter was not entered) public class Cmd{
@Param(shortHelp="prefix all lines (including blank ones)")
public boolean all = true;
@Param(shortHelp="prefix to be printed")
public String prefix = "LINE: ";
}
Initialize your app
add the following for a simple default usage of the library. Cmd cmd = new Cmd();
ParamParser parser = new ParamParser();
parser.parseDefault(args, cmd);
The command line
A boolean type is interpreted as switch (".all" is set by adding "-a" or "--all" parameter to command line)Other types require additional parameter (".max" is set by adding "-m" "100" or "--max" "100")
You can annotate getter or setter (isAll(), setAll()), but I prefer simple fields here (less code)
In case of multiple occurences of the same property, the last value is taken (except for Array)