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

Filter ROS arguments before gflags parsing #453

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Changes from all 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
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
Loading