Class ArgumentParserImpl
- java.lang.Object
-
- net.sourceforge.argparse4j.internal.ArgumentParserImpl
-
- All Implemented Interfaces:
ArgumentParser
public final class ArgumentParserImpl extends java.lang.Object implements ArgumentParser
The application code must not use this class directly.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classArgumentParserImpl.Candidate
-
Constructor Summary
Constructors Constructor Description ArgumentParserImpl(java.lang.String prog)ArgumentParserImpl(java.lang.String prog, boolean addHelp)ArgumentParserImpl(java.lang.String prog, boolean addHelp, java.lang.String prefixChars)ArgumentParserImpl(java.lang.String prog, boolean addHelp, java.lang.String prefixChars, java.lang.String fromFilePrefix)ArgumentParserImpl(java.lang.String prog, boolean addHelp, java.lang.String prefixChars, java.lang.String fromFilePrefix, TextWidthCounter textWidthCounter)ArgumentParserImpl(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 ArgumentImpladdArgument(java.lang.String... nameOrFlags)Creates newArgumentobject and adds to this parser and returns the object.ArgumentImpladdArgument(ArgumentGroupImpl group, java.lang.String... nameOrFlags)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.SubparsersImpladdSubparsers()ReturnsSubparsers.ArgumentParserImpldefaultHelp(boolean defaultHelp)If defaultHelp istrue, the default values of arguments are printed in help message.ArgumentParserImpldescription(java.lang.String description)Set text to display before the argument help.ArgumentParserImplepilog(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.StringgetCommand()java.lang.ObjectgetDefault(java.lang.String dest)Returns default value set byArgumentImpl.setDefault(Object)orsetDefault(String, Object).java.lang.StringgetFromFilePrefixChars()ArgumentParserImplgetMainParser()Returns main (parent) parser.java.lang.StringgetPrefixChars()java.lang.StringgetProg()TextWidthCountergetTextWidthCounter()voidhandleError(ArgumentParserException e)Prints usage and error message.booleanisDefaultHelp()NamespaceparseArgs(java.lang.String[] args)Parses command line arguments.voidparseArgs(java.lang.String[] args, int offset, java.util.Map<java.lang.String,java.lang.Object> attrs)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.voidparseArgs(ParseState state, java.util.Map<java.lang.String,java.lang.Object> attrs)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.ArgumentParserImplsetDefault(java.lang.String dest, java.lang.Object value)Sets parser-level default value of attributedest.ArgumentParserImplsetDefaults(java.util.Map<java.lang.String,java.lang.Object> attrs)Sets parser-level default values fromattrs.ArgumentParserImplusage(java.lang.String usage)Sets the text to display as usage line.ArgumentParserImplversion(java.lang.String version)Sets version string.
-
-
-
Constructor Detail
-
ArgumentParserImpl
public ArgumentParserImpl(java.lang.String prog)
-
ArgumentParserImpl
public ArgumentParserImpl(java.lang.String prog, boolean addHelp)
-
ArgumentParserImpl
public ArgumentParserImpl(java.lang.String prog, boolean addHelp, java.lang.String prefixChars)
-
ArgumentParserImpl
public ArgumentParserImpl(java.lang.String prog, boolean addHelp, java.lang.String prefixChars, java.lang.String fromFilePrefix)
-
ArgumentParserImpl
public ArgumentParserImpl(java.lang.String prog, boolean addHelp, java.lang.String prefixChars, java.lang.String fromFilePrefix, TextWidthCounter textWidthCounter)
-
ArgumentParserImpl
public ArgumentParserImpl(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 ArgumentImpl addArgument(java.lang.String... nameOrFlags)
Description copied from interface:ArgumentParserCreates 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.- Specified by:
addArgumentin interfaceArgumentParser- Parameters:
nameOrFlags- A name or a list of option strings of newArgument.- Returns:
Argumentobject.
-
addArgument
public ArgumentImpl addArgument(ArgumentGroupImpl group, java.lang.String... nameOrFlags)
-
addSubparsers
public SubparsersImpl addSubparsers()
Description copied from interface:ArgumentParserReturns
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.- Specified by:
addSubparsersin interfaceArgumentParser- Returns:
Subparsersobject.
-
addArgumentGroup
public ArgumentGroup addArgumentGroup(java.lang.String title)
Description copied from interface:ArgumentParserCreates 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.- Specified by:
addArgumentGroupin interfaceArgumentParser- Parameters:
title- The title printed in help message.- Returns:
ArgumentGroupobject.
-
addMutuallyExclusiveGroup
public MutuallyExclusiveGroup addMutuallyExclusiveGroup()
Description copied from interface:ArgumentParserCreates new mutually exclusive group,
MutuallyExclusiveGroupobject, without title and adds to this parser and returns the object.- Specified by:
addMutuallyExclusiveGroupin interfaceArgumentParser- Returns:
MutuallyExclusiveGroupobject.
-
addMutuallyExclusiveGroup
public MutuallyExclusiveGroup addMutuallyExclusiveGroup(java.lang.String title)
Description copied from interface:ArgumentParserCreates 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.- Specified by:
addMutuallyExclusiveGroupin interfaceArgumentParser- Parameters:
title- The title printed in help message.- Returns:
- The
MutuallyExclusiveGroupobject.
-
usage
public ArgumentParserImpl usage(java.lang.String usage)
Description copied from interface: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).- Specified by:
usagein interfaceArgumentParser- Parameters:
usage- usage text- Returns:
- this
-
description
public ArgumentParserImpl description(java.lang.String description)
Set text to display before the argument help.- Specified by:
descriptionin interfaceArgumentParser- Parameters:
description- text to display before the argument help- Returns:
- this
-
epilog
public ArgumentParserImpl epilog(java.lang.String epilog)
Description copied from interface:ArgumentParserSets the text to display after the argument help.- Specified by:
epilogin interfaceArgumentParser- Parameters:
epilog- The text to display after the argument help.- Returns:
- this
-
version
public ArgumentParserImpl version(java.lang.String version)
Description copied from interface:ArgumentParserSets 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:
versionin interfaceArgumentParser- Parameters:
version- The version string.- Returns:
- this
-
defaultHelp
public ArgumentParserImpl defaultHelp(boolean defaultHelp)
Description copied from interface:ArgumentParserIf 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:
defaultHelpin interfaceArgumentParser- Parameters:
defaultHelp- Switch to display the default value in help message.- Returns:
- this
-
isDefaultHelp
public boolean isDefaultHelp()
-
printHelp
public void printHelp()
Description copied from interface:ArgumentParserPrints help message in stdout.- Specified by:
printHelpin interfaceArgumentParser
-
printHelp
public void printHelp(java.io.PrintWriter writer)
Description copied from interface:ArgumentParserPrints help message in writer.- Specified by:
printHelpin interfaceArgumentParser- Parameters:
writer- Writer to print message.
-
formatHelp
public java.lang.String formatHelp()
Description copied from interface:ArgumentParserReturns help message.- Specified by:
formatHelpin interfaceArgumentParser- Returns:
- The help message.
-
printUsage
public void printUsage()
Description copied from interface:ArgumentParserPrint a brief description of how the program should be invoked on the command line in stdout.- Specified by:
printUsagein interfaceArgumentParser
-
printUsage
public void printUsage(java.io.PrintWriter writer)
Description copied from interface:ArgumentParserPrint a brief description of how the program should be invoked on the command line in writer.- Specified by:
printUsagein interfaceArgumentParser- Parameters:
writer- Writer to print message.
-
formatUsage
public java.lang.String formatUsage()
Description copied from interface:ArgumentParserReturns a brief description of how the program should be invoked on the command line.- Specified by:
formatUsagein interfaceArgumentParser- Returns:
- Usage text.
-
setDefault
public ArgumentParserImpl setDefault(java.lang.String dest, java.lang.Object value)
Description copied from interface:ArgumentParserSets parser-level default value of attribute
dest.The parser-level defaults always override argument-level defaults.
- Specified by:
setDefaultin interfaceArgumentParser- Parameters:
dest- The attribute name.value- The default value.- Returns:
- this
-
setDefaults
public ArgumentParserImpl setDefaults(java.util.Map<java.lang.String,java.lang.Object> attrs)
Description copied from interface:ArgumentParserSets 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.- Specified by:
setDefaultsin interfaceArgumentParser- Parameters:
attrs- The parser-level default values to add.- Returns:
- this
-
getDefault
public java.lang.Object getDefault(java.lang.String dest)
Returns default value set byArgumentImpl.setDefault(Object)orsetDefault(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.- Specified by:
getDefaultin interfaceArgumentParser- Parameters:
dest- attribute name of default value to get.- Returns:
- default value of given dest.
-
parseArgsOrFail
public Namespace parseArgsOrFail(java.lang.String[] args)
Description copied from interface:ArgumentParserParses 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 aNamespaceobject. Otherwise, the program exits with a1return code.- Specified by:
parseArgsOrFailin interfaceArgumentParser- Parameters:
args- Command line arguments.- Returns:
Namespaceobject.
-
parseArgs
public Namespace parseArgs(java.lang.String[] args) throws ArgumentParserException
Description copied from interface:ArgumentParserParses 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.- Specified by:
parseArgsin interfaceArgumentParser- Parameters:
args- Command line arguments.- Returns:
Namespaceobject.- 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 ArgumentParserExceptionDescription copied from interface:ArgumentParserParses command line arguments.
Unlike
ArgumentParser.parseArgs(String[]), which returnsNamespaceobject, this method stores attributes in givenattrs.- Specified by:
parseArgsin 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 ArgumentParserExceptionDescription copied from interface:ArgumentParserParses command line arguments.
Unlike
ArgumentParser.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[].- Specified by:
parseArgsin 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 ArgumentParserExceptionDescription copied from interface:ArgumentParserParses 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 inArgannotations will be also stored inuserData.- Specified by:
parseArgsin interfaceArgumentParser- Parameters:
args- Command line arguments.attrs- Map to store attributes.userData- Object to store attributes.- Throws:
ArgumentParserException- If an error occurred.
-
parseArgs
public void parseArgs(java.lang.String[] args, int offset, java.util.Map<java.lang.String,java.lang.Object> attrs) throws ArgumentParserException- Throws:
ArgumentParserException
-
parseArgs
public void parseArgs(ParseState state, java.util.Map<java.lang.String,java.lang.Object> attrs) throws ArgumentParserException
- Throws:
ArgumentParserException
-
getProg
public java.lang.String getProg()
-
printVersion
public void printVersion()
Description copied from interface:ArgumentParserPrints version string in stdout.- Specified by:
printVersionin interfaceArgumentParser
-
printVersion
public void printVersion(java.io.PrintWriter writer)
Description copied from interface:ArgumentParserPrints version string in writer.- Specified by:
printVersionin interfaceArgumentParser- Parameters:
writer- Writer to print version string.
-
formatVersion
public java.lang.String formatVersion()
Description copied from interface:ArgumentParserReturns version string.- Specified by:
formatVersionin interfaceArgumentParser- Returns:
- The version string.
-
handleError
public void handleError(ArgumentParserException e)
Description copied from interface:ArgumentParserPrints usage and error message.
Please note that this method does not terminate the program.
- Specified by:
handleErrorin interfaceArgumentParser- Parameters:
e- Error thrown byArgumentParser.parseArgs(String[]).
-
getCommand
public java.lang.String getCommand()
-
getTextWidthCounter
public TextWidthCounter getTextWidthCounter()
-
getPrefixChars
public java.lang.String getPrefixChars()
-
getFromFilePrefixChars
public java.lang.String getFromFilePrefixChars()
-
getMainParser
public ArgumentParserImpl getMainParser()
Returns main (parent) parser.- Returns:
- The main (parent) parser. null if this object is a root parser.
-
-