A simple command line argument parser for c applications. All resolved arguments can be accessed as a key-value pair and through the functions cparser_hasKey() and cparser_getValue().
There are two different options to resolve arguments:
Both functions require a key-value-pair cparser_dict to save the resolved arguments to.
-
Windows:
- Download and install(adding the path variable) the most recent WinLibs UCRT runtime and MinGW64(UCRT)
- Compile the code statically using gcc:
gcc.exe -o <filename>.exe <filename>.c -l:libcparser.lib -L<libpath> -I<libpath>
-
*nix:
- Verify the Dependencies
- Compile the code statically using gcc:
gcc -o <filename>.o <filename>.c -l:libcparser.a -L<libpath> -I<libpath>
unistd.h stdio.h stdlib.h string.h
struct cparser_kv {
char *key; // key string
char *val; // value string
};
typedef struct cparser_dict {
int kvn; // number of stored kv-pairs
struct cparser_kv *kv; // array that holds all kv-pairs
} cparser_dict_t;
-
description Parses key-value-pairs from a given input buffer of strings. Single dashes imply a single character argument. Their key is the first character after the dash, their value all other characters until the next whitespace or line end. Double dashes imply a word argument. Their key is the whole word - all characters until an equals sign or a whitespace or a line end, whichever comes first - after the double dash, their value will be every character after the first equals sign until the next whitespace or line end. params number of buffer elements int argc
, pointer to buffer of stringschar *argv[]
, pointer to a cparser dictionarycparser_dict_t *input_dict
return int
- On success, return 1, otherwise return -1. -
description Same as cparser_parse(). However, double dash arguments also accept a single space instead of an equals sign. params number of buffer elements int argc
, pointer to buffer of stringschar *argv[]
, pointer to a cparser dictionarycparser_dict_t *input_dict
return int
- On success, return 1, otherwise return -1.
-
description Frees the cparser dictionary. params pointer to a cparser dictionary cparser_dict_t *input_dict
return void
-
description Dumps the whole cparser dictionary to a specified file descriptor. params file descriptor int fd
, pointer to a cparser dictionarycparser_dict_t *input_dict
return void
-
description Checks if key has been resolved. params pointer to the requested key char *key
, pointer to a cparser dictionarycparser_dict_t *input_dict
return int
- Return 1 when the requested key is part of the cparser dictionary, otherwise return 0. -
description Obtains the value of a requested key and copies it to the destination location. params pointer to the requested key char *key
, pointer to destination bufferchar *dest
, pointer to a cparser dictionarycparser_dict_t *input_dict
return int
- On success, return 1, otherwise return 0.