coma.hooks.parser_hook

Parser hook factories and defaults.

add_argument_factory(*names_or_flags: str, **kwargs: Any) Callable[[T], T | None][source]

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

Essentially, creates and returns a hook function as a lightweight wrapper around ArgumentParser.add_argument() called on the current value of the coma.hooks.base.ParserData.parser object with the given names_or_flags and kwargs.

Note

The value of parser is assumed to be the sub-parser attached to the command currently being executed. Adding arguments to the global parser should be done directly on the parser object passed to wake().

Example

Add a command line flag specifying how many lines the command should read:

coma.command(..., parser_hook=argument_factory('-l', '--lines', type=int))
Parameters:
  • *names_or_flags (str) – Passed to add_argument().

  • **kwargs (Any) – Passed to add_argument().

Returns:

A hook with parser_hook semantics.

Return type:

Hook

default_factory(*config_ids: str) Callable[[T], T | None][source]

Factory for creating a parser hook that adds a file path argument for each given ConfigID via ArgumentParser.add_argument().

Equivalent to calling add_path_argument() for each ConfigID in config_ids with default parameters.

Note

If config_ids is empty, defaults to all registered configs for the command being executed. In other words, only specify config_ids explicitly to limit the factory to only those configs.

Note

Any config identifier in config_ids corresponding to a config where is_serializable() is False is skipped, as these can never be initialized from or serialized to a file.

Example

Add a file path argument only for main_cfg and not no_path_cfg:

@dataclass
class MainConfig:
    ...

@coma.command(parser_hook=default_factory("main_cfg"))
def my_cmd(main_cfg: MainConfig, no_path_cfg: dict):
    ...
Parameters:

*config_ids (ConfigID) – Configs for which to create a file path argument parser hook. If empty, do so for all configs registered with the command currently being executed.

Returns:

A hook with parser_hook semantics.

Return type:

Hook