espnet2.utils.config_argparse.ArgumentParser
espnet2.utils.config_argparse.ArgumentParser
class espnet2.utils.config_argparse.ArgumentParser(*args, **kwargs)
Bases: ArgumentParser
Simple implementation of ArgumentParser supporting config file.
This class is originated from https://github.com/bw2/ConfigArgParse, but this class lacks some features that it has:
- Not supporting multiple config files.
- Automatically adding “–config” as an option.
- Not supporting any formats other than YAML.
- Not checking argument types.
config
Path to the YAML configuration file.
Type: str
Parameters:
- *args – Variable length argument list for the base ArgumentParser.
- **kwargs – Keyword arguments for the base ArgumentParser.
Returns: A Namespace object with parsed arguments.
Return type: Namespace
Raises:ArgumentError – If the configuration file does not exist or has invalid contents.
####### Examples
parser = ArgumentParser() parser.add_argument(’–example’, help=’An example argument.’) args = parser.parse_known_args()
If a YAML config file is specified with the –config argument, the parser will load the configurations from that file as well.
NOTE
The parser does not check argument types, meaning any type of value can be set regardless of the expected argument type.
parse_known_args(args=None, namespace=None)
Parse the command line arguments, including those from a config file.
This method extends the default parse_known_args to support loading configurations from a YAML file specified via the “–config” option. It first parses known arguments and then loads the configuration from the specified YAML file if it exists. The loaded configuration values are set as defaults for the argument parser.
- Parameters:
- args (list , optional) – The list of command line arguments to parse. If None, uses sys.argv[1:].
- namespace (argparse.Namespace , optional) – An optional namespace to populate with the parsed arguments. If None, a new namespace is created.
- Returns: A tuple containing: : - Namespace: An object containing the parsed arguments.
- list: Any remaining unrecognized command line arguments.
- Return type: tuple
- Raises:
- ArgumentError – If the specified config file does not exist or if
- the contents of the config file do not form a valid dictionary. –
- RuntimeError – If any keys in the config file do not correspond
- to recognized arguments. –
####### Examples
>>> parser = ArgumentParser()
>>> parser.add_argument('--foo', type=int)
>>> parser.add_argument('--bar', type=str)
>>> args, unknown = parser.parse_known_args(['--config', 'config.yaml'])
NOTE
The method ignores the “–config” argument when loading from the config file, and does not enforce type checking for the values set from the config file.