Command Line Arguments
If desired, your stage can add its own command line arguments. This will be picked up
automatically if using the main_offboard
or main_onboard
modules.
Do this by populating the add_arguments
virtual API in the Stage
class. For example:
class ArgumentStage : public Stage
{
public:
/// Constructor. Does nothing.
ArgumentStage() : Stage("ArgumentStage")
{
}
/// Adds arguments to the command line parser.
void add_arguments(core::ArgumentParser &parser) const override
{
auto &group = parser.add_option_group("My Group");
group.add_option("test").help("Set this to enable testing.");
}
/// Handles if arguments are set or not.
void initialize(StageInterface &interface)
{
if (auto arguments = interface.parsed_arguments())
{
LINFO("Test count is {}", arguments->count("test");
}
}
};
Always make sure parsed_arguments
is set in your initialize
method; there are conditions
in which you can run a pipelien without processing arguments, and it will be a nullptr
in those
cases.
As mentioned previously, if using main_offboard
or main_onboard
, these arguments
will be automatically handled.
If not, you need to do a little work on your side. If you don’t have any other command line parser, you can handle ‘parsing’ arguments in the pipeline like so:
Pipeline pipeline;
pipeline.add_stage<ArgumentStage>();
int argc = 2;
const char *argv[3] = {"test", "--test", nullptr};
pipeline.parse_arguments(argc, argv);
This will generate a parser and parse the arguments for you, returning the result of the parse if desired. All stages will also have access to the parsed arguments.
If you want to use a custom parser, you can also provide that as the first argument
to the parse_arguments
API.