How to use CLI11 c++

 avatar
ketronix
c_cpp
3 years ago
1.1 kB
18
Indexable
#include <iostream>
#include "CLI11.hpp"

int main(int argc, char **argv) {

    CLI::App app{"My App"};

    // Define install command
    CLI::App *install = app.add_subcommand("install", "Install an app");
    std::vector<std::string> install_args;
    install->add_option("args", install_args, "Args to install")->required();

    // Define remove command
    CLI::App *remove = app.add_subcommand("remove", "Remove an app");
    std::vector<std::string> remove_args;
    remove->add_option("args", remove_args, "Args to remove")->required();

    // Define list command
    CLI::App *list = app.add_subcommand("list", "List apps");
    list->callback([&]() {
        std::cout << "List apps" << std::endl;
    });

    // Parse the command line
    CLI11_PARSE(app, argc, argv);

    // Handle commands
    if (install->parsed()) {
        for (auto arg : install_args) {
            std::cout << arg << std::endl;
        }
    } else if (remove->parsed()) {
        for (auto arg : remove_args) {
            std::cout << arg << std::endl;
        }
    }

    return 0;
}
Editor is loading...