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
ArgumentParsercreates 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
kwargsasArgumentParser, butallow_abbrevdefaults toFalse,add_helpis ignored and alwaysFalse, and fromprefix_charsthe first character is used for all options/flags.All methods except
finish()returnself.- Parameters
parser_class (argparse.ArgumentParser) – the argument parser class; defaults to
ArgumentParserkwargs – see
ArgumentParser
-
set_description(descr)[source]¶ Set the
descriptionattribute of the parser.- Parameters
descr (str) – description string
-
set_epilog(epilog)[source]¶ Set the
epilogattribute 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
stringis set it will be printed, else ifversionis 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
longends with|no, a negative flag will be added, i.e. for a flag--foothere will be a flag--no-foo. In this casecountanddestwill 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
Truethis 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_namefunc (callable) – can be called when the subcommand is invoked; will be available in the result after argument parsing under the name
subcommand_funckwargs – same arguments as for the method
add_parser()which is decribed in the documentation ofadd_subparsers()