def add_argument(self, *args, **kwargs):
"""
This method supports the same args as ArgumentParser.add_argument(..)
as well as the additional args below.
Additional Args:
env_var: If set, the value of this environment variable will override
any config file or default values for this arg (but can itself
be overriden on the commandline). Also, if auto_env_var_prefix is
set in the constructor, this env var name will be used instead of
the automatic name.
is_config_file_arg: If True, this arg is treated as a config file path
This provides an alternative way to specify config files in place of
the ArgumentParser(fromfile_prefix_chars=..) mechanism.
Default: False
is_write_out_config_file_arg: If True, this arg will be treated as a
config file path, and, when it is specified, will cause
configargparse to write all current commandline args to this file
as config options and then exit.
Default: False
"""
env_var = kwargs.pop("env_var", None)
is_config_file_arg = kwargs.pop(
"is_config_file_arg", None) or kwargs.pop(
"is_config_file", None) # for backward compat.
is_write_out_config_file_arg = kwargs.pop(
"is_write_out_config_file_arg", None)
action = self.original_add_argument_method(*args, **kwargs)
action.is_positional_arg = not action.option_strings
action.env_var = env_var
action.is_config_file_arg = is_config_file_arg
action.is_write_out_config_file_arg = is_write_out_config_file_arg
if action.is_positional_arg and env_var:
raise ValueError("env_var can't be set for a positional arg.")
if action.is_config_file_arg and not isinstance(action, argparse._StoreAction):
raise ValueError("arg with is_config_file_arg=True must have "
"action='store'")
if action.is_write_out_config_file_arg:
error_prefix = "arg with is_write_out_config_file_arg=True "
if not isinstance(action, argparse._StoreAction):
raise ValueError(error_prefix + "must have action='store'")
if is_config_file_arg:
raise ValueError(error_prefix + "can't also have "
"is_config_file_arg=True")
return action
评论列表
文章目录