summaryrefslogtreecommitdiffhomepage
path: root/docs/coption_api.md
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-11-20 08:36:02 +0100
committerTyge Løvset <[email protected]>2021-11-20 08:36:02 +0100
commit835a3a974f4f79ca789d6fca6c9745b4f4931870 (patch)
tree8d5db6c31705a5452170932cb082a08a181445e4 /docs/coption_api.md
parent71598f4f3a9cdd354b768c49e705a24288a6ff51 (diff)
downloadSTC-modified-835a3a974f4f79ca789d6fca6c9745b4f4931870.tar.gz
STC-modified-835a3a974f4f79ca789d6fca6c9745b4f4931870.zip
Added carr_X_idx().
Diffstat (limited to 'docs/coption_api.md')
-rw-r--r--docs/coption_api.md49
1 files changed, 18 insertions, 31 deletions
diff --git a/docs/coption_api.md b/docs/coption_api.md
index b7eebc43..be0d0978 100644
--- a/docs/coption_api.md
+++ b/docs/coption_api.md
@@ -39,46 +39,33 @@ int coption_get(coption *opt, int argc, char *argv[],
## Example
```c
-#include <stc/coption.h>
#include <stdio.h>
+#include <stc/coption.h>
int main(int argc, char *argv[]) {
- static coption_long long_options[] = {
- {"verbose", coption_no_argument, 'V'},
- {"help", coption_no_argument, 'H'},
- {"add", coption_no_argument, 'a'},
- {"append", coption_no_argument, 'b'},
- {"delete", coption_required_argument, 'd'},
- {"create", coption_required_argument, 'c'},
- {"file", coption_required_argument, 'f'},
- {NULL}
+ coption_long longopts[] = {
+ {"foo", coption_no_argument, 'f'},
+ {"bar", coption_required_argument, 'b'},
+ {"opt", coption_optional_argument, 'o'},
+ {0}
};
+ const char* shortopts = "xy:z::123";
+ if (argc == 1)
+ printf("Usage: program -x -y ARG -z [ARG] -1 -2 -3 --foo --bar ARG --opt [ARG] [ARGUMENTS]\n", argv[0]);
coption opt = coption_init();
int c;
- while ((c = coption_get(&opt, argc, argv, ":if:lr", long_options)) != -1) {
+ while ((c = coption_get(&opt, argc, argv, shortopts, longopts)) != -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.optstr);
- break;
- case '?':
- printf("unknown option: %s\n", opt.optstr);
- break;
+ case '?': printf("error: unknown option: %s\n", opt.optstr); break;
+ case ':': printf("error: missing argument for %s\n", opt.optstr); break;
+ default: printf("option: %c [%s]\n", opt.opt, opt.arg ? opt.arg : ""); break;
}
}
-
- for (; opt.ind < argc; ++opt.ind) {
- printf("extra arguments: %s\n", argv[opt.ind]);
- }
+ printf("\nNon-option arguments:");
+ for (int i = opt.ind; i < argc; ++i)
+ printf(" %s", argv[i]);
+ putchar('\n');
+}
return 0;
}
```