muellan / clipp

easy to use, powerful & expressive command line argument parsing for modern C++ / single header / usage & doc generation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

not set string value

hdhog opened this issue · comments

commented

sample code:

#include <iostream>
#include <filesystem>
#include <vector>
#include <clipp.h>

#define VERSION "0.0.0.0"
class app {
public:
    app(int argc, const char** argv) {
        args.assign(argv, argv + argc);
    }

    int exec() {
        try {
            cli =
                ("General:" %
                     ((clipp::option("-c", "--config") & clipp::value("file name", config_file)) %
                          "config file",
                      (clipp::option("-h", "--help").set(f_help)) % "help message",
                      (clipp::option("-v", "--version").set(f_version)) % "show version") ,
                 "Commands:" %
                     ("service" %
                      (clipp::command("service") % "service controll",
                       (((clipp::option("-I", "--install").set(cmd_install)) % "install service") |
                        ((clipp::option("-R", "--remove").set(cmd_remove)) % "remove service")))));

            auto parse_res = clipp::parse(args, cli);

            if(!parse_res.any_error()) {
                show_help();
                return 1;
            }

            if(f_help) {
                show_help();
                return 0;
            }

            if(f_version) {
                std::cout << VERSION << std::endl;
                return 0;
            }

            std::filesystem::path fname(config_file);
            std::cout << "cfg: " << config_file << "\n";
            std::cout << fname.is_absolute() << "\n";
            std::cout << fname << "\n";

            if(cmd_install) {
                //install
                return 0;
            }

            if(cmd_remove) {
                //remove
                return 0;
            }
        } catch(std::exception& rcError) {
            std::cerr << rcError.what() << std::endl;
            return 1;
        }
        return 0;
    }
    void show_help() {
        auto fmt = clipp::doc_formatting {}
                       .first_column(4)
                       .doc_column(36)
                       .merge_alternative_flags_with_common_prefix(true);

        std::cout << clipp::make_man_page(cli, "server", fmt)
                         .prepend_section("AUTHORS", "\tauthor")

                         .prepend_section("DESCRIPTION", "\tThe App")
                  << '\n';
    }

private:
    std::vector<std::string> args;
    clipp::group cli;
    bool f_help = false;
    bool f_version = false;
    bool cmd_install = false;
    bool cmd_remove = false;
    std::string config_file;
};

int main(int argc, const char* argv[]) {	
    app app_instance(argc, argv);
    return app_instance.exec();
}

flag output:

 .\cpp_clipp.exe -c config.toml

DESCRIPTION
        The App

AUTHORS
        author

SYNOPSIS
    server [-c <file name>] [-h] [-v] service [-(I|R)]

OPTIONS
    General:
        -c, --config <file name>    config file
        -h, --help                  help message
        -v, --version               show version

    Commands:
        service                     service controll
        -I, --install               install service
        -R, --remove                remove service

Output

.\cpp_clipp.exe -c config.toml
cfg:
0
""

variable config_file is empty.