From dfb3b8b6a620e9e29c969b99421858170d108b69 Mon Sep 17 00:00:00 2001 From: Brian Chen Date: Thu, 23 Jun 2022 12:15:40 -0700 Subject: [PATCH] multiple param set Signed-off-by: Brian Chen --- ros2param/ros2param/verb/set.py | 57 ++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/ros2param/ros2param/verb/set.py b/ros2param/ros2param/verb/set.py index 86c29fa06..5e1eccf09 100644 --- a/ros2param/ros2param/verb/set.py +++ b/ros2param/ros2param/verb/set.py @@ -37,14 +37,29 @@ def add_arguments(self, parser, cli_name): # noqa: D102 'node_name', help='Name of the ROS node') arg.completer = NodeNameCompleter( include_hidden_nodes_key='include_hidden_nodes') - parser.add_argument( - '--include-hidden-nodes', action='store_true', - help='Consider hidden nodes as well') + arg = parser.add_argument( - 'parameter_name', help='Name of the parameter') + 'parameters', nargs='*', + help='List of parameter name and value pairs i.e. "int_param 1 str_param hello_world"') arg.completer = ParameterNameCompleter() + parser.add_argument( - 'value', help='Value of the parameter') + '--include-hidden-nodes', action='store_true', + help='Consider hidden nodes as well') + + def build_parameters(self, params): + parameters = [] + if len(params) % 2: + raise RuntimeError('Must pass list of parameter name and value pairs') + + params = [(params[i], params[i+1]) for i in range(0, len(params), 2)] + for param_str in params: + parameter = Parameter() + parameter.name = param_str[0] + parameter.value = get_parameter_value(string_value=param_str[1]) + parameters.append(parameter) + + return parameters def main(self, *, args): # noqa: D102 with NodeStrategy(args) as node: @@ -56,23 +71,19 @@ def main(self, *, args): # noqa: D102 return 'Node not found' with DirectNode(args) as node: - parameter = Parameter() - Parameter.name = args.parameter_name - parameter.value = get_parameter_value(string_value=args.value) - + parameters = self.build_parameters(args.parameters) response = call_set_parameters( - node=node, node_name=args.node_name, parameters=[parameter]) + node=node, node_name=args.node_name, parameters=parameters) + results = response.results - # output response - assert len(response.results) == 1 - result = response.results[0] - if result.successful: - msg = 'Set parameter successful' - if result.reason: - msg += ': ' + result.reason - print(msg) - else: - msg = 'Setting parameter failed' - if result.reason: - msg += ': ' + result.reason - print(msg, file=sys.stderr) + for result in results: + if result.successful: + msg = 'Set parameter successful' + if result.reason: + msg += ': ' + result.reason + print(msg) + else: + msg = 'Setting parameter failed' + if result.reason: + msg += ': ' + result.reason + print(msg, file=sys.stderr)