Package org.kohsuke.args4j
Core classes of the Args4J command line parser.
What is Args4J?
Args4J is a command line parser.
As such, its job is to parse the String-array passed to the main()
method and
transfer the values to a Java object, which includes type conversion.
The entry point into this parsing is the CmdLineParser
class,
with its parse()
method.
Args4J must know the mapping between the flag from the command line and the target for the value. There are many ways to use Args4J:
- place an
@Option
or@Argument
annotation at a setter or on a field - provide a XML configuration file
- make all fields publicly available
Depending on what you want, you may have perform a configuration step before parsing.
Examples
java Main -text newText
The typical use involves writing a small bean class and providing the annotations.
This feature is available since the first Args4J release:
public class Bean { @Option(name="-text") String text; } public class Main { public static void main(String[] args) throws CmdLineException { Object bean = new Bean(); CmdLineParser parser = new CmdLineParser(bean); parser.parse(args); } }
An easy way to initialize fields without touching the bean source code is to use the FieldParser
.
The FieldParser
scans all fields of bean class (including inheritance), and makes them publicly available as options with a -
prefix in the name.
This feature is available since Args4J release 2.0.16:
public class Bean {
String text;
}
public class Main {
public static void main(String[] args) throws CmdLineException {
Object bean = new Bean();
CmdLineParser parser = new CmdLineParser(null);
new FieldParser().parse(parser, bean);
parser.parse(args);
}
}
While the FieldParser
is easier to use, the XmlParser
supports more features.
That said, it supports all features which are available via annotations: usage text, specifying handlers, and more. You have to provide an XML InputSource
or a URL to the XML file.
This feature is available since Args4J release 2.0.16:
public class Bean {
String text;
}
public class Main {
public static void main(String[] args) throws CmdLineException {
Object bean = new Bean();
CmdLineParser parser = new CmdLineParser(null);
new XmlParser().parse(getClass().getResource(bean.getClass().getName() + ".xml"), parser, bean);
parser.parse(args);
}
}
<args>
<option field="text" name="-text" usage="Output text"/>
</args>
-
ClassDescriptionArgument of the command line.Parser for analyzing Args4J annotations in the class hierarchy.Signals an error in the user input.Command line argument owner.Metadataconfiguration.Deprecated.This metadata parser makes all field available to the CmdLineParser.Signals an incorrect use of args4j annotations.A message that can be formatted with arguments and locale.Immutable run-time copy of
Option
annotation.Marks a field/setter that receives a command line switch value.Run-time copy of the Option or Argument annotation.SelectsOptionHandler
.Manages the registration of option handlers.Provide custom logic for creatingOptionHandler
implementation.Set of properties that controlsCmdLineParser
behaviours.Starter class which uses reflection to instantiate the business class, parse the command line parameters, sets the fields of the business class and doing the help message handling.Parses an XML-file specifying the 'annotations'.
OptionHandlerFilter