public final class CommandLineOptions
extends java.lang.Object
args of your main() method and configures a Java bean
accordingly.parse(String[], Object)| Modifier and Type | Method and Description |
|---|---|
static int |
applyCommandLineOption(java.lang.String optionName,
java.lang.reflect.Method method,
java.lang.String[] args,
int optionArgumentIndex,
java.lang.Object target)
Parses the command line option's arguments from the args and invokes the method.
|
static java.lang.reflect.Method |
getMethodForOption(java.lang.String option,
java.lang.Class<?> targetClass)
Determines the method of
target.getClass() that is applicable for the given option. |
static java.lang.String[] |
parse(java.lang.String[] args,
java.lang.Object target)
Sets the target's properties from the args.
|
static void |
printResource(java.lang.Class<?> clasS,
java.lang.String name,
java.nio.charset.Charset charset,
java.io.PrintStream ps)
Reads (and decodes) the contents of a resource, replaces all occurrences of
"
${system-property-name}" with the value of the system property, and writes
the result to a PrintStream. |
public static java.lang.String[] parse(java.lang.String[] args,
java.lang.Object target)
All public methods of the target (including those declared by superclasses) are regarded candidates iff
they are annotated with the CommandLineOption annotation.
The possible "names" of the command line option are derived from the name
element of the CommandLineOption annotation, or, if that is missing, from the method name.
When an element of args equals such a name, then the following elements in args are converted to match the parameter types of the method. After that, the method is invoked with the arguments, and the elements are removed from args.
Parsing terminates iff either
"--" is reached (which is consumed)"-" is reached (which is notconsumed)Example:
public
class MyMain {
// ...
@CommandLineOption
public static void
setWidth(double width) {
System.out.println("width=" + width);
}
}
...
final MyMain main = new MyMain();
String[] args = { "--width", "17.5", "a", "b" };
System.out.println(Arrays.toString(args); // Prints "[--width, 17.5, a, b]".
args = MainBean.parseCommandLineOptions(args, main); // Prints "width=17.5".
System.out.println(Arrays.toString(args); // Prints "[a, b]".
java.lang.IllegalArgumentException - An error occurred with parsing; typically a command-line application would
print the message of the exception to STDERR and call "System.exit(1)"java.lang.RuntimeException@Nullable public static java.lang.reflect.Method getMethodForOption(java.lang.String option, java.lang.Class<?> targetClass)
target.getClass() that is applicable for the given option.Null iff there is no applicable methodpublic static int applyCommandLineOption(java.lang.String optionName,
java.lang.reflect.Method method,
java.lang.String[] args,
int optionArgumentIndex,
@Nullable
java.lang.Object target)
Iff the method is a varargs method, then all remaining arguments are converted to an array.
optionArgumentIndex - The position of the first option argument in argsjava.lang.IllegalArgumentException - There are less option arguments available than the method requirespublic static void printResource(java.lang.Class<?> clasS,
java.lang.String name,
@Nullable
java.nio.charset.Charset charset,
java.io.PrintStream ps)
throws java.io.IOException
${system-property-name}" with the value of the system property, and writes
the result to a PrintStream.
The resource is found through the clasS's class loader, and the name "package-name/simple-class-name.name", where all
periods in the package-name have been replaced with slashes.
To ensure that the resource is decoded with the same charset as it was encoded, you should not use the JVM charset (which could be different in the build environment and the runtime environment).
charset - The charset to use for decoding the contents of the resource; null for the
JVM default charsetjava.io.FileNotFoundException - The resource could not be foundjava.io.IOException