Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to '--ros-args ... [--]'-based ROS args extraction #816

Merged
merged 7 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rclcpp/include/rclcpp/node_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ class NodeOptions
/// Set the arguments, return this for parameter idiom.
/**
* These arguments are used to extract remappings used by the node and other
* settings.
* settings. Being specific to a ROS node, an implicit `--ros-args` scope flag
* always precedes these arguments.
*
* This will cause the internal rcl_node_options_t struct to be invalidated.
*/
Expand Down
23 changes: 17 additions & 6 deletions rclcpp/src/rclcpp/node_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,23 @@ NodeOptions::get_rcl_node_options() const
node_options_->use_global_arguments = this->use_global_arguments_;
node_options_->domain_id = this->get_domain_id_from_env();

std::unique_ptr<const char *[]> c_args;
int c_argc = 0;
std::unique_ptr<const char *[]> c_argv;
if (!this->arguments_.empty()) {
c_args.reset(new const char *[this->arguments_.size()]);
for (std::size_t i = 0; i < this->arguments_.size(); ++i) {
c_args[i] = this->arguments_[i].c_str();
auto it = std::find(
this->arguments_.cbegin(), this->arguments_.cend(), RCL_ROS_ARGS_FLAG);

c_argc = static_cast<int>(this->arguments_.size());
if (it == this->arguments_.cend()) c_argc += 1;
wjwwood marked this conversation as resolved.
Show resolved Hide resolved

c_argv.reset(new const char *[c_argc]);

std::size_t i = 0;
if (it == this->arguments_.cend()) {
c_argv[i++] = RCL_ROS_ARGS_FLAG;
}
for (std::size_t j = 0; j < this->arguments_.size(); ++i, ++j) {
c_argv[i] = this->arguments_[j].c_str();
}
}

Expand All @@ -103,8 +115,7 @@ NodeOptions::get_rcl_node_options() const
}

rmw_ret_t ret = rcl_parse_arguments(
static_cast<int>(this->arguments_.size()), c_args.get(), this->allocator_,
&(node_options_->arguments));
c_argc, c_argv.get(), this->allocator_, &(node_options_->arguments));

if (RCL_RET_OK != ret) {
throw_from_rcl_error(ret, "failed to parse arguments");
Expand Down
4 changes: 2 additions & 2 deletions rclcpp/test/test_node_global_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class TestNodeWithGlobalArgs : public ::testing::Test
protected:
static void SetUpTestCase()
{
const char * const args[] = {"proc", "__node:=global_node_name"};
rclcpp::init(2, args);
const char * const args[] = {"proc", "--ros-args", "__node:=global_node_name"};
rclcpp::init(3, args);
}
};

Expand Down
6 changes: 4 additions & 2 deletions rclcpp/test/test_utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
#include "rclcpp/utilities.hpp"

TEST(TestUtilities, remove_ros_arguments) {
const char * const argv[] = {"process_name", "-d", "__ns:=/foo/bar",
"__ns:=/fiz/buz", "--foo=bar", "--baz"};
const char * const argv[] = {
"process_name", "-d", "--ros-args", "__ns:=/foo/bar", "__ns:=/fiz/buz", "--",
"--foo=bar", "--baz"
};
int argc = sizeof(argv) / sizeof(const char *);
auto args = rclcpp::remove_ros_arguments(argc, argv);

Expand Down