Skip to content

Latest commit

 

History

40 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cflg - Simple, STB-Style Flag Parsing for C

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.h with CFLG_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 -vqc as -v -q -c.
  • Long option completion: Accepts partial matches (e.g., --he for --help if 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.

Quick Start

  1. Copy cflg.h into your project.
  2. In one C file, define CFLG_IMPLEMENTATION before including cflg.h.
  3. 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 greet

Check the examples folder for real-world demos.

Notes

  • Positional Arguments: Access via fset.narg (count) and fset.args (array, rearranged to place positionals first after argv[0]).
  • Custom Parsers: Use cflg_flgset_func for custom types (see cflg.h for details).
  • Portability: Works on any C99+ compiler, no dependencies.

About

Go flag module, but in C

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Contributors

Languages