'CLI11 subcommands and option groups
I'm writing a small CLI program that will ultimately just print text when a user passes an IP address and port from the command line (linux terminal atm) - I'm new to command line parsers this is my first attempt at using one.
However, I'm having trouble understanding how to "connect" option groups and subcommands. I've researched alot before asking for help here (I've been going over documentation and examples for at least 48 hours). I'm not sure if it's my lack of knowledge in C++ or if the libraries documentation just assumes you know what you're doing, either way, I've had my ego stripped and I'm defeated already by this library.
What I'm looking to do is have the user of this tool specify an "option group" as a requirement, and then specify a subcommand as a requirement which takes an ip address and port, this would look like:
./program <option_group> <sub_command> <ip> <port>
The code below is what I currently have:
#include "CLI11.hpp"
#include <iostream>
#include <string>
int main(int argc, char **argv) {
CLI::App app("name of app");
app.set_help_all_flag("--help-all", "Expand all help");
auto liner_group = app.add_option_group("One liners", "reverse shells");
bool reverse_shell{false};
bool bind_shell{false};
liner_group->add_flag("-r, --reverse", reverse_shell, "Generate a reverse shell");
liner_group->add_flag("-b, --bind", bind_shell, "Generate a bind shell");
liner_group->require_option(1);
std::string ip;
std::string port;
auto bash = app.add_subcommand("bash", "bash reverse shell");
bash->add_option("-l, --lhost", ip, "enter ip address");
bash->add_option("-p, --port", port, "enter a port");
bash->callback([&]() { std::cout << ip << " " << port; });
CLI11_PARSE(app, argc, argv);
return 0;
}
So I'm using callback([&]() in the subcommand to take the ip and port and print the text to the terminal, but the option group basically does nothing except require you to choose an option group.
I would like to choose an option group (e.g -r), and then require a user to specify a subcommand after the option group (e.g -r subcommand). I have tried just writing app.require_subcommand(1); but this seems to prioritise the subcommand over the option group, when you execute the code with this it will always print "at least one subcommand is required" regardless of whether you specify an option group or not.
Ideally I would want app.require_subcommand(1); to execute only if an option group has been selected first. I've attempted to do this by just putting the subcommand in an if statement with the option group bool as a condition, but it apparently doesn't work like this lol I still get "at least one subcommand is required" and it ignored the option group.
I apologize in advance if what I'm looking to do is still unclear, I've been over every example in CLI11 code examples. It's just unclear to me how to go about doing this, every open source project I've looked at that uses this library is very complex, if there is a better way please tell me!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
