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
argparseargument.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.parserobject with the givennames_or_flagsandkwargs.Note
The value of
parseris assumed to be the sub-parser attached to the command currently being executed. Adding arguments to the global parser should be done directly on theparserobject passed towake().Example
Add a command line flag specifying how many lines the command should read:
coma.command(..., parser_hook=argument_factory('-l', '--lines', type=int))
- 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
ConfigIDvia ArgumentParser.add_argument().Equivalent to calling
add_path_argument()for eachConfigIDinconfig_idswith default parameters.Note
If
config_idsis empty, defaults to all registered configs for the command being executed. In other words, only specifyconfig_idsexplicitly to limit the factory to only those configs.Note
Any config identifier in
config_idscorresponding to a config whereis_serializable()isFalseis skipped, as these can never be initialized from or serialized to a file.Example
Add a file path argument only for
main_cfgand notno_path_cfg:@dataclass class MainConfig: ... @coma.command(parser_hook=default_factory("main_cfg")) def my_cmd(main_cfg: MainConfig, no_path_cfg: dict): ...