osrf_pycommon.cli_utils.verb_pattern module
API for implementing commands and verbs which used the verb pattern.
- osrf_pycommon.cli_utils.verb_pattern.call_prepare_arguments(func, parser, sysargs=None)
Call a prepare_arguments function with the correct number of parameters.
The
prepare_argumentsfunction of a verb can either take one parameter,parser, or two parametersparserandargs, whereargsare the current arguments being processed.- Parameters:
func (Callable) – Callable
prepare_argumentsfunction.parser (
argparse.ArgumentParser) – parser which is always passed to the functionsysargs (list) – arguments to optionally pass to the function, if needed
- Returns:
return value of function or the parser if the function returns None.
- Return type:
argparse.ArgumentParser- Raises:
ValueError if a function with the wrong number of parameters is given
- osrf_pycommon.cli_utils.verb_pattern.create_subparsers(parser, cmd_name, verbs, group, sysargs, title=None)
Creates argparse subparsers for each verb which can be discovered.
Using the
verbsparameter, the available verbs are iterated through. For each verb a subparser is created for it using theparserparameter. Thecmd_nameis used to fill the title and description of theadd_subparsersfunction call. Thegroupparameter is used with each verb to load the verb’sdescription,prepare_argumentsfunction, and the verb’sargument_preprocessorsif available. Each verb’sprepare_argumentsfunction is called, allowing them to add arguments. Finally a list ofargument_preprocessorsfunctions and verb subparsers are returned, one for each verb.- Parameters:
parser (
argparse.ArgumentParser) – parser for this commandcmd_name (str) – name of the command to which the verbs are being added
verbs (list) – list of verbs (by name as a string)
group (str) – name of the
entry_pointgroup for the verbssysargs (list) – list of system arguments
title (str) – optional custom title for the command
- Returns:
tuple of argument_preprocessors and verb subparsers
- Return type:
tuple
- osrf_pycommon.cli_utils.verb_pattern.default_argument_preprocessor(args)
Return unmodified args and an empty dict for extras
- osrf_pycommon.cli_utils.verb_pattern.list_verbs(group)
List verbs available for a given
entry_pointgroup.- Parameters:
group (str) –
entry_pointgroup name for the verbs to list- Returns:
list of verb names for the given
entry_pointgroup- Return type:
list of str
- osrf_pycommon.cli_utils.verb_pattern.load_verb_description(verb_name, group)
Load description of a verb in a given group by name.
- Parameters:
verb_name (str) – name of the verb to load, as a string
group (str) –
entry_pointgroup name which the verb is in
- Returns:
verb description
- Return type:
dict
- osrf_pycommon.cli_utils.verb_pattern.split_arguments_by_verb(arguments)
Split arguments by verb.
Given a list of arguments (list of strings), the verb, the pre verb arguments, and the post verb arguments are returned.
For example:
>>> args = ['--command-arg1', 'verb', '--verb-arg1', '--verb-arg2'] >>> split_arguments_by_verb(args) ('verb', ['--command-arg1'], ['--verb-arg1', '--verb-arg2'])
- Parameters:
arguments (list) – list of system arguments
- Returns:
the verb (str), pre verb args (list), and post verb args (list)
- Return type:
tuple