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. |
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. |
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 |
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 | exit |
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 |
**kwargs | 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 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"] |
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: [] |
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_args
Supports 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_args
Supports 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 |
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[IO] | open config files |