Class SubparserImpl
- java.lang.Object
-
- net.sourceforge.argparse4j.internal.SubparserImpl
-
- All Implemented Interfaces:
ArgumentParser
,Subparser
public final class SubparserImpl extends java.lang.Object implements Subparser
The application code must not use this class directly.
-
-
Constructor Summary
Constructors Constructor Description SubparserImpl(java.lang.String prog, boolean addHelp, java.lang.String prefixChars, java.lang.String fromFilePrefix, TextWidthCounter textWidthCounter, java.lang.String command, ArgumentParserImpl mainParser)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Argument
addArgument(java.lang.String... nameOrFlags)
Creates newArgument
object and adds to this parser and returns the object.ArgumentGroup
addArgumentGroup(java.lang.String title)
Creates newArgumentGroup
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(java.lang.String title)
Creates new mutually exclusive group,MutuallyExclusiveGroup
object, and adds to this parser and returns the object.Subparsers
addSubparsers()
ReturnsSubparsers
.SubparserImpl
aliases(java.lang.String... alias)
Sets alias names for this Subparser.SubparserImpl
defaultHelp(boolean defaultHelp)
If defaultHelp istrue
, the default values of arguments are printed in help message.SubparserImpl
description(java.lang.String description)
Sets the text to display before the argument help.SubparserImpl
epilog(java.lang.String epilog)
Sets the text to display after the argument help.java.lang.String
formatHelp()
Returns help message.java.lang.String
formatUsage()
Returns a brief description of how the program should be invoked on the command line.java.lang.String
formatVersion()
Returns version string.java.lang.String
getCommand()
java.lang.Object
getDefault(java.lang.String dest)
Returns default value of givendest
.void
handleError(ArgumentParserException e)
Prints usage and error message.SubparserImpl
help(java.lang.String help)
Sets the text to display in help message.Namespace
parseArgs(java.lang.String[] args)
Parses command line arguments.void
parseArgs(java.lang.String[] args, java.lang.Object userData)
Parses command line arguments.void
parseArgs(java.lang.String[] args, java.util.Map<java.lang.String,java.lang.Object> attrs)
Parses command line arguments.void
parseArgs(java.lang.String[] args, java.util.Map<java.lang.String,java.lang.Object> attrs, java.lang.Object userData)
Parses command line arguments.void
parseArgs(ParseState state, java.util.Map<java.lang.String,java.lang.Object> opts)
Namespace
parseArgsOrFail(java.lang.String[] args)
Parses command line arguments, handling any errors.void
printHelp()
Prints help message in stdout.void
printHelp(java.io.PrintWriter writer)
Prints help message in writer.void
printSubparserHelp(java.io.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(java.io.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(java.io.PrintWriter writer)
Prints version string in writer.SubparserImpl
setDefault(java.lang.String dest, java.lang.Object value)
Sets parser-level default value of attributedest
.SubparserImpl
setDefaults(java.util.Map<java.lang.String,java.lang.Object> attrs)
Sets parser-level default values fromattrs
.SubparserImpl
usage(java.lang.String usage)
Sets the text to display as usage line.SubparserImpl
version(java.lang.String version)
Sets version string.
-
-
-
Constructor Detail
-
SubparserImpl
public SubparserImpl(java.lang.String prog, boolean addHelp, java.lang.String prefixChars, java.lang.String fromFilePrefix, TextWidthCounter textWidthCounter, java.lang.String command, ArgumentParserImpl mainParser)
-
-
Method Detail
-
addArgument
public Argument addArgument(java.lang.String... nameOrFlags)
Description copied from interface: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
.- Specified by:
addArgument
in interfaceArgumentParser
- Parameters:
nameOrFlags
- A name or a list of option strings of newArgument
.- Returns:
Argument
object.
-
addArgumentGroup
public ArgumentGroup addArgumentGroup(java.lang.String title)
Description copied from interface: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.- Specified by:
addArgumentGroup
in interfaceArgumentParser
- Parameters:
title
- The title printed in help message.- Returns:
ArgumentGroup
object.
-
addMutuallyExclusiveGroup
public MutuallyExclusiveGroup addMutuallyExclusiveGroup()
Description copied from interface:ArgumentParser
Creates new mutually exclusive group,
MutuallyExclusiveGroup
object, without title and adds to this parser and returns the object.- Specified by:
addMutuallyExclusiveGroup
in interfaceArgumentParser
- Returns:
MutuallyExclusiveGroup
object.
-
addMutuallyExclusiveGroup
public MutuallyExclusiveGroup addMutuallyExclusiveGroup(java.lang.String title)
Description copied from interface: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.- Specified by:
addMutuallyExclusiveGroup
in interfaceArgumentParser
- Parameters:
title
- The title printed in help message.- Returns:
- The
MutuallyExclusiveGroup
object.
-
addSubparsers
public Subparsers addSubparsers()
Description copied from interface: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 sameSubparsers
object.Subparsers
object provides a way to add sub-commands.- Specified by:
addSubparsers
in interfaceArgumentParser
- Returns:
Subparsers
object.
-
usage
public SubparserImpl usage(java.lang.String usage)
Description copied from interface: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)
.- Specified by:
usage
in interfaceArgumentParser
- Parameters:
usage
- usage text- Returns:
- this
-
description
public SubparserImpl description(java.lang.String description)
Description copied from interface:ArgumentParser
Sets the text to display before the argument help.- Specified by:
description
in interfaceArgumentParser
- Specified by:
description
in interfaceSubparser
- Parameters:
description
- The text to display before the argument help.- Returns:
- this
-
epilog
public SubparserImpl epilog(java.lang.String epilog)
Description copied from interface:ArgumentParser
Sets the text to display after the argument help.- Specified by:
epilog
in interfaceArgumentParser
- Specified by:
epilog
in interfaceSubparser
- Parameters:
epilog
- The text to display after the argument help.- Returns:
- this
-
version
public SubparserImpl version(java.lang.String version)
Description copied from interface: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.- Specified by:
version
in interfaceArgumentParser
- Specified by:
version
in interfaceSubparser
- Parameters:
version
- The version string.- Returns:
- this
-
defaultHelp
public SubparserImpl defaultHelp(boolean defaultHelp)
Description copied from interface: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.
- Specified by:
defaultHelp
in interfaceArgumentParser
- Specified by:
defaultHelp
in interfaceSubparser
- Parameters:
defaultHelp
- Switch to display the default value in help message.- Returns:
- this
-
printHelp
public void printHelp()
Description copied from interface:ArgumentParser
Prints help message in stdout.- Specified by:
printHelp
in interfaceArgumentParser
-
printHelp
public void printHelp(java.io.PrintWriter writer)
Description copied from interface:ArgumentParser
Prints help message in writer.- Specified by:
printHelp
in interfaceArgumentParser
- Parameters:
writer
- Writer to print message.
-
formatHelp
public java.lang.String formatHelp()
Description copied from interface:ArgumentParser
Returns help message.- Specified by:
formatHelp
in interfaceArgumentParser
- Returns:
- The help message.
-
printUsage
public void printUsage()
Description copied from interface:ArgumentParser
Print a brief description of how the program should be invoked on the command line in stdout.- Specified by:
printUsage
in interfaceArgumentParser
-
printUsage
public void printUsage(java.io.PrintWriter writer)
Description copied from interface:ArgumentParser
Print a brief description of how the program should be invoked on the command line in writer.- Specified by:
printUsage
in interfaceArgumentParser
- Parameters:
writer
- Writer to print message.
-
formatUsage
public java.lang.String formatUsage()
Description copied from interface:ArgumentParser
Returns a brief description of how the program should be invoked on the command line.- Specified by:
formatUsage
in interfaceArgumentParser
- Returns:
- Usage text.
-
printVersion
public void printVersion()
Description copied from interface:ArgumentParser
Prints version string in stdout.- Specified by:
printVersion
in interfaceArgumentParser
-
printVersion
public void printVersion(java.io.PrintWriter writer)
Description copied from interface:ArgumentParser
Prints version string in writer.- Specified by:
printVersion
in interfaceArgumentParser
- Parameters:
writer
- Writer to print version string.
-
formatVersion
public java.lang.String formatVersion()
Description copied from interface:ArgumentParser
Returns version string.- Specified by:
formatVersion
in interfaceArgumentParser
- Returns:
- The version string.
-
setDefault
public SubparserImpl setDefault(java.lang.String dest, java.lang.Object value)
Description copied from interface:ArgumentParser
Sets parser-level default value of attribute
dest
.The parser-level defaults always override argument-level defaults.
- Specified by:
setDefault
in interfaceArgumentParser
- Specified by:
setDefault
in interfaceSubparser
- Parameters:
dest
- The attribute name.value
- The default value.- Returns:
- this
-
setDefaults
public SubparserImpl setDefaults(java.util.Map<java.lang.String,java.lang.Object> attrs)
Description copied from interface: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.- Specified by:
setDefaults
in interfaceArgumentParser
- Specified by:
setDefaults
in interfaceSubparser
- Parameters:
attrs
- The parser-level default values to add.- Returns:
- this
-
getDefault
public java.lang.Object getDefault(java.lang.String dest)
Description copied from interface:ArgumentParser
Returns default value of given
dest
.Returns default value set by
Argument.setDefault(Object)
,ArgumentParser.setDefault(String, Object)
orArgumentParser.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, returnsnull
.- Specified by:
getDefault
in interfaceArgumentParser
- Parameters:
dest
- The attribute name of default value to get.- Returns:
- The default value of given dest.
-
help
public SubparserImpl help(java.lang.String help)
Description copied from interface:Subparser
Sets the text to display in help message.
-
parseArgsOrFail
public Namespace parseArgsOrFail(java.lang.String[] args)
Description copied from interface:ArgumentParser
Parses command line arguments, handling any errors.
This is a shortcut method that combines
ArgumentParser.parseArgs(java.lang.String[])
andArgumentParser.handleError(net.sourceforge.argparse4j.inf.ArgumentParserException)
. If the arguments can be successfully parsed, the resulted attributes are returned as aNamespace
object. Otherwise, the program exits with a1
return code.- Specified by:
parseArgsOrFail
in interfaceArgumentParser
- Parameters:
args
- Command line arguments.- Returns:
Namespace
object.
-
parseArgs
public Namespace parseArgs(java.lang.String[] args) throws ArgumentParserException
Description copied from interface: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.- Specified by:
parseArgs
in interfaceArgumentParser
- Parameters:
args
- Command line arguments.- Returns:
Namespace
object.- Throws:
ArgumentParserException
- If an error occurred.
-
parseArgs
public void parseArgs(java.lang.String[] args, java.util.Map<java.lang.String,java.lang.Object> attrs) throws ArgumentParserException
Description copied from interface:ArgumentParser
Parses command line arguments.
Unlike
ArgumentParser.parseArgs(String[])
, which returnsNamespace
object, this method stores attributes in givenattrs
.- Specified by:
parseArgs
in interfaceArgumentParser
- Parameters:
args
- Command line arguments.attrs
- Map object to store attributes.- Throws:
ArgumentParserException
- If an error occurred.
-
parseArgs
public void parseArgs(java.lang.String[] args, java.lang.Object userData) throws ArgumentParserException
Description copied from interface:ArgumentParser
Parses command line arguments.
Unlike
ArgumentParser.parseArgs(String[])
, which returnsNamespace
object, this method stores attributes in givenuserData
. The location to store value is designated usingArg
annotations. User don't have to specifyArg
for all attributes: the missing attributes are just skipped. This method performs simpleList
to generic array conversion. For example, user can assignList<Integer>
attribute to generic arrayint[]
.- Specified by:
parseArgs
in interfaceArgumentParser
- Parameters:
args
- Command line arguments.userData
- Object to store attributes.- Throws:
ArgumentParserException
- If an error occurred.
-
parseArgs
public void parseArgs(java.lang.String[] args, java.util.Map<java.lang.String,java.lang.Object> attrs, java.lang.Object userData) throws ArgumentParserException
Description copied from interface:ArgumentParser
Parses command line arguments.
This is a combination of
ArgumentParser.parseArgs(String[], Map)
andArgumentParser.parseArgs(String[], Object)
. The all attributes will be stored inattrs
. The attributes specified inArg
annotations will be also stored inuserData
.- Specified by:
parseArgs
in interfaceArgumentParser
- Parameters:
args
- Command line arguments.attrs
- Map to store attributes.userData
- Object to store attributes.- Throws:
ArgumentParserException
- If an error occurred.
-
handleError
public void handleError(ArgumentParserException e)
Description copied from interface:ArgumentParser
Prints usage and error message.
Please note that this method does not terminate the program.
- Specified by:
handleError
in interfaceArgumentParser
- Parameters:
e
- Error thrown byArgumentParser.parseArgs(String[])
.
-
aliases
public SubparserImpl aliases(java.lang.String... alias)
Description copied from interface:Subparser
Sets alias names for this Subparser. The alias names must be unique for eachSubparsers
instance which this object belongs to.
-
parseArgs
public void parseArgs(ParseState state, java.util.Map<java.lang.String,java.lang.Object> opts) throws ArgumentParserException
- Throws:
ArgumentParserException
-
printSubparserHelp
public void printSubparserHelp(java.io.PrintWriter writer, int format_width)
-
getCommand
public java.lang.String getCommand()
-
-