Skip to content

Instantly share code, notes, and snippets.

@mfehr
Created February 7, 2019 18:32
Show Gist options
  • Save mfehr/18762943fb6af9710a5583f439dd9aff to your computer and use it in GitHub Desktop.
Save mfehr/18762943fb6af9710a5583f439dd9aff to your computer and use it in GitHub Desktop.
Directly parse and set Gflags from ROS params.
// NOTE(mfehr): program_name can be obtained through argv[0].
void parseGflagsFromRosParams(
const char* program_name, const ros::NodeHandle& nh_private) {
CHECK_NOTNULL(program_name);
std::vector<google::CommandLineFlagInfo> flags;
google::GetAllFlags(&flags);
VLOG(1) << "Parsing gflags from ROS params...";
std::stringstream ss;
for (google::CommandLineFlagInfo& flag : flags) {
if (flag.type == "int32" || flag.type == "uint64" || flag.type == "int64") {
int32_t ros_param_value;
if (nh_private.getParam(flag.name, ros_param_value)) {
ss << "--" << flag.name << "=" << ros_param_value << std::endl;
}
} else if (flag.type == "bool") {
bool ros_param_value;
if (nh_private.getParam(flag.name, ros_param_value)) {
ss << "--" << flag.name << "=" << ros_param_value << std::endl;
}
} else if (flag.type == "string") {
std::string ros_param_value;
if (nh_private.getParam(flag.name, ros_param_value)) {
ss << "--" << flag.name << "=" << ros_param_value << std::endl;
}
} else if (flag.type == "double") {
double ros_param_value;
if (nh_private.getParam(flag.name, ros_param_value)) {
ss << "--" << flag.name << "=" << ros_param_value << std::endl;
}
} else {
LOG(ERROR) << "Cannot parse gflag '" << flag.name
<< "' from ROS params, type " << flag.type
<< " is not supported!";
}
}
const std::string ros_param_gflag_file = ss.str();
CHECK(google::ReadFlagsFromString(
ros_param_gflag_file, program_name, false /*errors_are_fatal*/));
VLOG(1) << "\n\nThe following Gflags have been set using ROS params:\n"
<< ros_param_gflag_file << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment