class ArgumentParser(argparse.ArgumentParser): (source)
Constructor: ArgumentParser(*args, **kwargs)
Drop-in replacement for argparse.ArgumentParser that adds support for
environment variables and .ini or .yaml-style config files.
| Method | __init__ |
Supports args of the argparse.ArgumentParser constructor as **kwargs, as well as the following additional args. |
| Method | convert |
Converts a config file or env var key + value to a list of commandline args to append to the commandline. |
| Method | format |
Undocumented |
| Method | format |
Returns a string with all args and settings and where they came from (eg. commandline, config file, environment variable or default) |
| Method | get |
Compute a commandline arg key to be used for a config file setting that doesn't correspond to any defined configargparse arg (and so doesn't have a user-specified commandline arg key). |
| Method | get |
Converts the given settings back to a dictionary that can be passed to ConfigFormatParser.serialize(..). |
| Method | get |
This method decides which actions can be set in a config file and what their keys will be. It returns a list of 0 or more config keys that can be used to set the given action's value in a config file. |
| Method | get |
If called after parse_args() or parse_known_args(), returns a dict that contains up to 4 keys corresponding to where a given option's value is coming from: - "command_line" - "environment_variables" - "config_file" - "defaults" Each such key, will be mapped to another dictionary containing the options set via that method... |
| Method | parse |
Supports all the same args as the argparse.ArgumentParser.parse_args(), as well as the following additional args. |
| Method | parse |
Supports all the same args as the argparse.ArgumentParser.parse_args(), as well as the following additional args. |
| Method | print |
Prints the format_values() string (to sys.stdout or another file). |
| Method | write |
Write the given settings to output files. |
| Instance Variable | exit |
Undocumented |
| Method | _could |
Check whether a command line arg could set the given option. |
| Method | _find |
Find the right index to insert config/env var args into the command line. |
| Method | _find |
Find where the subcommand is on the command line, if there is one. |
| Method | _names |
Check whether a command line arg would set a write-out-config-file arg. |
| Method | _open |
Tries to parse config file path(s) from within command_line_args. Returns a list of opened config files, including files specified on the commandline as well as any default_config_files specified in the constructor that are present on disk. |
| Method | _option |
Find the option strings that, when already on the command line, mean an env var or config file value for the given action should be dropped. |
| Method | _reachable |
Find this parser and every subparser these args could be handed to. |
| Method | _reject |
Reject synthesized command line args that set a write-out-config-file arg. |
| Method | _write |
Find the write-out-config-file args the given parsers define. |
| Instance Variable | _add |
Undocumented |
| Instance Variable | _add |
Undocumented |
| Instance Variable | _auto |
Undocumented |
| Instance Variable | _config |
Undocumented |
| Instance Variable | _config |
Undocumented |
| Instance Variable | _default |
Undocumented |
| Instance Variable | _ignore |
Undocumented |
| Instance Variable | _source |
Undocumented |
| Instance Variable | _write |
Undocumented |
Inherited from ArgumentParser:
| Method | add |
Undocumented |
| Method | convert |
Undocumented |
| Method | error |
error(message: string) |
| Method | exit |
Undocumented |
| Method | format |
Undocumented |
| Method | parse |
Undocumented |
| Method | parse |
Undocumented |
| Method | print |
Undocumented |
| Method | print |
Undocumented |
| Instance Variable | add |
Undocumented |
| Instance Variable | allow |
Undocumented |
| Instance Variable | epilog |
Undocumented |
| Instance Variable | formatter |
Undocumented |
| Instance Variable | fromfile |
Undocumented |
| Instance Variable | prog |
Undocumented |
| Instance Variable | usage |
Undocumented |
| Method | _add |
Undocumented |
| Method | _check |
Undocumented |
| Method | _get |
Undocumented |
| Method | _get |
Undocumented |
| Method | _get |
Undocumented |
| Method | _get |
Undocumented |
| Method | _get |
Undocumented |
| Method | _get |
Undocumented |
| Method | _get |
Undocumented |
| Method | _get |
Undocumented |
| Method | _match |
Undocumented |
| Method | _match |
Undocumented |
| Method | _parse |
Undocumented |
| Method | _parse |
Undocumented |
| Method | _print |
Undocumented |
| Method | _read |
Undocumented |
| Instance Variable | _optionals |
Undocumented |
| Instance Variable | _positionals |
Undocumented |
| Instance Variable | _subparsers |
Undocumented |
Inherited from _AttributeHolder (via ArgumentParser):
| Method | __repr__ |
Undocumented |
| Method | _get |
Undocumented |
Inherited from _ActionsContainer (via ArgumentParser, _AttributeHolder):
| Method | add |
add_argument(dest, ..., name=value, ...) add_argument(option_string, option_string, ..., name=value, ...) |
| Method | add |
Undocumented |
| Method | add |
Undocumented |
| Method | get |
Undocumented |
| Method | register |
Undocumented |
| Method | set |
Undocumented |
| Instance Variable | argument |
Undocumented |
| Instance Variable | conflict |
Undocumented |
| Instance Variable | description |
Undocumented |
| Instance Variable | prefix |
Undocumented |
| Method | _add |
Undocumented |
| Method | _check |
Undocumented |
| Method | _get |
Undocumented |
| Method | _get |
Undocumented |
| Method | _get |
Undocumented |
| Method | _handle |
Undocumented |
| Method | _handle |
Undocumented |
| Method | _pop |
Undocumented |
| Method | _registry |
Undocumented |
| Method | _remove |
Undocumented |
| Instance Variable | _action |
Undocumented |
| Instance Variable | _actions |
Undocumented |
| Instance Variable | _defaults |
Undocumented |
| Instance Variable | _has |
Undocumented |
| Instance Variable | _mutually |
Undocumented |
| Instance Variable | _negative |
Undocumented |
| Instance Variable | _option |
Undocumented |
| Instance Variable | _registries |
Undocumented |
argparse.ArgumentParser.__init__Supports args of the argparse.ArgumentParser constructor
as **kwargs, as well as the following additional args.
| Parameters | |
| *args | Undocumented |
| add | Whether to add a description of config file syntax to the help message. |
| add | Whether to add something to the help message for args that can be set through environment variables. |
| auto | If set to a string instead of None, all config- file-settable options will become also settable via environment variables whose names are this prefix followed by the config file key, all in upper case. (eg. setting this to foo_ will allow an arg like --my-arg to also be set via the FOO_MY_ARG environment variable) |
| default | When specified, this list of config files will be parsed in order, with the values from each config file taking precedence over previous ones. This allows an application to look for config files in multiple standard locations such as the install directory, home directory, and/or current directory. Also, shell * syntax can be used to specify all conf files in a directory. For example: ["/etc/conf/app_config.ini", "/etc/conf/conf-enabled/*.ini", "~/.my_app_config.ini", "./app_config.txt"] Path entries may be strings, os.PathLike objects (e.g. pathlib.Path), or zero-argument callable functions that return an open file-like object containing config file contents. Any provided callable is invoked each time the parser opens config files, and the returned stream is closed by the parser after parsing. The callable must return a stream. This is useful for sourcing config from non-filesystem locations such as in-memory buffers, secrets managers, or HTTP responses. |
| ignore | If true, settings that are found in a config file but don't correspond to any defined configargparse args will be ignored. If false, they will be processed and appended to the commandline like other args, and can be retrieved using parse_known_args() instead of parse_args() |
| config | function used to open a config file for reading or writing. Needs to return a file-like object. |
| config | configargparse.ConfigFileParser subclass which determines the config file format. configargparse comes with DefaultConfigFileParser and YAMLConfigFileParser. |
| args | A list of one or more command line args to be used for specifying the config file path (eg. ["-c", "--config-file"]). Default: [] |
| config | When args_for_setting_config_path is set, set this to True to always require users to provide a config path. |
| config | the help message to use for the args listed in args_for_setting_config_path. |
| args | A list of one or more command line args to use for specifying a config file output path. If provided, these args cause configargparse to write out a config file with settings based on the other provided commandline args, environment variants and defaults, and then to exit. (eg. ["-w", "--write-out-config-file"]). Default: [] These args can only be set on the command line. A config file key that names one is an error, and they can't have an env_var, since either would let whoever controls those overwrite an arbitrary file. |
| write | The help message to use for the args in args_for_writing_out_config_file. |
Converts a config file or env var key + value to a list of commandline args to append to the commandline.
| Parameters | |
| action | The argparse Action object for this setting, or None if this config file setting doesn't correspond to any defined configargparse arg. |
| key | string (config file key or env var name) |
| value | parsed value of type string or list |
| Returns | |
list[str] | args |
Returns a string with all args and settings and where they came from (eg. commandline, config file, environment variable or default)
| Returns | |
str | source to settings string |
Compute a commandline arg key to be used for a config file setting that doesn't correspond to any defined configargparse arg (and so doesn't have a user-specified commandline arg key).
| Parameters | |
| key | The config file key that was being set. |
| Returns | |
str | command line key |
Converts the given settings back to a dictionary that can be passed to ConfigFormatParser.serialize(..).
| Parameters | |
| source | the dictionary described in parse_known_args() |
| parsed | namespace object created within parse_known_args() |
| Returns | |
OrderedDict | where keys are strings and values are either strings or lists |
If called after parse_args() or parse_known_args(), returns a dict that contains up to 4 keys corresponding
to where a given option's value is coming from:
- "command_line"
- "environment_variables"
- "config_file"
- "defaults"
Each such key, will be mapped to another dictionary containing the options set via that method. Here the key
will be the option name, and the value will be a 2-tuple of the form (argparse.Action obj, str value).
| Returns | |
dict[str, dict[str, tuple[argparse.Action, str]]] | source to settings dict |
argparse.ArgumentParser.parse_argsSupports all the same args as the argparse.ArgumentParser.parse_args(),
as well as the following additional args.
| Parameters | |
| args | a list of args as in argparse, or a string (eg. "-x -y bla") |
| namespace | Undocumented |
| config | String. Used for testing. |
| env | Dictionary. Used for testing. |
| Returns | |
argparse.Namespace | namespace |
argparse.ArgumentParser.parse_known_argsSupports all the same args as the argparse.ArgumentParser.parse_args(),
as well as the following additional args.
| Parameters | |
| args | a list of args as in argparse, or a string (eg. "-x -y bla") |
| namespace | Undocumented |
configstr | |
envdict | |
ignorebool | This flag determines behavior when user specifies --help or -h. If False, it will have the default behavior - printing help and exiting. If True, it won't do either. |
| Returns | |
tuple[argparse.Namespace, list[str]] | tuple namescpace, unknown_args |
Write the given settings to output files.
| Parameters | |
| parsed | namespace object created within parse_known_args() |
| output | any number of file paths to write the config to |
| exit | whether to exit the program after writing the config files |
Check whether a command line arg could set the given option.
This covers every form argparse accepts: the option on its own, with its value attached after an = (or, for a short option, straight after it), bundled with other short options, and abbreviated. Where argparse would need to look at the rest of the command line to decide, this says yes, since it is used to reject args that must not reach an option at all, and being wrong in that direction only costs an error message.
| Parameters | |
| arg | a single command line arg. |
| option | an option string of the option to look for. |
| Returns | |
bool | whether the arg could set the option |
Find the right index to insert config/env var args into the command line.
Inserts before the -- separator if present, before a subparser command so the parent parser sees injected args first, before the first optional arg, or at position 0 when a REMAINDER positional exists. Falls back to appending.
Check whether a command line arg would set a write-out-config-file arg.
| Parameters | |
| arg | a single command line arg. |
| parsers | the parsers that could end up parsing it. |
| Returns | |
bool | whether it names a write-out-config-file arg |
Tries to parse config file path(s) from within command_line_args. Returns a list of opened config files, including files specified on the commandline as well as any default_config_files specified in the constructor that are present on disk.
| Parameters | |
| command | List of all args |
| Returns | |
list[tuple[io.IOBase, str]] | list of (stream, source_label) pairs. The source_label is unique per entry (file path for path entries; "<entry_label>[<index>]" for callable entries), so different entries cannot collapse into a single source key in format_values(). |
Find the option strings that, when already on the command line, mean an env var or config file value for the given action should be dropped.
These are the action's own option strings plus those of every other action in the same mutually exclusive group, mirroring the conflicts argparse checks for. Without the latter, a config file value for one member of a group would clash with a different member given on the command line instead of being overridden by it (see issue #164).
| Parameters | |
| action | the argparse.Action to find overriding option strings for. |
| Returns | |
list[str] | the option strings |
Find this parser and every subparser these args could be handed to.
A subparser is given the args this parser has put together and treats them as its own command line, and argparse parses it into this parser's namespace, so both what a subparser accepts and what it stores matter here. Subparsers that aren't ConfigArgParse parsers are walked through as well, since one of them can hold a subparser that is.
| Returns | |
list[argparse.ArgumentParser] | this parser and its subparsers |
Reject synthesized command line args that set a write-out-config-file arg.
Config file entries and environment variables are turned into command line args and then parsed like any others, so either one's key or its value can end up naming an arg. A write-out-config-file arg overwrites the path it is given and then exits the program, so only the real command line may set one: otherwise whoever controls a config file the program reads could destroy an arbitrary file without the user passing any args at all. (This is also why get_possible_config_keys() keeps these args out of the keys a config file can set.)
These args are checked against every parser reachable from this one, not just the one that looks like it will parse them. Which subcommand runs can't be told from the args: an option's value reads exactly like a subcommand name, and the synthesized args can name a subcommand themselves. So a config file may not name a write-out arg anywhere in the parser tree, which costs an error on a key named after one on a subcommand that wasn't going to run.
| Parameters | |
| synthesized | command line args built from a config file or from environment variables, before they are added to the real ones. |
| source | where they came from, for the error message. |
| parsers | the parsers that could end up parsing them. |
| Returns | |
bool | whether the args may be used. error() is meant to stop the program, but a program can override exit(), so this says so rather than counting on the call not returning. |
Find the write-out-config-file args the given parsers define.
| Parameters | |
| parsers | the parsers to look in. |
| Returns | |
list | each such arg as an (argparse.ArgumentParser, argparse.Action) pair, since it's the parser an arg belongs to that decides which command line args can set it. |