- java.lang.Object
-
- net.sourceforge.argparse4j.ArgumentParserBuilder
-
public class ArgumentParserBuilder extends java.lang.ObjectArgumentParserBuilder is a class to build new ArgumentParser with a given custom configuration.- Since:
- 0.8.0
-
-
Field Summary
Fields Modifier and Type Field Description private booleanaddHelp_private booleancjkWidthHack_private intdefaultFormatWidth_private java.lang.StringfromFilePrefix_private booleanincludeArgumentNamesAsKeysInResult_private java.util.Localelocale_(package private) booleanmustHelpTextIncludeMutualExclusivity_private booleannoDestConversionForPositionalArgs_private java.lang.StringprefixChars_private java.lang.Stringprog_private booleansingleMetavar_private booleanterminalWidthDetection_
-
Constructor Summary
Constructors Constructor Description ArgumentParserBuilder(java.lang.String prog, DefaultSettings defaultSettings)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ArgumentParserBuilderaddHelp(boolean flag)If true,-h/--helpare available.ArgumentParserbuild()ArgumentParserBuildercjkWidthHack(boolean flag)Settrueto enable CJK width hack.private ArgumentParserConfigurationImplconfig()private TextWidthCountercreateTextWidthCounter()ArgumentParserBuilderdefaultFormatWidth(int defaultFormatWidth)Set the default format width.ArgumentParserBuilderfromFilePrefix(java.lang.String fromFilePrefix)The set of characters that prefix file path from which additional arguments should be read.private intgetFormatWidth()ArgumentParserBuilderincludeArgumentNamesAsKeysInResult(boolean flag)Include the name (see below) of the argument as a key in the parse result in addition to the key indestof the argument.ArgumentParserBuilderlocale(java.util.Locale locale)The locale to use for messages.ArgumentParserBuildermustHelpTextIncludeMutualExclusivity(boolean flag)Add a text to the help of mutually-exclusive groups explaining that at most 1 arguments of the group may be given.ArgumentParserBuildernoDestConversionForPositionalArgs(boolean flag)Do not perform any conversion to produce "dest" value (SeeArgument.getDest()) from positional argument name.ArgumentParserBuilderprefixChars(java.lang.String prefixChars)The set of characters that prefix named arguments.ArgumentParserBuildersingleMetavar(boolean flag)If singleMetavar istrue, a metavar string in help message is only shown after the last flag instead of each flag.ArgumentParserBuilderterminalWidthDetection(boolean flag)Settrueto enable terminal width detection.
-
-
-
Field Detail
-
prog_
private final java.lang.String prog_
-
addHelp_
private boolean addHelp_
-
prefixChars_
private java.lang.String prefixChars_
-
fromFilePrefix_
private java.lang.String fromFilePrefix_
-
locale_
private java.util.Locale locale_
-
cjkWidthHack_
private boolean cjkWidthHack_
-
defaultFormatWidth_
private int defaultFormatWidth_
-
terminalWidthDetection_
private boolean terminalWidthDetection_
-
singleMetavar_
private boolean singleMetavar_
-
noDestConversionForPositionalArgs_
private boolean noDestConversionForPositionalArgs_
-
includeArgumentNamesAsKeysInResult_
private boolean includeArgumentNamesAsKeysInResult_
-
mustHelpTextIncludeMutualExclusivity_
boolean mustHelpTextIncludeMutualExclusivity_
-
-
Constructor Detail
-
ArgumentParserBuilder
ArgumentParserBuilder(java.lang.String prog, DefaultSettings defaultSettings)
-
-
Method Detail
-
addHelp
public ArgumentParserBuilder addHelp(boolean flag)
If true,
-h/--helpare available. If false, they are not. Default value:true.- Parameters:
flag-trueorfalse- Returns:
- This builder.
-
prefixChars
public ArgumentParserBuilder prefixChars(java.lang.String prefixChars)
The set of characters that prefix named arguments. Default value:
"-".- Parameters:
prefixChars- The named argument prefixes.- Returns:
- This builder.
-
fromFilePrefix
public ArgumentParserBuilder fromFilePrefix(java.lang.String fromFilePrefix)
The set of characters that prefix file path from which additional arguments should be read. Specify
nullto disable reading arguments from file. Default value:null.- Parameters:
fromFilePrefix- The from file path prefixes.- Returns:
- This builder.
-
locale
public ArgumentParserBuilder locale(java.util.Locale locale)
The locale to use for messages. Default value:
Locale.getDefault().- Parameters:
locale- The locale for messages.- Returns:
- This builder.
-
cjkWidthHack
public ArgumentParserBuilder cjkWidthHack(boolean flag)
Set
trueto enable CJK width hack.The CJK width hack is treat Unicode characters having East Asian Width property Wide/Full/Ambiguous to have twice a width of ascii characters when formatting help message if locale is "ja", "zh" or "ko". This feature is enabled by default.
- Parameters:
flag-trueorfalse- Returns:
- This builder.
-
defaultFormatWidth
public ArgumentParserBuilder defaultFormatWidth(int defaultFormatWidth)
Set the default format width. This width is used when terminal width detection is disabled or when terminal width detection cannot detect the actual number of columns. Default value:
ArgumentParsers.DEFAULT_FORMAT_WIDTH.- Parameters:
defaultFormatWidth- The default format width.- Returns:
- This builder.
-
terminalWidthDetection
public ArgumentParserBuilder terminalWidthDetection(boolean flag)
Set
trueto enable terminal width detection.If this feature is enabled, argparse4j will automatically detect the terminal width and use it to format help messages. This feature is enabled by default.
- Parameters:
flag-trueorfalse- Returns:
- This builder.
-
singleMetavar
public ArgumentParserBuilder singleMetavar(boolean flag)
If singleMetavar is
true, a metavar string in help message is only shown after the last flag instead of each flag.By default and
falseis given to this method, a metavar is shown after each flag:-f FOO, --foo FOO
If
trueis given to this method, a metavar string is shown only once:-f, --foo FOO
- Parameters:
flag- Switch to display a metavar only after the last flag.- Returns:
- This builder.
-
noDestConversionForPositionalArgs
public ArgumentParserBuilder noDestConversionForPositionalArgs(boolean flag)
Do not perform any conversion to produce "dest" value (See
Argument.getDest()) from positional argument name.Prior 0.5.0, no conversion is made to produce "dest" value from positional argument name. Since 0.5.0, "dest" value is generated by replacing "-" with "_" in positional argument name. This is the same conversion rule for named arguments.
By default, this is set to
false(which means, conversion will be done). Application is advised to update its implementation to cope with this change. But if it is not feasible, call this method withtrueto turn off the conversion and retain the same behaviour with pre-0.5.0 version.- Parameters:
flag- Switch not to perform conversion to produce dest for positional arguments. Iftrueis given, no conversion is made.- Returns:
- This builder.
-
includeArgumentNamesAsKeysInResult
public ArgumentParserBuilder includeArgumentNamesAsKeysInResult(boolean flag)
Include the name (see below) of the argument as a key in the parse result in addition to the key in
destof the argument.The argument name is determined as follows:
- Positional arguments: the name of the argument
- Named arguments: the first long flag, or if no long flag is present, the first flag, without the prefix
- Parameters:
flag- Iftrueis given, the argument name (as defined above) will also be in included as a key in the result. Otherwise onlydestof the argument will be a key in the result.- Returns:
- This builder.
-
mustHelpTextIncludeMutualExclusivity
public ArgumentParserBuilder mustHelpTextIncludeMutualExclusivity(boolean flag)
Add a text to the help of mutually-exclusive groups explaining that at most 1 arguments of the group may be given.
- Parameters:
flag- Iftrueis given, the help text of mutually-exclusive groups will be extended to indicate at most 1 argument of the group can be given. Otherwise the mutual exclusivity of a group is only visible in the usage.- Returns:
- This builder.
-
build
public ArgumentParser build()
-
config
private ArgumentParserConfigurationImpl config()
-
createTextWidthCounter
private TextWidthCounter createTextWidthCounter()
-
getFormatWidth
private int getFormatWidth()
-
-