/clap

C++ library for analyzing program call arguments.

Primary LanguageC++MIT LicenseMIT

clap

C++ library for analyzing program call arguments.

  • you can use single-letter (short) arguments in the call line, e.g. -i (ignore case) -r (recursive),
  • you can use longer arguments, e.g.: --icase, --resursive,
  • specifying the value of the argument: -d '/Users' or -d='/Users' or --dir '/Users' lub --dir='/Users',
  • if you have several arguments without values (-i and -r) you can combine them: -ir
  • the text value of the argument may contain spaces: -t='ala ma kota' or --text 'ala ma kota'

dependencies:

fmt library

  1. from https://github.com/fmtlib/fmt
  2. install on macOS: brew install fmt

How to add to your own project (using CMake):

  1. Go to the root directory of your project (I assume you have git initialized).
  2. Enter the command: git submodule add https://github.com/piotrpsz/clap.git
  3. In your project's CMakeLists.txt file, add the following:
    • add_subdirectory(clap)
    • target_link_libraries(your_project_name PUBLIC clap)

Example of use:

First create a Clap object with the parameters you want:

auto clap = Clap("dirscanner v. 0.1",
                 Arg()
                    .marker("-i")
                    .promarker("--icase")
                    .ordef(false),
                 Arg()
                    .marker("-r")
                    .promarker("--recursive"),
                 Arg()
                    .marker("-d")
                    .promarker("--dir")
);

Then, after the program starts, call the parse function::

int main(int argn, char* argv[]) {
    clap.parse(argn, argv);
    ...
    ...
}

And finally to get the parsing result:

    if (auto directory = clap["--dir"]; directory)
        std::cout << *directory << '\n';

    if (auto icase = clap["-i"]; icase)
        std::cout << *icase << '\n';

When no value is given for the argument, it receives the logical value true (that it was in the call).