kyaa/README.md

186 lines
6.7 KiB
Markdown
Raw Permalink Normal View History

2016-08-18 19:46:44 -07:00
# kyaa
super hacky macro hacks for parsing arguments in C.
2019-06-25 21:45:28 -07:00
## FAQ
2016-08-18 19:46:44 -07:00
2019-06-25 21:45:28 -07:00
*Fairly Arbitrary Questions*
### Why?
kyaa exists for when the "quick 'n' dirty" isn't dirty enough.
also, it keeps argument parsing *in* main,
instead of delegating with callbacks and whatnot.
### How?
kyaa secretly wraps flag-handling in if-else statements with curly braces.
it's not a switch-case method!
kyaa instead uses `continue` to handle certain arguments.
### Should I use this?
honestly, no.
### What's in a name?
i have no idea why i called it "kyaa,"
other than it being the first thing that came to mind
that wasn't already in use by some other software.
### What do I need?
the prerequisites are:
* C99 (or greater)
2016-08-18 19:46:44 -07:00
2016-08-18 19:55:00 -07:00
* `stdbool.h`
2019-06-25 21:45:28 -07:00
* `stdio.h` (can be avoided by overriding `KYAA_OUT` and `KYAA_ERR`)
2016-08-18 20:04:48 -07:00
* `string.h`
2016-08-18 19:46:44 -07:00
2019-06-25 21:45:28 -07:00
* `limits.h` (just for `kyaa_extra.h`, can be avoided by defining `LONG_MIN`)
2017-04-01 19:23:01 -07:00
2019-06-25 22:18:05 -07:00
kyaa does not allocate any memory on heap.
2017-03-30 23:29:13 -07:00
## tutorial
2016-08-18 19:46:44 -07:00
2019-06-25 21:45:28 -07:00
ensure `argc` and `argv` are defined in a function that returns an `int`.
2016-08-18 19:55:00 -07:00
kyaa doesn't actually care if it's in `main` or not.
2016-08-18 19:46:44 -07:00
2016-08-18 19:50:10 -07:00
iterate over the arguments with `KYAA_LOOP`:
2016-08-18 19:46:44 -07:00
```c
2019-06-25 17:26:22 -07:00
int main(int argc, char **argv) {
2016-08-18 19:46:44 -07:00
KYAA_LOOP {
2019-06-25 21:29:23 -07:00
// kyaa_iter, kyaa_name, and kyaa_read_stdin are exposed here.
2016-08-18 19:46:44 -07:00
// [other code goes here]
}
return 0;
}
```
2016-08-18 19:50:10 -07:00
use `KYAA_BEGIN` and `KYAA_END` to begin parsing arguments.
put unrelated code above `KYAA_BEGIN` or below `KYAA_END`, but not within:
2016-08-18 19:46:44 -07:00
```c
KYAA_LOOP {
// [other code goes here]
KYAA_BEGIN
// kyaa_arg and kyaa_etc are exposed here.
// [kyaa code goes here]
KYAA_END
// [other code goes here]
}
```
2016-08-18 19:50:10 -07:00
use `KYAA_FLAG` for defining flags that don't take an argument,
and `KYAA_FLAG_ARG` or `KYAA_FLAG_LONG` for flags that do:
2016-08-18 19:46:44 -07:00
```c
bool use_feature = false;
2019-06-25 17:26:22 -07:00
const char *log_fn = "log.txt";
2016-08-18 19:46:44 -07:00
long my_var = 0;
KYAA_LOOP {
// [other code goes here]
KYAA_BEGIN
// arguments: short flag, long flag, help description
2019-06-25 17:26:22 -07:00
KYAA_FLAG('x', "enable-feature",
2016-08-18 19:46:44 -07:00
" enable some feature")
use_feature = true;
// same arguments, but kyaa_etc contains the relevant string.
2019-06-25 17:26:22 -07:00
KYAA_FLAG_ARG('l', "log-file",
2016-08-18 19:46:44 -07:00
" use a given filename for the log file")
log_fn = kyaa_etc;
2019-06-25 17:26:22 -07:00
// same arguments, kyaa_etc is set, but kyaa_long_value is also set.
KYAA_FLAG_LONG('v', "var",
2016-08-18 19:46:44 -07:00
" set an integer variable\n"
" default: 0")
2019-06-25 17:26:22 -07:00
my_var = kyaa_long_value;
2016-08-18 19:46:44 -07:00
KYAA_END
// [other code goes here]
}
2019-06-25 17:26:22 -07:00
return 0;
2016-08-18 19:46:44 -07:00
```
2016-08-18 19:50:10 -07:00
kyaa handles `-h` and `--help` for printing help text.
additional help text may be defined using `KYAA_HELP`:
2016-08-18 19:46:44 -07:00
```c
KYAA_LOOP {
KYAA_BEGIN
KYAA_HELP(
" {files...}\n"
" do things with files.")
KYAA_END
do_stuff(kyaa_arg);
}
```
2016-08-18 19:50:10 -07:00
kyaa interprets an argument of `-` as a request to enable reading from stdin:
2019-06-25 21:29:23 -07:00
`kyaa_read_stdin` is set to true. this may be reset by the user if desired.
2016-08-18 19:46:44 -07:00
2019-06-25 17:26:22 -07:00
flag values may be specified in four ways; the following are equivalent:
2016-08-18 19:46:44 -07:00
* `-v42`
* `-v 42`
* `--var 42`
2019-06-25 17:26:22 -07:00
* `--var=42`
2016-08-18 19:46:44 -07:00
2016-08-18 19:50:10 -07:00
kyaa returns `KYAA_OKAY` when `-h` or `--help` is given,
2019-06-25 17:26:22 -07:00
or `KYAA_FAIL` in the event of invalid flags, missing arguments,
2016-08-18 19:50:10 -07:00
or invalid numbers (`KYAA_FLAG_LONG`).
2016-08-18 19:46:44 -07:00
2019-06-25 17:26:22 -07:00
kyaa prints help text to `stdout` and error messages to `stderr`.
this can be overridden by specifying `KYAA_OUT` and `KYAA_ERR` respectively.
2017-03-30 23:29:13 -07:00
## API
### kyaa.h
2019-06-25 21:57:47 -07:00
signature | name | arguments or default value | description
2019-06-25 17:26:22 -07:00
--- | --- | --- | ---
define | KYAA\_OKAY | 0 | value to return upon a successful early exit
define | KYAA\_FAIL | 1 | value to return upon encountering an error
2019-06-25 21:57:47 -07:00
define | KYAA\_OUT | `printf(__VA_ARGS__)` | printf-like function for help text
define | KYAA\_ERR | `fprintf(stderr, __VA_ARGS__)` | printf-like function for error text
2019-06-25 17:26:22 -07:00
macro | KYAA\_SETUP | *none* | performs a sanity check and initializes variables
macro | KYAA\_LOOP | *none* | begins iterating over arguments
macro | KYAA\_BEGIN | *none* | begins parsing arguments
macro | KYAA\_END | *none* | finishes parsing arguments
macro | KYAA\_DESCRIBE | c, name, description | describes a flag for help text
macro | KYAA\_FLAG | c, name, description | handles a flag that takes no arguments
macro | KYAA\_FLAG\_ARG | c, name, description | handles a flag that takes a string argument
macro | KYAA\_HELP | description | prints additional text for help text
`const char *` | kyaa\_name | *n/a* | the name of the program (usually `argv[0]`)
`int` | kyaa\_iter | *n/a* | the index of the argument being parsed
`const char *` | kyaa\_arg | *n/a* | the whole argument being parsed
`const char *` | kyaa\_etc | *n/a* | an argument to a flag, or `NULL`
`bool` | kyaa\_read\_stdin | *n/a* | set when an argument is `-` (remains set unless reset by user)
2019-06-25 21:57:47 -07:00
#### internal use
signature | name | arguments or default value | description
--- | --- | --- | ---
2019-06-25 17:26:22 -07:00
macro | KYAA\_IS\_LONG | arg, name | tests whether `arg` describes a long flag called `name`
`char` | kyaa\_char | *n/a* | the character assigned to the current flag being parsed
`bool` | kyaa\_parsing | *n/a* | whether parsing occurs (`--` cancels parsing)
`bool` | kyaa\_skip | *n/a* | set when a flag needs another argument
`bool` | kyaa\_helping | *n/a* | set when `-h` or `--help` is encountered
`bool` | kyaa\_any | *n/a* | set when any flag has been matched
`const char *` | kyaa\_equals | *n/a* | a pointer to the first `=` character in a long flag, or `NULL`
2017-03-30 23:29:13 -07:00
### kyaa\_extra.h
2019-06-25 21:57:47 -07:00
signature | name | arguments or default value
2017-04-01 19:23:01 -07:00
--- | --- | ---
macro | KYAA\_FLAG\_LONG | c, name, description
`char *` | kyaa\_flag\_arg | *n/a*
`char *` | kyaa\_skip\_spaces | `char *` str
`const char *` | kyaa\_str\_to\_long | `char *` str, `long *` p\_out