public final class SubparserImpl extends Object implements Subparser
Constructor and Description |
---|
SubparserImpl(String prog,
boolean addHelp,
String prefixChars,
String fromFilePrefix,
TextWidthCounter textWidthCounter,
String command,
ArgumentParserImpl mainParser) |
Modifier and Type | Method and Description |
---|---|
Argument |
addArgument(String... nameOrFlags)
Creates new
Argument object and adds to this parser and returns
the object. |
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. |
Subparsers |
addSubparsers()
Returns
Subparsers . |
SubparserImpl |
aliases(String... alias)
Sets alias names for this Subparser.
|
SubparserImpl |
defaultHelp(boolean defaultHelp)
If defaultHelp is
true , the default values of arguments are
printed in help message. |
SubparserImpl |
description(String description)
Sets the text to display before the argument help.
|
SubparserImpl |
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() |
Object |
getDefault(String dest)
Returns default value of given
dest . |
void |
handleError(ArgumentParserException e)
Prints usage and error message.
|
SubparserImpl |
help(String help)
Sets the text to display in help message.
|
void |
parseArgs(ParseState state,
Map<String,Object> opts) |
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.
|
void |
printHelp()
Prints help message in stdout.
|
void |
printHelp(PrintWriter writer)
Prints help message in writer.
|
void |
printSubparserHelp(PrintWriter writer,
int format_width) |
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.
|
SubparserImpl |
setDefault(String dest,
Object value)
Sets parser-level default value of attribute
dest . |
SubparserImpl |
setDefaults(Map<String,Object> attrs)
Sets parser-level default values from
attrs . |
SubparserImpl |
usage(String usage)
Sets the text to display as usage line.
|
SubparserImpl |
version(String version)
Sets version string.
|
public SubparserImpl(String prog, boolean addHelp, String prefixChars, String fromFilePrefix, TextWidthCounter textWidthCounter, String command, ArgumentParserImpl mainParser)
public Argument addArgument(String... nameOrFlags)
ArgumentParser
Creates new Argument
object and adds to this parser and returns
the object.
The nameOrFlags
is either a single name of positional argument or
a list of option strings for optional argument, e.g. foo
or
-f, --foo
.
addArgument
in interface ArgumentParser
nameOrFlags
- A name or a list of option strings of new Argument
.Argument
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 ArgumentParser
title
- 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 ArgumentParser
MutuallyExclusiveGroup
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 ArgumentParser
title
- The title printed in help message.MutuallyExclusiveGroup
object.public Subparsers 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 ArgumentParser
Subparsers
object.public SubparserImpl usage(String usage)
ArgumentParser
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)
.
usage
in interface ArgumentParser
usage
- usage textpublic SubparserImpl description(String description)
ArgumentParser
description
in interface ArgumentParser
description
in interface Subparser
description
- The text to display before the argument help.public SubparserImpl epilog(String epilog)
ArgumentParser
epilog
in interface ArgumentParser
epilog
in interface Subparser
epilog
- The text to display after the argument help.public SubparserImpl 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 ArgumentParser
version
in interface Subparser
version
- The version string.public SubparserImpl 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 ArgumentParser
defaultHelp
in interface Subparser
defaultHelp
- Switch to display the default value in help message.public void printHelp()
ArgumentParser
printHelp
in interface ArgumentParser
public void printHelp(PrintWriter writer)
ArgumentParser
printHelp
in interface ArgumentParser
writer
- Writer to print message.public String formatHelp()
ArgumentParser
formatHelp
in interface ArgumentParser
public void printUsage()
ArgumentParser
printUsage
in interface ArgumentParser
public void printUsage(PrintWriter writer)
ArgumentParser
printUsage
in interface ArgumentParser
writer
- Writer to print message.public String formatUsage()
ArgumentParser
formatUsage
in interface ArgumentParser
public void printVersion()
ArgumentParser
printVersion
in interface ArgumentParser
public void printVersion(PrintWriter writer)
ArgumentParser
printVersion
in interface ArgumentParser
writer
- Writer to print version string.public String formatVersion()
ArgumentParser
formatVersion
in interface ArgumentParser
public SubparserImpl 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 ArgumentParser
setDefault
in interface Subparser
dest
- The attribute name.value
- The default value.public SubparserImpl 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 ArgumentParser
setDefaults
in interface Subparser
attrs
- The parser-level default values to add.public Object getDefault(String dest)
ArgumentParser
Returns default value of given dest
.
Returns default value set by Argument.setDefault(Object)
,
ArgumentParser.setDefault(String, Object)
or
ArgumentParser.setDefaults(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, returns
null
.
getDefault
in interface ArgumentParser
dest
- The attribute name of default value to get.public SubparserImpl help(String help)
Subparser
public Namespace parseArgsOrFail(String[] args)
ArgumentParser
Parses 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 ArgumentParser
args
- Command line arguments.Namespace
object.public Namespace parseArgs(String[] args) throws ArgumentParserException
ArgumentParser
Parses 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 ArgumentParser
args
- Command line arguments.Namespace
object.ArgumentParserException
- If an error occurred.public void parseArgs(String[] args, Map<String,Object> attrs) throws ArgumentParserException
ArgumentParser
Parses command line arguments.
Unlike ArgumentParser.parseArgs(String[])
, which returns Namespace
object, this method stores attributes in given attrs
.
parseArgs
in interface ArgumentParser
args
- Command line arguments.attrs
- Map object to store attributes.ArgumentParserException
- If an error occurred.public void parseArgs(String[] args, Object userData) throws ArgumentParserException
ArgumentParser
Parses 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 ArgumentParser
args
- Command line arguments.userData
- Object to store attributes.ArgumentParserException
- If an error occurred.public void parseArgs(String[] args, Map<String,Object> attrs, Object userData) throws ArgumentParserException
ArgumentParser
Parses 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 ArgumentParser
args
- Command line arguments.attrs
- Map to store attributes.userData
- Object to store attributes.ArgumentParserException
- If an error occurred.public void handleError(ArgumentParserException e)
ArgumentParser
Prints usage and error message.
Please note that this method does not terminate the program.
handleError
in interface ArgumentParser
e
- Error thrown by ArgumentParser.parseArgs(String[])
.public SubparserImpl aliases(String... alias)
Subparser
Subparsers
instance which this object belongs to.public void parseArgs(ParseState state, Map<String,Object> opts) throws ArgumentParserException
ArgumentParserException
public void printSubparserHelp(PrintWriter writer, int format_width)
public String getCommand()
Copyright © 2012–2014. All rights reserved.