public final class ArgumentParserImpl extends Object implements ArgumentParser
| Modifier and Type | Class and Description |
|---|---|
static class |
ArgumentParserImpl.Candidate |
| Constructor and Description |
|---|
ArgumentParserImpl(ArgumentParserConfigurationImpl config) |
ArgumentParserImpl(ArgumentParserConfigurationImpl config,
String command,
ArgumentParserImpl mainParser) |
| Modifier and Type | Method and Description |
|---|---|
ArgumentImpl |
addArgument(ArgumentGroupImpl group,
String... nameOrFlags) |
ArgumentImpl |
addArgument(String... nameOrFlags)
Creates new
Argument, adds it to this container and returns it. |
ArgumentGroup |
addArgumentGroup(String title)
Creates new
ArgumentGroup object and adds to this parser and
returns the object. |
MutuallyExclusiveGroup |
addMutuallyExclusiveGroup()
Creates new mutually exclusive group,
MutuallyExclusiveGroup
object, without title and adds to this parser and returns the object. |
MutuallyExclusiveGroup |
addMutuallyExclusiveGroup(String title)
Creates new mutually exclusive group,
MutuallyExclusiveGroup
object, and adds to this parser and returns the object. |
SubparsersImpl |
addSubparsers()
Returns
Subparsers. |
ArgumentParserImpl |
defaultHelp(boolean defaultHelp)
If defaultHelp is
true, the default values of arguments are
printed in help message. |
ArgumentParserImpl |
description(String description)
Set text to display before the argument help.
|
ArgumentParserImpl |
epilog(String epilog)
Sets the text to display after the argument help.
|
String |
formatHelp()
Returns help message.
|
String |
formatUsage()
Returns a brief description of how the program should be invoked on the
command line.
|
String |
formatVersion()
Returns version string.
|
String |
getCommand() |
ArgumentParserConfigurationImpl |
getConfig()
Get the configuration of this argument parser.
|
Object |
getDefault(String dest)
Returns default value set by
ArgumentImpl.setDefault(Object) or
setDefault(String, Object). |
String |
getFromFilePrefixChars() |
String |
getPrefixChars() |
String |
getProg() |
void |
handleError(ArgumentParserException e)
Prints usage and error message.
|
void |
handleError(ArgumentParserException e,
PrintWriter writer)
Prints usage and error message to the given writer.
|
void |
parseArgs(net.sourceforge.argparse4j.internal.ParseState state,
Map<String,Object> attrs) |
Namespace |
parseArgs(String[] args)
Parses command line arguments.
|
void |
parseArgs(String[] args,
Map<String,Object> attrs)
Parses command line arguments.
|
void |
parseArgs(String[] args,
Map<String,Object> attrs,
Object userData)
Parses command line arguments.
|
void |
parseArgs(String[] args,
Object userData)
Parses command line arguments.
|
Namespace |
parseArgsOrFail(String[] args)
Parses command line arguments, handling any errors.
|
Namespace |
parseKnownArgs(String[] args,
List<String> unknown)
Just like
ArgumentParser.parseArgs(String[]), but parses only known arguments
without throwing exception for unrecognized arguments. |
void |
parseKnownArgs(String[] args,
List<String> unknown,
Map<String,Object> attrs)
Just like
ArgumentParser.parseArgs(String[], Map), but parses only known
arguments without throwing exception for unrecognized arguments. |
void |
parseKnownArgs(String[] args,
List<String> unknown,
Map<String,Object> attrs,
Object userData)
Just like
ArgumentParser.parseArgs(String[], Map, Object), but parses only
known arguments without throwing exception for unrecognized arguments. |
void |
parseKnownArgs(String[] args,
List<String> unknown,
Object userData)
Just like
ArgumentParser.parseArgs(String[], Object), but parses only known
arguments without throwing exception for unrecognized arguments. |
Namespace |
parseKnownArgsOrFail(String[] args,
List<String> unknown)
Just like
ArgumentParser.parseArgsOrFail(String[]), but parses only known
arguments without throwing exception for unrecognized arguments. |
void |
printHelp()
Prints help message in stdout.
|
void |
printHelp(PrintWriter writer)
Prints help message in writer.
|
void |
printUsage()
Print a brief description of how the program should be invoked on the
command line in stdout.
|
void |
printUsage(PrintWriter writer)
Print a brief description of how the program should be invoked on the
command line in writer.
|
void |
printVersion()
Prints version string in stdout.
|
void |
printVersion(PrintWriter writer)
Prints version string in writer.
|
ArgumentParserImpl |
setDefault(String dest,
Object value)
Sets parser-level default value of attribute
dest. |
ArgumentParserImpl |
setDefaults(Map<String,Object> attrs)
Sets parser-level default values from
attrs. |
ArgumentParserImpl |
usage(String usage)
Sets the text to display as usage line.
|
ArgumentParserImpl |
version(String version)
Sets version string.
|
public ArgumentParserImpl(ArgumentParserConfigurationImpl config)
public ArgumentParserImpl(ArgumentParserConfigurationImpl config, String command, ArgumentParserImpl mainParser)
public ArgumentImpl addArgument(String... nameOrFlags)
ArgumentContainer
Creates new Argument, adds it to this container and returns it.
The nameOrFlags is either a single name of positional argument or
a list of option strings for named argument, e.g. foo or
-f, --foo.
addArgument in interface ArgumentContainernameOrFlags - A name or a list of option strings of new Argument.Argument object.public ArgumentImpl addArgument(ArgumentGroupImpl group, String... nameOrFlags)
public SubparsersImpl addSubparsers()
ArgumentParser
Returns Subparsers.
The method name is rather controversial because repeated call of this
method does not add new Subparsers object. Instead, this method
always returns same Subparsers object. Subparsers object
provides a way to add sub-commands.
addSubparsers in interface ArgumentParserSubparsers object.public ArgumentGroup addArgumentGroup(String title)
ArgumentParser
Creates new ArgumentGroup object and adds to this parser and
returns the object.
The title is printed in help message as a title of this group.
ArgumentGroup provides a way to conceptually group up command
line arguments.
addArgumentGroup in interface ArgumentParsertitle - The title printed in help message.ArgumentGroup object.public MutuallyExclusiveGroup addMutuallyExclusiveGroup()
ArgumentParser
Creates new mutually exclusive group, MutuallyExclusiveGroup
object, without title and adds to this parser and returns the object.
addMutuallyExclusiveGroup in interface ArgumentParserMutuallyExclusiveGroup object.public MutuallyExclusiveGroup addMutuallyExclusiveGroup(String title)
ArgumentParser
Creates new mutually exclusive group, MutuallyExclusiveGroup
object, 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 title is printed in help message as a title of this
group.
addMutuallyExclusiveGroup in interface ArgumentParsertitle - The title printed in help message.MutuallyExclusiveGroup object.public ArgumentParserImpl usage(String usage)
ArgumentParserSets 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).
usage in interface ArgumentParserusage - usage textpublic ArgumentParserImpl description(String description)
description in interface ArgumentContainerdescription in interface ArgumentParserdescription - text to display before the argument helppublic ArgumentParserImpl epilog(String epilog)
ArgumentParserepilog in interface ArgumentParserepilog - The text to display after the argument help.public ArgumentParserImpl version(String version)
ArgumentParser
Sets version string. It will be displayed ArgumentParser.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.
version in interface ArgumentParserversion - The version string.public ArgumentParserImpl defaultHelp(boolean defaultHelp)
ArgumentParser
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.
defaultHelp in interface ArgumentParserdefaultHelp - Switch to display the default value in help message.public void printHelp()
ArgumentParserprintHelp in interface ArgumentParserpublic void printHelp(PrintWriter writer)
ArgumentParserprintHelp in interface ArgumentParserwriter - Writer to print message.public String formatHelp()
ArgumentParserformatHelp in interface ArgumentParserpublic void printUsage()
ArgumentParserprintUsage in interface ArgumentParserpublic void printUsage(PrintWriter writer)
ArgumentParserprintUsage in interface ArgumentParserwriter - Writer to print message.public String formatUsage()
ArgumentParserformatUsage in interface ArgumentParserpublic ArgumentParserImpl setDefault(String dest, Object value)
ArgumentParser
Sets parser-level default value of attribute dest.
The parser-level defaults always override argument-level defaults.
setDefault in interface ArgumentParserdest - The attribute name.value - The default value.public ArgumentParserImpl setDefaults(Map<String,Object> attrs)
ArgumentParser
Sets parser-level default values from attrs.
All key-value pair in attrs are registered to parser-level
defaults. The parser-level defaults always override argument-level
defaults.
setDefaults in interface ArgumentParserattrs - The parser-level default values to add.public Object getDefault(String dest)
ArgumentImpl.setDefault(Object) or
setDefault(String, Object). 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.getDefault in interface ArgumentParserdest - attribute name of default value to get.public Namespace parseArgsOrFail(String[] args)
ArgumentParserParses command line arguments, handling any errors.
This is a shortcut method that combines ArgumentParser.parseArgs(java.lang.String[]) and
ArgumentParser.handleError(net.sourceforge.argparse4j.inf.ArgumentParserException). If the arguments can be successfully parsed, the
resulted attributes are returned as a Namespace object.
Otherwise, the program exits with a 1 return code.
parseArgsOrFail in interface ArgumentParserargs - Command line arguments.Namespace object.public Namespace parseKnownArgsOrFail(String[] args, List<String> unknown)
ArgumentParser
Just like ArgumentParser.parseArgsOrFail(String[]), but parses only known
arguments without throwing exception for unrecognized arguments. If
unknown is not null, unrecognized arguments will be stored in it.
parseKnownArgsOrFail in interface ArgumentParserargs - Command line arguments.unknown - Output variable to store unrecognized arguments, or nullNamespace object.public Namespace parseArgs(String[] args) throws ArgumentParserException
ArgumentParserParses command line arguments.
The resulted attributes are returned as Namespace object. This
method must not alter the status of this parser and can be called
multiple times.
parseArgs in interface ArgumentParserargs - Command line arguments.Namespace object.ArgumentParserException - If an error occurred.public Namespace parseKnownArgs(String[] args, List<String> unknown) throws ArgumentParserException
ArgumentParser
Just like ArgumentParser.parseArgs(String[]), but parses only known arguments
without throwing exception for unrecognized arguments. If unknown
is not null, unrecognized arguments will be stored in it.
parseKnownArgs in interface ArgumentParserargs - Command line arguments.unknown - Output variable to store unrecognized arguments, or nullNamespace object.ArgumentParserException - If an error occurred.public void parseArgs(String[] args, Map<String,Object> attrs) throws ArgumentParserException
ArgumentParserParses command line arguments.
Unlike ArgumentParser.parseArgs(String[]), which returns Namespace
object, this method stores attributes in given attrs.
parseArgs in interface ArgumentParserargs - Command line arguments.attrs - Map object to store attributes.ArgumentParserException - If an error occurred.public void parseKnownArgs(String[] args, List<String> unknown, Map<String,Object> attrs) throws ArgumentParserException
ArgumentParser
Just like ArgumentParser.parseArgs(String[], Map), but parses only known
arguments without throwing exception for unrecognized arguments. If
unknown is not null, unrecognized arguments will be stored in it.
parseKnownArgs in interface ArgumentParserargs - Command line arguments.unknown - Output variable to store unrecognized arguments, or nullattrs - Map object to store attributes.ArgumentParserException - If an error occurred.public void parseArgs(String[] args, Object userData) throws ArgumentParserException
ArgumentParserParses command line arguments.
Unlike ArgumentParser.parseArgs(String[]), which returns Namespace
object, this method stores attributes in given userData. The
location to store value is designated using Arg annotations. User
don't have to specify Arg for all attributes: the missing
attributes are just skipped. This method performs simple List to
generic array conversion. For example, user can assign
List<Integer> attribute to generic array int[].
parseArgs in interface ArgumentParserargs - Command line arguments.userData - Object to store attributes.ArgumentParserException - If an error occurred.public void parseKnownArgs(String[] args, List<String> unknown, Object userData) throws ArgumentParserException
ArgumentParser
Just like ArgumentParser.parseArgs(String[], Object), but parses only known
arguments without throwing exception for unrecognized arguments. If
unknown is not null, unrecognized arguments will be stored in it.
parseKnownArgs in interface ArgumentParserargs - Command line arguments.unknown - Output variable to store unrecognized arguments, or nulluserData - Object to store attributes.ArgumentParserException - If an error occurred.public void parseArgs(String[] args, Map<String,Object> attrs, Object userData) throws ArgumentParserException
ArgumentParserParses command line arguments.
This is a combination of ArgumentParser.parseArgs(String[], Map) and
ArgumentParser.parseArgs(String[], Object). The all attributes will be stored
in attrs. The attributes specified in Arg annotations
will be also stored in userData.
parseArgs in interface ArgumentParserargs - Command line arguments.attrs - Map to store attributes.userData - Object to store attributes.ArgumentParserException - If an error occurred.public void parseKnownArgs(String[] args, List<String> unknown, Map<String,Object> attrs, Object userData) throws ArgumentParserException
ArgumentParser
Just like ArgumentParser.parseArgs(String[], Map, Object), but parses only
known arguments without throwing exception for unrecognized arguments. If
unknown is not null, unrecognized arguments will be stored in it.
parseKnownArgs in interface ArgumentParserargs - Command line arguments.unknown - Output variable to store unrecognized arguments, or nullattrs - Map to store attributes.userData - Object to store attributes.ArgumentParserException - If an error occurred.public void parseArgs(net.sourceforge.argparse4j.internal.ParseState state,
Map<String,Object> attrs)
throws ArgumentParserException
ArgumentParserExceptionpublic ArgumentParserConfigurationImpl getConfig()
ArgumentParserGet the configuration of this argument parser.
getConfig in interface ArgumentParserpublic String getProg()
public void printVersion()
ArgumentParserprintVersion in interface ArgumentParserpublic void printVersion(PrintWriter writer)
ArgumentParserprintVersion in interface ArgumentParserwriter - Writer to print version string.public String formatVersion()
ArgumentParserformatVersion in interface ArgumentParserpublic void handleError(ArgumentParserException e)
ArgumentParserPrints usage and error message.
Please note that this method does not terminate the program.
handleError in interface ArgumentParsere - Error thrown by ArgumentParser.parseArgs(String[]).public void handleError(ArgumentParserException e, PrintWriter writer)
ArgumentParserPrints usage and error message to the given writer.
Please note that this method does not terminate the program.
handleError in interface ArgumentParsere - Error thrown by ArgumentParser.parseArgs(String[]).public String getCommand()
public String getPrefixChars()
public String getFromFilePrefixChars()
Copyright © 2012–2017. All rights reserved.