CLI11 icon indicating copy to clipboard operation
CLI11 copied to clipboard

Can I add option in a callback function?

Open liuqi123123 opened this issue 2 years ago • 2 comments

Can I add option in a callback function? such as : CLI::App app{"argparse"}; parameter = app.add_subcommand("parameter", "")->parse_complete_callback([&](){ std::string path; parameter->add_option("--path", path, "the absolute path of files"); }); CLI11_PARSE(app, argc, argv); I have try this, but the added option is ineffective! I have this question because I met a condition when I need know what subcommand has been add before I add option to this subcommand. thanks for your answer!

liuqi123123 avatar Aug 31 '23 02:08 liuqi123123

yes it is possible, though wouldn't necessarily be what I would recommend. See https://github.com/CLIUtils/CLI11#callbacks for information on the callbacks available.

The way you have it written above the callback won't be executed until parsing for the subcommand is complete, Meaning there will be no chance for that new option to get called since it will already completed the parsing stage. You would need to add it in the pre_parse_callback.

Also in your above example the path variable is local, so may cause a memory error if it were ever used.

Some other options are the TriggerOn and TriggerOff helper functions which can trigger a group of options to turn on or off based on the execution of another option_group/subcommand.

If you really need adaptive option generation, be careful of the memory and you will likely need to rely on the pre_parse_callback for option_groups/subcommands and ->trigger_on_parse() modifier for options

phlptp avatar Sep 01 '23 14:09 phlptp

thanks for your reply!I will try later!

liuqi123123 avatar Sep 06 '23 02:09 liuqi123123

going to close this as no response for 6+ months

phlptp avatar May 10 '24 22:05 phlptp