Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/piotrpsz/clap

C++ library for analyzing program call arguments.
https://github.com/piotrpsz/clap

cpp cpp20

Last synced: about 2 months ago
JSON representation

C++ library for analyzing program call arguments.

Awesome Lists containing this project

README

        

# 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:
```c++
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::
```c++
int main(int argn, char* argv[]) {
clap.parse(argn, argv);
...
...
}
```
And finally to get the parsing result:
```c++
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).