Class ArgumentParserBuilder


  • public class ArgumentParserBuilder
    extends java.lang.Object
    ArgumentParserBuilder is a class to build new ArgumentParser with a given custom configuration.
    Since:
    0.8.0
    • 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/--help are available. If false, they are not. Default value: true.

        Parameters:
        flag - true or false
        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 null to 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 true to 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 - true or false
        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 true to 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 - true or false
        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 false is given to this method, a metavar is shown after each flag:

         -f FOO, --foo FOO
         

        If true is 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 with true to 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. If true is 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 dest of 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 - If true is given, the argument name (as defined above) will also be in included as a key in the result. Otherwise only dest of 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 - If true is 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.
      • getFormatWidth

        private int getFormatWidth()