API Reference

For a better understanding it is useful to read the documentation for module argparse.

argparsebuilder.named_partial(name, func, /, doc=None, *args, **kwargs)[source]

Return a named partial function.

When an ArgumentParser creates an error message when the conversion of an argument failed, it uses the __name__ attribute of the conversion function for the type name. A function created with partial() does not have such an attribute and in that case a (ugly looking) name is created by applying repr() to the function.

This function is a convenient way to create a partial function and set its __name__ and __doc__ attributes.

Parameters
Returns

partial function object with __name__ attribute set to name

class argparsebuilder.ArgParseBuilder(*, parser_class=None, **kwargs)[source]

Builder for an ArgumentParser.

Takes the same kwargs as ArgumentParser, but allow_abbrev defaults to False, add_help is ignored and always False, and from prefix_chars the first character is used for all options/flags.

All methods except finish() return self.

Parameters
set_description(descr)[source]

Set the description attribute of the parser.

Parameters

descr (str) – description string

set_epilog(epilog)[source]

Set the epilog attribute of the parser.

Parameters

epilog (str) – epilog string

add_help(short='h', long='help', *, help='show this help message and exit')[source]

Add help.

Parameters
  • short (str) – short option

  • long (str) – long option

  • help (str) – help text

add_version(short='V', long='version', *, version=None, string=None, help='show version and exit')[source]

Add version.

If string is set it will be printed, else if version is set the resulting string will be prog + ' ' + version.

Parameters
  • short (str) – short option

  • long (str) – long option

  • version (string or tuple) – version; if it is a tuple it will be converted to a string

  • string – version string

  • help (str) – help text

add_flag(short=None, long=None, *, count=False, dest=None, const=None, help=None)[source]

Add flag.

Parameters
  • short (str) – short option

  • long (str) – long option

  • count (bool) – if True count the number of times the flag occurs

  • dest (str) – name of the list to which the values will be appended; ignored if count is set

  • const – only used if dest is set; defaults to the flag name

  • help (str) – help text

If long ends with |no, a negative flag will be added, i.e. for a flag --foo there will be a flag --no-foo. In this case count and dest will be ignored.

add_opt(short=None, long=None, *, type=None, nargs=None, default=None, const=None, choices=None, required=False, multiple=False, metavar=None, help=None)[source]

Add option.

Parameters
add_pos(name, *, type=None, nargs=None, default=None, choices=None, metavar=None, help=None)[source]

Add positional argument.

Parameters
add_mutually_exclusive_group(required=False)[source]

Add mutually exclusive group.

See: argparse.ArgumentParser.add_mutually_exclusive_group()

finish_mutually_exclusive_group()[source]

Finish mutually exclusive group.

Adding a new mutually exclusive or argument group or a subcommand will finish a current mutually exclusive group implicitly.

add_argument_group(title=None, description=None)[source]

Add argument group.

See: argparse.ArgumentParser.add_argument_group()

finish_argument_group()[source]

Finish argument group.

Adding a new argument group or a subcommand will finish a current argument group implicitly.

set_subcommands_attrs(**kwargs)[source]

Set attributes for subcommands.

If used at all, it must be called before first call to add_subcommand().

Parameters

kwargs – same arguments as for add_subparsers()

add_subcommand(name, *, func=None, **kwargs)[source]

Add subcommand.

Parameters
  • name (str) – name of the subcommand; will be available in the result after argument parsing under the name subcommand_name

  • func (callable) – can be called when the subcommand is invoked; will be available in the result after argument parsing under the name subcommand_func

  • kwargs – same arguments as for the method add_parser() which is decribed in the documentation of add_subparsers()

finish_subcommand()[source]

Finish subcommand.

Adding a new subcommand will finish a current subcommand implicitly.

finish()[source]

Finish the builder and return the argument parser.