Skip to content

Commit

Permalink
Adapt to '--ros-args ... [--]'-based ROS args extraction (#405)
Browse files Browse the repository at this point in the history
* Use --ros-args in args related tests.

Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>

* Prepend Node CLI args with --ros-args flags.

Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>

* Document implicit --ros-args flag in Node CLI args.

Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>

* Only add implicit --ros-args flag if not present already.

Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
  • Loading branch information
hidmic authored Aug 7, 2019
1 parent 0bb0a48 commit 29e4dcd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion rclpy/rclpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion rclpy/rclpy/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion rclpy/test/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion rclpy/test/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 29e4dcd

Please sign in to comment.