diff options
| author | Tyge Løvset <[email protected]> | 2020-12-09 20:44:58 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-12-09 20:44:58 +0100 |
| commit | 39cfd7c23e849b78d0156a3025c15a8eb25dd356 (patch) | |
| tree | 5725c1efbeceb0da4ff2262f27d5cc14b52ba6a7 /docs | |
| parent | e3d6b9b3cb42a0fc066572e9aefb41b5cf45dc2c (diff) | |
| download | STC-modified-39cfd7c23e849b78d0156a3025c15a8eb25dd356.tar.gz STC-modified-39cfd7c23e849b78d0156a3025c15a8eb25dd356.zip | |
Renamed coption.h to copt.h. Added docs/copt_api.md.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/copt_api.md | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/docs/copt_api.md b/docs/copt_api.md new file mode 100644 index 00000000..e91c21ea --- /dev/null +++ b/docs/copt_api.md @@ -0,0 +1,82 @@ +# Introduction + +This describes the API of string type *copt_get()* function for command line argument parsing. + +## Types + +```c +enum { + copt_no_argument = 0, + copt_required_argument = 1, + copt_optional_argument = 2 +}; +typedef struct { + int ind; /* equivalent to optind */ + int opt; /* equivalent to optopt */ + const char *arg; /* equivalent to optarg */ + const char *faulty; /* points to the faulty option, if any */ + int longindex; /* index of long option; or -1 if short */ + ... +} copt_t; + +typedef struct { + const char *name; + int has_arg; + int val; +} copt_long_t; +``` + +## Methods + +``` +copt_t copt_init(void); +int copt_get(copt_t *opt, int argc, char *argv[], + const char *shortopts, const copt_long_t *longopts); +``` + +## Example + +``` +#include <stdio.h> +#include "stc/copt.h" + +int main(int argc, char *argv[]) { + static copt_long_t long_options[] = { + {"verbose", copt_no_argument, 'V'}, + {"help", copt_no_argument, 'H'}, + {"add", copt_no_argument, 'a'}, + {"append", copt_no_argument, 'b'}, + {"delete", copt_required_argument, 'd'}, + {"create", copt_required_argument, 'c'}, + {"file", copt_required_argument, 'f'}, + {NULL} + }; + copt_t opt = copt_inits; + int c; + while ((c = copt_get(&opt, argc, argv, ":if:lr", long_options)) != -1) { + switch (c) { + case 'V': case 'H': + case 'a': case 'b': + case 'd': case 'c': + case 'i': case 'l': + case 'r': + printf("option: %c\n", c); + break; + case 'f': + printf("filename: %s\n", opt.arg); + break; + case ':': + printf("option %s needs a value\n", opt.faulty); + break; + case '?': + printf("unknown option: %s\n", opt.faulty); + break; + } + } + + for (; opt.ind < argc; ++opt.ind) { + printf("extra arguments: %s\n", argv[opt.ind]); + } + return 0; +} +```
\ No newline at end of file |
