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 withpartial()
does not have such an attribute and in that case a (ugly looking) name is created by applyingrepr()
to the function.This function is a convenient way to create a partial function and set its
__name__
and__doc__
attributes.- Parameters
name (str) – the name of the partial function
func – see
functools.partial()
doc (str) – documentation string for
__doc__
attributeargs – see
functools.partial()
kwargs – see
functools.partial()
- Returns
partial function object with
__name__
attribute set toname
-
class
argparsebuilder.
ArgParseBuilder
(*, parser_class=None, **kwargs)[source]¶ Builder for an
ArgumentParser
.Takes the same
kwargs
asArgumentParser
, butallow_abbrev
defaults toFalse
,add_help
is ignored and alwaysFalse
, and fromprefix_chars
the first character is used for all options/flags.All methods except
finish()
returnself
.- Parameters
parser_class (argparse.ArgumentParser) – the argument parser class; defaults to
ArgumentParser
kwargs – see
ArgumentParser
-
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_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 ifversion
is set the resulting string will beprog + ' ' + version
.
-
add_flag
(short=None, long=None, *, count=False, dest=None, const=None, help=None)[source]¶ Add flag.
- Parameters
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 casecount
anddest
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
short (str) – short option
long (str) – long option
type – see
add_argument()
nargs – see
add_argument()
default – see
add_argument()
const – see
add_argument()
choices – see
add_argument()
required – see
add_argument()
multiple (bool) – if
True
this option can be used multiple times and all values are appended to a listmetavar – see
add_argument()
help (str) – help text
-
add_pos
(name, *, type=None, nargs=None, default=None, choices=None, metavar=None, help=None)[source]¶ Add positional argument.
- Parameters
name (str) – name of argument
type – see
add_argument()
nargs – see
add_argument()
default – see
add_argument()
choices – see
add_argument()
metavar – see
add_argument()
help (str) – help text
-
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.
-
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 ofadd_subparsers()