From 29e4dcd52321a049a40febc01cb524b3854abe50 Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Wed, 7 Aug 2019 12:58:24 -0300 Subject: [PATCH] Adapt to '--ros-args ... [--]'-based ROS args extraction (#405) * Use --ros-args in args related tests. Signed-off-by: Michel Hidalgo * Prepend Node CLI args with --ros-args flags. Signed-off-by: Michel Hidalgo * Document implicit --ros-args flag in Node CLI args. Signed-off-by: Michel Hidalgo * Only add implicit --ros-args flag if not present already. Signed-off-by: Michel Hidalgo --- rclpy/rclpy/__init__.py | 3 ++- rclpy/rclpy/node.py | 7 ++++++- rclpy/test/test_node.py | 5 ++++- rclpy/test/test_utilities.py | 11 ++++++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/rclpy/rclpy/__init__.py b/rclpy/rclpy/__init__.py index f841dd74b..38f6cd406 100644 --- a/rclpy/rclpy/__init__.py +++ b/rclpy/rclpy/__init__.py @@ -121,7 +121,8 @@ def create_node( :param node_name: A name to give to the node. :param context: The context to associated with the node, or ``None`` for the default global context. - :param cli_args: Command line arguments to be used by the node. + :param cli_args: Command line arguments to be used by the node. Being specific to a ROS node, + an implicit `--ros-args` scope flag always precedes these arguments. :param namespace: The namespace prefix to apply to entities associated with the node (node name, topics, etc). :param use_global_arguments: ``False`` if the node should ignore process-wide command line diff --git a/rclpy/rclpy/node.py b/rclpy/rclpy/node.py index 74512e6f7..46c0afb67 100644 --- a/rclpy/rclpy/node.py +++ b/rclpy/rclpy/node.py @@ -115,6 +115,8 @@ def __init__( :param context: The context to be associated with, or ``None`` for the default global context. :param cli_args: A list of strings of command line args to be used only by this node. + Being specific to a ROS node, an implicit `--ros-args` scope flag always precedes + these arguments. :param namespace: The namespace to which relative topic and service names will be prefixed. Validated by :func:`validate_namespace`. :param use_global_arguments: ``False`` if the node should ignore process-wide command line @@ -144,12 +146,15 @@ def __init__( self._parameter_overrides = {} self._descriptors = {} + if cli_args is not None and '--ros-args' not in cli_args: + cli_args = ['--ros-args', *cli_args] namespace = namespace or '' if not self._context.ok(): raise NotInitializedException('cannot create node') try: self.__handle = Handle(_rclpy.rclpy_create_node( - node_name, namespace, self._context.handle, cli_args, use_global_arguments)) + node_name, namespace, self._context.handle, cli_args, use_global_arguments + )) except ValueError: # these will raise more specific errors if the name or namespace is bad validate_node_name(node_name) diff --git a/rclpy/test/test_node.py b/rclpy/test/test_node.py index 937337fe7..86aa94963 100644 --- a/rclpy/test/test_node.py +++ b/rclpy/test/test_node.py @@ -1529,7 +1529,10 @@ class TestCreateNode(unittest.TestCase): def test_use_global_arguments(self): context = rclpy.context.Context() - rclpy.init(args=['process_name', '__node:=global_node_name'], context=context) + rclpy.init( + args=['process_name', '--ros-args', '__node:=global_node_name'], + context=context + ) try: node1 = rclpy.create_node( 'my_node', namespace='/my_ns', use_global_arguments=True, context=context) diff --git a/rclpy/test/test_utilities.py b/rclpy/test/test_utilities.py index a285035a1..b73488f5f 100644 --- a/rclpy/test/test_utilities.py +++ b/rclpy/test/test_utilities.py @@ -21,7 +21,16 @@ class TestValidateRemoveRosArgs(unittest.TestCase): def test_remove_ros_args(self): - args = ['process_name', '-d', '__ns:=/foo/bar', '__ns:=/fiz/buz', '--foo=bar', '--baz'] + args = [ + 'process_name', + '-d', + '--ros-args', + '__ns:=/foo/bar', + '__ns:=/fiz/buz', + '--', + '--foo=bar', + '--baz' + ] stripped_args = rclpy.utilities.remove_ros_args(args=args) self.assertEqual(4, len(stripped_args)) self.assertEqual('process_name', stripped_args[0])