coma.hooks.parser_hook

Parser hook utilities, factories, and defaults.

factory(*names_or_flags, **kwargs) Callable[[...], None][source]

Factory for creating a parser hook that adds an argparse argument.

Creates a parser hook that add an argument to the ArgumentParser of the hook protocol.

Example:

coma.initiate(..., parser_hook=factory('-l', '--lines', type=int))
Parameters
Returns

A parser hook

single_config_factory(config_id: str, *names_or_flags, **kwargs) Callable[[...], None][source]

Factory for creating a parser hook that adds a single config file path argument.

If no arguments are provided, the following defaults are used for add_argument():

from coma.config import default_default, default_flag, default_help
names_or_flags = [default_flag(config_id)]
kwargs = {
    "type": str,
    "metavar": "FILE",
    "dest": default_dest(config_id)
    "default": default_default(config_id),
    "help": default_help(config_id),
}

Any of these defaults can be overridden by providing alternative arguments. Additional arguments beyond these can also be provided.

Example:

@dataclass
class Config:
    ...

cfg_id = default_id(Config)
parser_hook = single_config_factory(cfg_id, metavar=cfg_id.upper())
coma.register(..., parser_hook=parser_hook)
Parameters
Returns

A parser hook

multi_config(parser: argparse.ArgumentParser, configs: Dict[str, Any]) None[source]

Parser hook for adding all config file path arguments.

Equivalent to calling single_config_factory() for each config in configs.

Automatically adds file path arguments for all configs using parser.

Example:

@dataclass
class Config:
    ...

coma.initiate(..., parser_hook=multi_config)
Parameters
  • parser – The parser parameter of the parser hook protocol

  • configs – The configs parameter of the parser hook protocol

default(parser: argparse.ArgumentParser, configs: Dict[str, Any]) None

Default parser hook.

An alias for multi_config().