cflg is a single-header stb-style C library for parsing command-line flags, inspired by Go's flag package. It’s simple, uses no dynamic memory (stack-only), and integrates easily into any C99+ project. Key features:
- Drop-in: Just include
cflg.hwithCFLG_IMPLEMENTATION. - No malloc: All parsing happens on the stack.
- Direct binding: Flags map to your variables (bool, int, string, etc.).
- Automatic
--help: Generates a help message from flag definitions. - sorting flags: Sorts flags lexicographically while Generating help message.
- Aggregated short options: Supports
-vqcas-v -q -c. - Long option completion: Accepts partial matches (e.g.,
--hefor--helpif unambiguous). - Custom parsers: Support complex types (e.g.,
--memory=512m).
Unlike getopt (verbose, manual help) or argp (complex, GNU-only), cflg offers a modern, portable API with minimal setup.
- Copy
cflg.hinto your project. - In one C file, define
CFLG_IMPLEMENTATIONbefore includingcflg.h. - Define flags, parse, and use variables directly.
Example (main.c):
#include <stdio.h>
#include <stdbool.h>
#define CFLG_IMPLEMENTATION
#include "cflg.h"
int main(int argc, char *argv[]) {
/* Define flag variables, initial valuse are default values */
const char *name = "world";
bool loud = false;
/* Initialize flag set */
flgset_t fset = {0};
/* Bind flags to variables */
flgset_string(&fset, &name, 'n', "name", "<NAME>", "Name to greet");
flgset_bool(&fset, &loud, 'l', "loud", "Greet loudly");
/* Parse arguments (handles --help and errors automatically) */
flgset_parse(&fset, argc, argv);
/* Use parsed values */
printf("Hello, %s%s\n", name, loud ? "!" : "");
/* Access positional arguments */
for (int i = 1; i < fset.narg; i++) {
printf("Positional: %s\n", fset.args[i]);
}
return 0;
}Compile and run:
gcc main.c -o greet
./greet --name AliReza --loud cmd1
# Output: Hello, AliReza!
# Positional: cmd1
./greet --help
# Output: Usage: greet [OPTION]... [COMMAND]...
#
# -h, --help print this help
# -l, --loud Greet loudly
# -n, --name=<NAME> Name to greetCheck the examples folder for real-world demos.
- Positional Arguments: Access via
fset.narg(count) andfset.args(array, rearranged to place positionals first afterargv[0]). - Custom Parsers: Use
cflg_flgset_funcfor custom types (seecflg.hfor details). - Portability: Works on any C99+ compiler, no dependencies.