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_arguments function of a verb can either take one parameter, parser, or two parameters parser and args, where args are the current arguments being processed.

Parameters:
  • func (Callable) – Callable prepare_arguments function.

  • parser (argparse.ArgumentParser) – parser which is always passed to the function

  • sysargs (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 verbs parameter, the available verbs are iterated through. For each verb a subparser is created for it using the parser parameter. The cmd_name is used to fill the title and description of the add_subparsers function call. The group parameter is used with each verb to load the verb’s description, prepare_arguments function, and the verb’s argument_preprocessors if available. Each verb’s prepare_arguments function is called, allowing them to add arguments. Finally a list of argument_preprocessors functions and verb subparsers are returned, one for each verb.

Parameters:
  • parser (argparse.ArgumentParser) – parser for this command

  • cmd_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_point group for the verbs

  • sysargs (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_point group.

Parameters:

group (str) – entry_point group name for the verbs to list

Returns:

list of verb names for the given entry_point group

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_point group 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