Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sir-ragna/flag-parser
flagparser.h inspired by the goflags package.
https://github.com/sir-ragna/flag-parser
c c89
Last synced: about 2 months ago
JSON representation
flagparser.h inspired by the goflags package.
- Host: GitHub
- URL: https://github.com/sir-ragna/flag-parser
- Owner: sir-ragna
- License: mit
- Created: 2021-10-23T13:52:07.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-30T12:08:40.000Z (about 3 years ago)
- Last Synced: 2024-10-13T17:22:08.941Z (3 months ago)
- Topics: c, c89
- Language: C
- Homepage:
- Size: 274 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
[![.github/workflows/codeql-analysis.yml](https://github.com/sir-ragna/flag-parser/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/sir-ragna/flag-parser/actions/workflows/codeql-analysis.yml)
# flagparser
Somewhat inspired by the golang flags package.
The idea is to have a minimal amount of effort to define cmdline
arguments. Parsing is taken care of and some minimal usage is
generated.Steps.
- Define the cmdline args
- [optionally] define a rest collection
- Parse the flags. This will populate the pointers.# Example code
Below is an example definition of each type of variable.
```c
#include "flagparser.h"
#include "stdio.h"int main(int argc, const char *argv[])
{
/* Example of an integer */
int *amount = flg_int_arg(
"-a", /* short flag format */
"--amount", /* long flag format */
1, /* default value */
"Specify the amount" /* Help string */
);/* Example of a boolean argument, also called a switch
* The defaul value of switches is always false */
bool *keep = flg_bool_arg(
"-k", /* short flag format */
"--keep", /* long flag format */
"Keep temp files" /* help string */
);/* Example a string argument */
char **dns = flg_string_arg(
"-d", /* short flag format */
"--dns", /* long flag format */
"1.1.1.1", /* default value */
"Specify the target DNS server" /* help string */
);
/* You can optionally define a 'rest collection'.
* This will help the documentation string to name what you
* expect to be present after the [OPTIONS]. */
flg_define_rest_collection(
"FILE", /* Name of the collection.
* ./example [OPTIONS] [FILE]... */
0, /* minimum amount of args required */
"Files to process" /* Help string */
);/* flg_parse_flags will populate the pointers
* It will also return an offset you can use to know when the
* [OPTIONS] and where the 'rest collection' starts. */
int offset = flg_parse_flags(argc, argv);/* Optional demonstration on how to deal with the non-flag strings
* arguments. The _offset_ variable allows us to iterate over them.
*/
int i;
for (i = offset; i < argc; i++)
{
printf("Rest ARG: %i \"%s\"\n", i - offset, argv[i]);
}
/* Demonstration on flag usage */
if (*keep)
puts("Keep is enabled");
else
puts("Keep is not enabled");
printf("DNS server: \"%s\"\n", *dns);
printf("Amount: %d\n", *amount);/* Clean-up */
free(amount);
free(keep);
free(*dns);
free(dns);return 0;
}
```## Built-in `--help`
The `--help` flag is built-in and will list the cmdline
arguments when called together with their help string.![built-in help example](images/example-help.png)
# Code guidelines
- Compiles under **c89** and **C++17**. (run `make test`)
- Identifiers use snake case
- Uses the prefix `flg_` for public identifiers
- Uses the prefix `_flg_` for private identifiers