daniele77 / cli

A library for interactive command line interfaces in modern C++
Boost Software License 1.0
1.24k stars 140 forks source link

Flags for commands? #232

Closed pkbehera closed 1 month ago

pkbehera commented 8 months ago

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?

daniele77 commented 8 months ago

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"});