define_modes¶
-
pyscripts.define_modes(parser, modes, call_name='func', defaults=None, apply_to_parsers=None)[source]¶ Build subparsers in the given parser, from the given set of callables (modes) to run. The subparsers will have the name of the callables, and their description is taken directly from their docstrings. One can retrieve the single parser (per callable) as:
>>> parser = argparse.ArgumentParser() >>> subparsers = make_subparsers(parser, [func_1, func2]) >>> parser_func_1 = subparsers.choices['func_1'] >>> parser_func_2 = subparsers.choices['func_2']
By default, “func” will be the name associated to the callable execution, so after typing
>>> parser = argparse.ArgumentParser() >>> make_subparsers(parser, [func_1, func2]) >>> args = parser.parse_args()
then “args.func” will be the callable to call (func_1 or func_2, in this case). The name of this attribute can be changed via “arg_mode”. The stored function is not directly “func_1” or “func_2”, but a simple wrapper around these functions which allows to give an instance of argparse.Namespace as the only argument. The wrapper will transform it into a dictionary, expanding its values and passing them to the desired functions.
Parameters: - parser (argparse.ArgumentParser) – parser where to add the subparsers.
- modes (collection(callable)) – collection of callables (modes) to add.
- call_name (str) – name for the callable callable after parsing the arguments.
- defaults (dict or None) – default arguments for the parsers. The name specified in “call_name” is reserved.
- apply_to_parsers (callable or None) – callable to call on each parser. It must take a parser as the only argument.
Returns: collection of subparsers.
Return type: argparse._SubParsersAction