daniele77/cli

Flags for commands?

pkbehera opened this issue · 1 comments

Is there support for flags on the command line?

auto root_menu_p = make_unique<cli::Menu>("demo");

root_menu_p->Insert("reboot", [&](std::ostream& out, uint32_t delay_s, bool force) {
// blah
// blah
}, "Reboots after given delay(seconds)",   {"delay(seconds)", "force"});

With the above handler in the place, I was expecting the following command to work! :)

demo> reboot 5 --force
wrong command: reboot 5 --force

Is it possible to get this working somehow?

This library is designed for interactive shell use, not parsing command-line arguments. That's why it does not support "flags" in that style.

With the example you provide, you can use the following commands:

demo> reboot 5 true
demo> reboot 5 false
demo> reboot 5 1
demo> reboot 5 0

Of course, you can achieve the functionality you described by using the following code snippet:

root_menu_p->Insert("reboot", [&](std::ostream& out, uint32_t delay_s, std::string force) {
    if (force == "--force")
        // blah
    else if (force.empty())
       // blah
    else
        out << "Wrong parameter\n";
}, "Reboots after given delay(seconds)",   {"delay(seconds)", "force"});