Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thehxdev/cap
Single header command-line argument parser written in C and compatible with C99
https://github.com/thehxdev/cap
argument-parser c cmake command-line cpp library parser single-header
Last synced: about 2 months ago
JSON representation
Single header command-line argument parser written in C and compatible with C99
- Host: GitHub
- URL: https://github.com/thehxdev/cap
- Owner: thehxdev
- Created: 2024-03-14T14:30:33.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-06-01T11:15:46.000Z (8 months ago)
- Last Synced: 2024-06-01T12:38:50.284Z (7 months ago)
- Topics: argument-parser, c, cmake, command-line, cpp, library, parser, single-header
- Language: C
- Homepage:
- Size: 48.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Cap
Cap is a single header command-line argument parser library written in C
with low memory footprint and compatible with C99 standard.## Usage
To use Cap just include `cap.h` file then compile and link `cap.c` file with your project.
## Example
Use this code snippet to learn how to use Cap.
```c
#include
#include "cap.h"int main(int argc, char **argv) {
int err = 0;// Initialize cap
Cap_t *cap = cap_init(argc, argv);
if (cap == NULL)
// handle error
return 1;// register a new sub-command
// say our app is a compiler and we want a `compile` sub-command
err = cap_register_subcmd(cap, "compile", "Compile the source files");
if (err != 0)
// handle error
goto exit;// register a new flag
// say we want to add a new flag to `compile` sub-command
err = cap_register_flag(cap, "compile", "op", "optimize the source code");// regist a new flag with no sub-command
err = cap_register_flag(cap, NULL, "file", "file path");err = cap_parse_args(cap);
if (err != 0)
// must handle this error
goto exit;// get raw arguments for `compile` sub-command
Cap_RawArgs_t run_args = cap_subcmd_rawargs(cap, "compile");// a flag is present in command-line args or not (used for boolean flags)
// result will be 0 (false) or 1 (true)
int op = cap_flag_provided(cap, "compile", "op");// get a flag's value
char *tmp = cap_flag_getval(cap, NULL, "file");exit:
// deinit and free all memory allocated by cap
cap_deinit(&cap);
return err;
}
```