Skip to content

Command line parser

Raymond Chen edited this page Mar 30, 2019 · 4 revisions

The cmd_reader.h header file provides a basic command line parser.

All of the types reside in the xlang::cmd namespace.

The basic idea is as follows:

int main(int argc, char** argv)
{
    static constexpr xlang::cmd::option options[] =
    {
        // name, min, max
        { "verbose", 0, 0 },
        { "name", 1 },
        { "in", 1 },
    };

    xlang::cmd::reader args{ argc, argv, options };

    if (!args)
    {
        // command line parsing error
        print_instructions();
        return 1;
    }

    // Did the user pass -verbose?
    bool verbose = args.exist("verbose");

    // Get all the "-name" options.
    std::vector<std::string> names = args.values("name");

    // Get all the "-in" options. They are files.
    // Directories will be auto-enumerated.
    std::set<std::string> files = args.files("in");

    return 0;
}
Clone this wiki locally