https://github.com/lukaswrz/readarg
A small, C99, single-header, zero-allocation, dependency-free, public domain argument parsing library
https://github.com/lukaswrz/readarg
argument-parsing c cli command-line header-only library parser single-header zero-allocation
Last synced: 3 months ago
JSON representation
A small, C99, single-header, zero-allocation, dependency-free, public domain argument parsing library
- Host: GitHub
- URL: https://github.com/lukaswrz/readarg
- Owner: lukaswrz
- License: cc0-1.0
- Created: 2021-05-18T15:45:07.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-07T23:59:21.000Z (over 1 year ago)
- Last Synced: 2024-08-08T02:32:17.023Z (over 1 year ago)
- Topics: argument-parsing, c, cli, command-line, header-only, library, parser, single-header, zero-allocation
- Language: C
- Homepage:
- Size: 99.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# readarg
## Features
* Short options (`-f`)
* Short options with a value directly following the option character
(`-fvalue`)
* Short options with a value as a separate `argv` element (`-f value`)
* Grouped short options (`-asdf`, `-asdfvalue`, `-asdf value`)
* Long options (`--file`)
* Long options with a value separated by an equal sign (`--file=value`)
* Long options with a value as a separate `argv` element (`--file value`)
* Multiple values are represented in an array (`-f value1 -f value2 ...`)
* Operands mixed with options (`-f value1 operand1 -f value2 operand2`)
## Usage
Installing this library is as simple as downloading the header file, dropping
it into your project directory and including it. Alternatively, you could choose
to use a Git submodule. In any case, attribution is not required.
It is required that one file in your project defines the
`READARG_IMPLEMENTATION` macro before including the `readarg.h` header file,
as with any other single-header library.
An example for how to use readarg can be found in `test/test.c`. If you want to
see how readarg represents options and operands, run `test.bash`.
## Terminology
If you're wondering what exactly the difference between an option, an operand or
an argument is, you can skim this document to get a better idea:
[POSIX Utility Conventions](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html)
## Internals
readarg permutes `argv` to handle multiple values for each option and to assign
values to operands. The advantage of this approach is as follows:
* Allocations are not needed because the memory provided by `argv` is reused
* It's fairly simple to represent all of this data in an intuitive data
structure (in my opinion anyway)