Interface ArgumentParser
-
- All Known Subinterfaces:
Subparser
- All Known Implementing Classes:
ArgumentParserImpl,SubparserImpl
public interface ArgumentParserThis interface defines behavior of ArgumentParser.
The typical usage is set description using
description(String)and add arguments usingaddArgument(String...). To add sub-command, first calladdSubparsers()to obtainSubparsersobject.Subparsersobject provides necessary methods to add sub-commands. To make a conceptual group of arguments, first calladdArgumentGroup(String)to createArgumentGroupobject. And add argument to that group usingArgumentGroup.addArgument(String...). Similarly, to add the mutually exclusive group of arguments, useaddMutuallyExclusiveGroup(String)to createMutuallyExclusiveGroupobject. To parse command-line arguments, callparseArgs(String[])or several overloaded methods.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description ArgumentaddArgument(java.lang.String... nameOrFlags)Creates newArgumentobject and adds to this parser and returns the object.ArgumentGroupaddArgumentGroup(java.lang.String title)Creates newArgumentGroupobject and adds to this parser and returns the object.MutuallyExclusiveGroupaddMutuallyExclusiveGroup()Creates new mutually exclusive group,MutuallyExclusiveGroupobject, without title and adds to this parser and returns the object.MutuallyExclusiveGroupaddMutuallyExclusiveGroup(java.lang.String title)Creates new mutually exclusive group,MutuallyExclusiveGroupobject, and adds to this parser and returns the object.SubparsersaddSubparsers()ReturnsSubparsers.ArgumentParserdefaultHelp(boolean defaultHelp)If defaultHelp istrue, the default values of arguments are printed in help message.ArgumentParserdescription(java.lang.String description)Sets the text to display before the argument help.ArgumentParserepilog(java.lang.String epilog)Sets the text to display after the argument help.java.lang.StringformatHelp()Returns help message.java.lang.StringformatUsage()Returns a brief description of how the program should be invoked on the command line.java.lang.StringformatVersion()Returns version string.java.lang.ObjectgetDefault(java.lang.String dest)Returns default value of givendest.voidhandleError(ArgumentParserException e)Prints usage and error message.NamespaceparseArgs(java.lang.String[] args)Parses command line arguments.voidparseArgs(java.lang.String[] args, java.lang.Object userData)Parses command line arguments.voidparseArgs(java.lang.String[] args, java.util.Map<java.lang.String,java.lang.Object> attrs)Parses command line arguments.voidparseArgs(java.lang.String[] args, java.util.Map<java.lang.String,java.lang.Object> attrs, java.lang.Object userData)Parses command line arguments.NamespaceparseArgsOrFail(java.lang.String[] args)Parses command line arguments, handling any errors.voidprintHelp()Prints help message in stdout.voidprintHelp(java.io.PrintWriter writer)Prints help message in writer.voidprintUsage()Print a brief description of how the program should be invoked on the command line in stdout.voidprintUsage(java.io.PrintWriter writer)Print a brief description of how the program should be invoked on the command line in writer.voidprintVersion()Prints version string in stdout.voidprintVersion(java.io.PrintWriter writer)Prints version string in writer.ArgumentParsersetDefault(java.lang.String dest, java.lang.Object value)Sets parser-level default value of attributedest.ArgumentParsersetDefaults(java.util.Map<java.lang.String,java.lang.Object> attrs)Sets parser-level default values fromattrs.ArgumentParserusage(java.lang.String usage)Sets the text to display as usage line.ArgumentParserversion(java.lang.String version)Sets version string.
-
-
-
Method Detail
-
addArgument
Argument addArgument(java.lang.String... nameOrFlags)
Creates new
Argumentobject and adds to this parser and returns the object.The
nameOrFlagsis either a single name of positional argument or a list of option strings for optional argument, e.g.fooor-f, --foo.
-
addArgumentGroup
ArgumentGroup addArgumentGroup(java.lang.String title)
Creates new
ArgumentGroupobject and adds to this parser and returns the object.The
titleis printed in help message as a title of this group.ArgumentGroupprovides a way to conceptually group up command line arguments.- Parameters:
title- The title printed in help message.- Returns:
ArgumentGroupobject.
-
addMutuallyExclusiveGroup
MutuallyExclusiveGroup addMutuallyExclusiveGroup()
Creates new mutually exclusive group,
MutuallyExclusiveGroupobject, without title and adds to this parser and returns the object.- Returns:
MutuallyExclusiveGroupobject.
-
addMutuallyExclusiveGroup
MutuallyExclusiveGroup addMutuallyExclusiveGroup(java.lang.String title)
Creates new mutually exclusive group,
MutuallyExclusiveGroupobject, and adds to this parser and returns the object.The arguments added to this group are mutually exclusive; if more than one argument belong to the group are specified, an error will be reported. The
titleis printed in help message as a title of this group.- Parameters:
title- The title printed in help message.- Returns:
- The
MutuallyExclusiveGroupobject.
-
addSubparsers
Subparsers addSubparsers()
Returns
Subparsers.The method name is rather controversial because repeated call of this method does not add new
Subparsersobject. Instead, this method always returns sameSubparsersobject.Subparsersobject provides a way to add sub-commands.- Returns:
Subparsersobject.
-
usage
ArgumentParser usage(java.lang.String usage)
Sets the text to display as usage line. By default, the usage line is calculated from the arguments this object contains.
If the given usage contains ${prog} string, it will be replaced with the program name given in
ArgumentParsers.newArgumentParser(String).- Parameters:
usage- usage text- Returns:
- this
-
description
ArgumentParser description(java.lang.String description)
Sets the text to display before the argument help.- Parameters:
description- The text to display before the argument help.- Returns:
- this
-
epilog
ArgumentParser epilog(java.lang.String epilog)
Sets the text to display after the argument help.- Parameters:
epilog- The text to display after the argument help.- Returns:
- this
-
version
ArgumentParser version(java.lang.String version)
Sets version string. It will be displayed
printVersion().If the given usage contains ${prog} string, it will be replaced with the program name given in
ArgumentParsers.newArgumentParser(String). This processed text will be printed without text-wrapping.- Parameters:
version- The version string.- Returns:
- this
-
defaultHelp
ArgumentParser defaultHelp(boolean defaultHelp)
If defaultHelp is
true, the default values of arguments are printed in help message.By default, the default values are not printed in help message.
- Parameters:
defaultHelp- Switch to display the default value in help message.- Returns:
- this
-
printHelp
void printHelp()
Prints help message in stdout.
-
printHelp
void printHelp(java.io.PrintWriter writer)
Prints help message in writer.- Parameters:
writer- Writer to print message.
-
formatHelp
java.lang.String formatHelp()
Returns help message.- Returns:
- The help message.
-
printUsage
void printUsage()
Print a brief description of how the program should be invoked on the command line in stdout.
-
printUsage
void printUsage(java.io.PrintWriter writer)
Print a brief description of how the program should be invoked on the command line in writer.- Parameters:
writer- Writer to print message.
-
formatUsage
java.lang.String formatUsage()
Returns a brief description of how the program should be invoked on the command line.- Returns:
- Usage text.
-
printVersion
void printVersion()
Prints version string in stdout.
-
printVersion
void printVersion(java.io.PrintWriter writer)
Prints version string in writer.- Parameters:
writer- Writer to print version string.
-
formatVersion
java.lang.String formatVersion()
Returns version string.- Returns:
- The version string.
-
setDefault
ArgumentParser setDefault(java.lang.String dest, java.lang.Object value)
Sets parser-level default value of attribute
dest.The parser-level defaults always override argument-level defaults.
- Parameters:
dest- The attribute name.value- The default value.- Returns:
- this
-
setDefaults
ArgumentParser setDefaults(java.util.Map<java.lang.String,java.lang.Object> attrs)
Sets parser-level default values from
attrs.All key-value pair in
attrsare registered to parser-level defaults. The parser-level defaults always override argument-level defaults.- Parameters:
attrs- The parser-level default values to add.- Returns:
- this
-
getDefault
java.lang.Object getDefault(java.lang.String dest)
Returns default value of given
dest.Returns default value set by
Argument.setDefault(Object),setDefault(String, Object)orsetDefaults(Map). Please note that while parser-level defaults always override argument-level defaults while parsing, this method examines argument-level defaults first. If no default value is found, then check parser-level defaults. If no default value is found, returnsnull.- Parameters:
dest- The attribute name of default value to get.- Returns:
- The default value of given dest.
-
parseArgsOrFail
Namespace parseArgsOrFail(java.lang.String[] args)
Parses command line arguments, handling any errors.
This is a shortcut method that combines
parseArgs(java.lang.String[])andhandleError(net.sourceforge.argparse4j.inf.ArgumentParserException). If the arguments can be successfully parsed, the resulted attributes are returned as aNamespaceobject. Otherwise, the program exits with a1return code.- Parameters:
args- Command line arguments.- Returns:
Namespaceobject.
-
parseArgs
Namespace parseArgs(java.lang.String[] args) throws ArgumentParserException
Parses command line arguments.
The resulted attributes are returned as
Namespaceobject. This method must not alter the status of this parser and can be called multiple times.- Parameters:
args- Command line arguments.- Returns:
Namespaceobject.- Throws:
ArgumentParserException- If an error occurred.
-
parseArgs
void parseArgs(java.lang.String[] args, java.util.Map<java.lang.String,java.lang.Object> attrs) throws ArgumentParserExceptionParses command line arguments.
Unlike
parseArgs(String[]), which returnsNamespaceobject, this method stores attributes in givenattrs.- Parameters:
args- Command line arguments.attrs- Map object to store attributes.- Throws:
ArgumentParserException- If an error occurred.
-
parseArgs
void parseArgs(java.lang.String[] args, java.lang.Object userData) throws ArgumentParserExceptionParses command line arguments.
Unlike
parseArgs(String[]), which returnsNamespaceobject, this method stores attributes in givenuserData. The location to store value is designated usingArgannotations. User don't have to specifyArgfor all attributes: the missing attributes are just skipped. This method performs simpleListto generic array conversion. For example, user can assignList<Integer>attribute to generic arrayint[].- Parameters:
args- Command line arguments.userData- Object to store attributes.- Throws:
ArgumentParserException- If an error occurred.
-
parseArgs
void parseArgs(java.lang.String[] args, java.util.Map<java.lang.String,java.lang.Object> attrs, java.lang.Object userData) throws ArgumentParserExceptionParses command line arguments.
This is a combination of
parseArgs(String[], Map)andparseArgs(String[], Object). The all attributes will be stored inattrs. The attributes specified inArgannotations will be also stored inuserData.- Parameters:
args- Command line arguments.attrs- Map to store attributes.userData- Object to store attributes.- Throws:
ArgumentParserException- If an error occurred.
-
handleError
void handleError(ArgumentParserException e)
Prints usage and error message.
Please note that this method does not terminate the program.
- Parameters:
e- Error thrown byparseArgs(String[]).
-
-