https://github.com/t-m-e/argparse
C header-only implementation of an argument parser.
https://github.com/t-m-e/argparse
argparse argparser argument-parser c
Last synced: 3 months ago
JSON representation
C header-only implementation of an argument parser.
- Host: GitHub
- URL: https://github.com/t-m-e/argparse
- Owner: t-m-e
- Created: 2023-12-17T00:03:23.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-22T02:53:57.000Z (over 1 year ago)
- Last Synced: 2025-01-20T10:48:49.718Z (4 months ago)
- Topics: argparse, argparser, argument-parser, c
- Language: C
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Argparse
## Purpose
Argparse is used to parse arguments! Easy enough right?
Argparse is a header-only implementation made to be easily integrated into any project. Both declaration and implementation exist inside of the `include/argparse.h` file.## Rule Strings
Rule strings are strings that define names of flags and how many arguments they consume. Rule strings take the following format: `:` consumes an argument, whitespace delimits different flag names, and anything else is parsed as a flag.
Good thing to note, the Argparse parsing takes argument names literally, so if your argument is input as `-a` then the parser will interpret it as it is input.## Usage
Use with argc/argv:
```c
/* $ ./argparse -a -b argb -c argc1 argc2 */#include "include/argparse.h"
int main(
int argc,
char** argv
) {
struct Argparse argparse = Argparse_new("-a -b: -c::", ++argv, --argc);/* use your args */
if (Argparse_exists(&argparse, "-a")) {
// do something
}char* option = Argparse_get(&argparse, "-b");
char* option1 = Argparse_getArg(&argparse, "-c", 0);
char* option2 = Argparse_getArg(&argparse, "-c", 1);
}
```Use with self defined array:
```c
#include "include/argparse.h"
#includeint main() {
char* argv[] = {
"-a", "-b", "some", "other", "thing"
};
int argc = 5;struct Argparse argparse = Argparse_new("-a -b", argv, argc);
if (Argparse_exists(&argparse, "-a")) { /* ... */ }
if (Argparse_exists(&argparse, "-b")) { /* ... */ }
char* param = Argparse_getParam(&argparse, 0);
/* macro defined in argparse.h
* item is a user defined name for the parameter value (a char*) inside of the loop scope
* argparse is the struct Argparse variable defined above
*/
FOREACH_ARGPARSE_PARAM(item, argparse) {
printf("%s", item);
}
}
```