Skip to content

AWeirdDev/kargs.h

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kargs.h

A single-header command line argument parser written in C.

It's inspired by the flag package in Go and argparse in Python. It's relatively simple to work with, so feel free to put it in your simple workflows, if you just somehow love null pointers.

Just get this shit:

wget https://raw.githubusercontent.com/AWeirdDev/kargs.h/refs/heads/main/kargs.h

And start using it:

#include <stdio.h>

#define KARGS_IMPLEMENTATION
#include "kargs.h"

int main(int argc, char **argv) {
    // Zero initialize the args container.
    // It kind of acts like a context.
    Ka_Args args = {0};

    // You get pointers to the values
    int *number =
        ka_arg_int(&args, "-n --number", .description = "Count to a number",
                   .optional = false);
    char **name = 
        ka_arg_string(&args, "--name", .description = "Your name", 
                      .optional = false);

    // This serves as the entrypoint, it basically does all the job
    ka_args_entry(&args, argc, argv);

    // Then you can use the value
    for (int i = 0; i < *number; i++) {
        printf("%s\n", *name);
    }

    // If you don't trust your OS, free this shit.
    ka_args_free(args);
}

Symbols are prefixed with ka_ or Ka_.

Supported types

  • int *ka_arg_int() – Integer
  • char **ka_arg_string() – String
  • bool *ka_arg_boolean() – Boolean

I'll add more types when I actually need it. Normally I don't.

Acknowledgements

After watching some of Mr. Zuzin's VODs I figured perhaps I really should start learning C just to know how miserable it is. And it is. So, forgive me for not writing idiomatic C code, this is my very first time writing it.

About

Single-header command line argument parser in C.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Contributors