https://github.com/erkkah/tinyargs
Tiny command-line parser for C / C++
https://github.com/erkkah/tinyargs
command-line parser tiny
Last synced: 18 days ago
JSON representation
Tiny command-line parser for C / C++
- Host: GitHub
- URL: https://github.com/erkkah/tinyargs
- Owner: erkkah
- License: unlicense
- Created: 2021-10-17T15:09:10.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-17T19:43:17.000Z (over 3 years ago)
- Last Synced: 2025-03-28T20:12:05.433Z (about 1 month ago)
- Topics: command-line, parser, tiny
- Language: C
- Homepage:
- Size: 23.4 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://github.com/erkkah/tinyargs/actions/workflows/build.yml)
[](https://github.com/boyter/scc/)# tinyargs
Another commandline argument parser for C / C++.
This one is tiny, source only, and builds cleanly with `-Wall -pedantic` under C99 and C++11 on macOS, Linux and Windows.
See [header file](args.h) for reference docs.
Example use:
```C
#include "args.h"
#includeint main(int argc, const char** argv) {
static OPTDEFS(opts,
INTOPT("blurgAmount", "9"),
FLOATOPT("fudgeRatio", "-2.33"),
STRINGOPT("trunkPrefix", "-=-"),
BOOLOPT("help"));if (!parseArgs(&argc, &argv, opts)) {
printf("Invalid option \"%s\".\n\n", argv[argc]);
printf(
"Usage: %s [options] [args]\n\n"
"Options:\n\n%s\n",
argv[0],
listOptions(opts));
return 42;
}printf("Will operate on: [\n");
for (int i = 0; i < argc; i++) {
printf("\t%s\n", argv[i]);
}
printf("]\n");printf(
"with a blurgAmount of %d, "
"a fudgeRatio of %f, "
"using trunkPrefix: %s, "
"%shelp requested.\n",
getIntOption(opts, "blurgAmount"),
getFloatOption(opts, "fudgeRatio"),
getStringOption(opts, "trunkPrefix"),
getBoolOption(opts, "help") ? "" : "no ");
}
``````
$ ./example -fudgeRatio=3.14 -help A B C
Will operate on: [
A
B
C
]
with a blurgAmount of 9, a fudgeRatio of 3.140000, using trunkPrefix: -=-, help requested.
``````
$ ./example -notAnOption=66
Invalid option "-notAnOption=66".Usage: ./example [options] [args]
Options:
-blurgAmount= (default=9)
-fudgeRatio= (default=-2.330000)
-trunkPrefix= (default="-=-")
-help
```