public class CliUtil extends Object
public static void main(final String[] args) throws Exception { Program program = new Program(); // initialize the Checkable class with the @Option information CliUtil.execute(program, args); // register Unit converters, parse the command line, catch --help, --version and error // do rest of what the main method should do }When the program is Checkable, the
check()
method is called after the arguments have been parsed. Here, further
checks on the arguments (i.e., range checks) can be carried out. Potentially, check() can also provide other initialization
of the program to be executed, but this can better be provided by other methods in main() . Make sure that expensive
initialization is not carried out in the constructor of the program class that is given to the execute method.
Alternatively, move the command line options to a separate class, e.g. called Options and initialize that class rather than
the real program class. The real program can then take the values of the program from the Options class. An example:
public class Program { @Command(description = "Test program for CLI", name = "Program", mixinStandardHelpOptions = true, version = "1.0") public static class Options implements Checkable { @Option(names = {"-p", "--port"}, description = "Internet port to use", defaultValue = "80") private int port; public int getPort() { return this.port; } @Override public void check() throws Exception { if (this.port <= 0 || this.port > 65535) throw new Exception("Port should be between 1 and 65535"); } } public Program() { // initialization for the program; avoid really starting things } public static void main(final String[] args) { Options options = new Options(); CliUtil.execute(options, args); System.out.println("port = " + options.getPort()); // you can now call methods on the program, e.g. for real initialization using the CLI parameters in options } }
Constructor and Description |
---|
CliUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
changeCommandDescription(Class<?> programClass,
String newDescription)
Change the value of the 'description' property of an already present @Command annotation in a class or superclass of
that class.
|
static void |
changeCommandDescription(Object program,
String newDescription)
Change the value of the 'description' property of an already present @Command annotation in a class or superclass of
that class.
|
static void |
changeCommandName(Class<?> programClass,
String newName)
Change the value of the 'name' property of an already present @Command annotation in a class or superclass of that
class.
|
static void |
changeCommandName(Object program,
String newName)
Change the value of the 'name' property of an already present @Command annotation in a class or superclass of that
class.
|
static void |
changeCommandProperty(Class<?> programClass,
String propertyName,
Object newValue)
Change the value of a property of an already present @Command annotation in a class or superclass of that class.
|
static void |
changeCommandProperty(Object program,
String propertyName,
Object newValue)
Change the value of a property of an already present @Command annotation in a class or superclass of that class.
|
static void |
changeCommandVersion(Class<?> programClass,
String newVersion)
Change the value of the 'version' property of an already present @Command annotation in a class or superclass of that
class.
|
static void |
changeCommandVersion(Object program,
String newVersion)
Change the value of the 'version' property of an already present @Command annotation in a class or superclass of that
class.
|
static void |
changeOptionDefault(Class<?> programClass,
String fieldName,
String newDefaultValue)
Change the default value of an already present @Option annotation of the "defaultValue" field in a class or
superclass.
|
static void |
changeOptionDefault(Object program,
String fieldName,
String newDefaultValue)
Change the default value of an already present @Option annotation of the "defaultValue" field in a class or
superclass.
|
static void |
changeOptionProperty(Class<?> programClass,
String fieldName,
String propertyName,
Object newValue)
Change the value of a property of an already present @Option annotation of a field in a class or superclass.
|
static void |
changeOptionProperty(Object program,
String fieldName,
String propertyName,
Object newValue)
Change the value of a property of an already present @Option annotation of a field in a class or superclass.
|
static void |
execute(CommandLine commandLine,
String[] args)
Parse the given CommandLine object, that has been generated for a program.
|
static void |
execute(Object program,
String[] args)
Parse the command line for the program.
|
public static void execute(Object program, String[] args)
program
- Object; the potentially checkable program with the @Option informationargs
- String[]; the arguments from the command linepublic static void execute(CommandLine commandLine, String[] args)
commandLine
- CommandLine; the CommandLine object for the program with the @Option informationargs
- String[]; the arguments from the command linepublic static void changeOptionProperty(Class<?> programClass, String fieldName, String propertyName, Object newValue) throws CliException, NoSuchFieldException, IllegalStateException, IllegalArgumentException
programClass
- Class<?>; the class of the program for which the options should be changedfieldName
- String; the field for which the defaultValue in @Option should be changedpropertyName
- String; the name of the property to change the value ofnewValue
- String; the new value of the propertyCliException
- when the field cannot be found, or when the @Option annotation is not present in the fieldNoSuchFieldException
- when the field with the name does not exist in the program objectIllegalStateException
- when the annotation has no member values or access to the member values is deniedIllegalArgumentException
- when the value that is changed is of a different type than the type of the newValuepublic static void changeOptionProperty(Object program, String fieldName, String propertyName, Object newValue) throws CliException, NoSuchFieldException, IllegalStateException, IllegalArgumentException
program
- Object; the program for which the options should be changedfieldName
- String; the field for which the defaultValue in @Option should be changedpropertyName
- String; the name of the property to change the value ofnewValue
- String; the new value of the propertyCliException
- when the field cannot be found, or when the @Option annotation is not present in the fieldNoSuchFieldException
- when the field with the name does not exist in the program objectIllegalStateException
- when the annotation has no member values or access to the member values is deniedIllegalArgumentException
- when the value that is changed is of a different type than the type of the newValuepublic static void changeOptionDefault(Object program, String fieldName, String newDefaultValue) throws CliException, NoSuchFieldException, IllegalStateException, IllegalArgumentException
program
- Object; the program for which the options should be changedfieldName
- String; the field for which the defaultValue in @Option should be changednewDefaultValue
- Object; the new value of the defaultValueCliException
- when the field cannot be found, or when the @Option annotation is not present in the fieldNoSuchFieldException
- when the field with the name does not exist in the program objectIllegalStateException
- when the annotation has no member values or access to the member values is deniedIllegalArgumentException
- when the value that is changed is of a different type than the type of the newValuepublic static void changeOptionDefault(Class<?> programClass, String fieldName, String newDefaultValue) throws CliException, NoSuchFieldException, IllegalStateException, IllegalArgumentException
programClass
- Class<?>; the class of the program for which the options should be changedfieldName
- String; the field for which the defaultValue in @Option should be changednewDefaultValue
- Object; the new value of the defaultValueCliException
- when the field cannot be found, or when the @Option annotation is not present in the fieldNoSuchFieldException
- when the field with the name does not exist in the program objectIllegalStateException
- when the annotation has no member values or access to the member values is deniedIllegalArgumentException
- when the value that is changed is of a different type than the type of the newValuepublic static void changeCommandProperty(Object program, String propertyName, Object newValue) throws CliException, NoSuchFieldException, IllegalStateException, IllegalArgumentException
program
- Object; the program for which the cli property should be changedpropertyName
- String; the name of the property to change the value ofnewValue
- Object; the new value of the propertyCliException
- when the field cannot be found, or when the @Option annotation is not present in the fieldNoSuchFieldException
- when the field with the name does not exist in the program objectIllegalStateException
- when the annotation has no member values or access to the member values is deniedIllegalArgumentException
- when the value that is changed is of a different type than the type of the newValuepublic static void changeCommandProperty(Class<?> programClass, String propertyName, Object newValue) throws CliException, NoSuchFieldException, IllegalStateException, IllegalArgumentException
programClass
- Class<?>; the class of the program for which the options should be changedpropertyName
- String; the name of the property to change the value ofnewValue
- Object; the new value of the propertyCliException
- when the field cannot be found, or when the @Option annotation is not present in the fieldNoSuchFieldException
- when the field with the name does not exist in the program objectIllegalStateException
- when the annotation has no member values or access to the member values is deniedIllegalArgumentException
- when the value that is changed is of a different type than the type of the newValuepublic static void changeCommandName(Object program, String newName) throws CliException, NoSuchFieldException, IllegalStateException, IllegalArgumentException
program
- Object; the program for which the cli property should be changednewName
- String; the new value of the nameCliException
- when the field cannot be found, or when the @Option annotation is not present in the fieldNoSuchFieldException
- when the field with the name does not exist in the program objectIllegalStateException
- when the annotation has no member values or access to the member values is deniedIllegalArgumentException
- when the value that is changed is of a different type than the type of the newValuepublic static void changeCommandName(Class<?> programClass, String newName) throws CliException, NoSuchFieldException, IllegalStateException, IllegalArgumentException
programClass
- Class<?>; the class of the program for which the options should be changednewName
- String; the new value of the nameCliException
- when the field cannot be found, or when the @Option annotation is not present in the fieldNoSuchFieldException
- when the field with the name does not exist in the program objectIllegalStateException
- when the annotation has no member values or access to the member values is deniedIllegalArgumentException
- when the value that is changed is of a different type than the type of the newValuepublic static void changeCommandDescription(Object program, String newDescription) throws CliException, NoSuchFieldException, IllegalStateException, IllegalArgumentException
program
- Object; the program for which the cli property should be changednewDescription
- String; the new value of the descriptionCliException
- when the field cannot be found, or when the @Option annotation is not present in the fieldNoSuchFieldException
- when the field with the name does not exist in the program objectIllegalStateException
- when the annotation has no member values or access to the member values is deniedIllegalArgumentException
- when the value that is changed is of a different type than the type of the newValuepublic static void changeCommandDescription(Class<?> programClass, String newDescription) throws CliException, NoSuchFieldException, IllegalStateException, IllegalArgumentException
programClass
- Class<?>; the class of the program for which the options should be changednewDescription
- String; the new value of the descriptionCliException
- when the field cannot be found, or when the @Option annotation is not present in the fieldNoSuchFieldException
- when the field with the name does not exist in the program objectIllegalStateException
- when the annotation has no member values or access to the member values is deniedIllegalArgumentException
- when the value that is changed is of a different type than the type of the newValuepublic static void changeCommandVersion(Object program, String newVersion) throws CliException, NoSuchFieldException, IllegalStateException, IllegalArgumentException
program
- Object; the program for which the cli property should be changednewVersion
- String; the new value of the versionCliException
- when the field cannot be found, or when the @Option annotation is not present in the fieldNoSuchFieldException
- when the field with the name does not exist in the program objectIllegalStateException
- when the annotation has no member values or access to the member values is deniedIllegalArgumentException
- when the value that is changed is of a different type than the type of the newValuepublic static void changeCommandVersion(Class<?> programClass, String newVersion) throws CliException, NoSuchFieldException, IllegalStateException, IllegalArgumentException
programClass
- Class<?>; the class of the program for which the options should be changednewVersion
- String; the new value of the versionCliException
- when the field cannot be found, or when the @Option annotation is not present in the fieldNoSuchFieldException
- when the field with the name does not exist in the program objectIllegalStateException
- when the annotation has no member values or access to the member values is deniedIllegalArgumentException
- when the value that is changed is of a different type than the type of the newValueCopyright © 2018–2019 Delft University of Technology. All rights reserved.