Skip to content

Commit

Permalink
Filter ROS arguments before gflags parsing (#453)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll authored Nov 3, 2023
1 parent 9b0e3a9 commit 161cbcf
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions ros_gz_sim/src/create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,32 @@ DEFINE_double(Y, 0, "Yaw component of initial orientation, in radians.");
// If these are not needed, just use the `gz service` command line instead.
int main(int _argc, char ** _argv)
{
rclcpp::init(_argc, _argv);
auto filtered_arguments = rclcpp::init_and_remove_ros_arguments(_argc, _argv);
auto ros2_node = rclcpp::Node::make_shared("ros_gz_sim");

// Construct a new argc/argv pair from the flags that weren't parsed by ROS
// Gflags wants a mutable pointer to argv, which is why we can't use a
// vector of strings here
int filtered_argc = filtered_arguments.size();
char ** filtered_argv = new char *[(filtered_argc + 1)];
for (int ii = 0; ii < filtered_argc; ++ii) {
filtered_argv[ii] = new char[filtered_arguments[ii].size() + 1];
snprintf(filtered_argv[ii], sizeof(filtered_argv), "%s", filtered_arguments[ii].c_str());
}
filtered_argv[filtered_argc] = nullptr;

gflags::AllowCommandLineReparsing();
gflags::SetUsageMessage(
R"(Usage: create -world [arg] [-file FILE] [-param PARAM] [-topic TOPIC]
[-string STRING] [-name NAME] [-allow_renaming RENAMING] [-x X] [-y Y] [-z Z]
[-R ROLL] [-P PITCH] [-Y YAW])");
gflags::ParseCommandLineFlags(&_argc, &_argv, true);
gflags::ParseCommandLineFlags(&filtered_argc, &filtered_argv, false);

// Free our temporary argc/argv pair
for (size_t ii = 0; filtered_argv[ii] != nullptr; ++ii) {
delete[] filtered_argv[ii];
}
delete[] filtered_argv;

// World
std::string world_name = FLAGS_world;
Expand Down

0 comments on commit 161cbcf

Please sign in to comment.