readarg/README.md

43 lines
1.7 KiB
Markdown
Raw Permalink Normal View History

2022-10-12 19:49:23 +00:00
# readarg
2021-02-08 16:07:25 +00:00
2022-09-29 19:33:41 +00:00
## Features
2021-02-08 16:07:25 +00:00
2022-09-29 19:33:41 +00:00
* 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`)
2021-02-08 16:07:25 +00:00
2022-09-29 19:33:41 +00:00
## Usage
2021-02-08 16:07:25 +00:00
2022-09-29 19:33:41 +00:00
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.
2021-02-08 16:07:25 +00:00
2022-09-29 19:33:41 +00:00
It is required that one file in your project defines the
2022-10-12 19:49:23 +00:00
`READARG_IMPLEMENTATION` macro before including the `readarg.h` header file,
2022-09-29 19:33:41 +00:00
as with any other single-header library.
2021-02-08 16:07:25 +00:00
2022-10-12 19:49:23 +00:00
An example for how to use readarg can be found in `test/test.c`. If you want to
2022-11-20 11:41:10 +00:00
see how readarg represents options and operands, run `test.bash`.
2021-02-08 16:07:25 +00:00
2022-09-29 19:33:41 +00:00
## Terminology
2021-02-08 16:07:25 +00:00
2022-09-29 19:33:41 +00:00
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)
2021-02-08 16:07:25 +00:00
2022-09-29 19:33:41 +00:00
## Internals
2021-02-08 16:07:25 +00:00
2022-10-12 19:49:23 +00:00
readarg permutes `argv` to handle multiple values for each option and to assign
2022-09-29 19:33:41 +00:00
values to operands. The advantage of this approach is as follows:
2021-02-08 16:07:25 +00:00
2022-09-29 19:33:41 +00:00
* 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)