<div created="20141130022153074" creator="l" modified="20141130022153293" modifier="lh" title="$:/status/UserName">
<pre>lh3</pre>
</div>
-<div list="About" title="$:/StoryList">
+<div list="About [[Ketopt: parsing command-line arguments]]" title="$:/StoryList">
<pre></pre>
</div>
<div plugin-type="info" title="$:/temp/info-plugin" type="application/json">
}
```</pre>
</div>
-<div created="20180831083058888" creator="lh3" modified="20180831084804032" modifier="lh3" tags="[[Library Documentations]]" title="Ketopt: parsing command-line arguments">
+<div created="20180831083058888" creator="lh3" modified="20180831114806605" modifier="lh3" tags="[[Library Documentations]]" title="Ketopt: parsing command-line arguments">
<pre>!!Synopsis
* Functionality: parse command-line arguments, supporting similar features to GNU's [[getopt_long|https://linux.die.net/man/3/getopt_long]], but with a slightly different interface.
printf("Non-option arguments:");
for (i = opt.ind; i < argc; ++i)
printf(" %s", argv[i]);
- fputc('\n', stderr);
+ putchar('\n');
return 0;
}
+```
+!!!Example 2: subcommand
+```c
+#include <stdio.h>
+#include "ketopt.h"
+int main(int argc, char *argv[])
+{
+ ketopt_t om = KETOPT_INIT, os = KETOPT_INIT;
+ int i, c;
+ while ((c = ketopt(&om, argc, argv, 0, "x", 0)) >= 0)
+ if (c == 'x') printf("main -x\n");
+ if (om.ind == argc) {
+ fprintf(stderr, "missing subcommand\n");
+ return 1;
+ }
+ printf("subcommand: %s\n", argv[om.ind]);
+ while ((c = ketopt(&os, argc - om.ind, argv + om.ind, 1, "x", 0)) >= 0)
+ if (c == 'x') printf("sub -x\n");
+ printf("Non-option arguments:");
+ for (i = os.ind + om.ind; i < argc; ++i)
+ printf(" %s", argv[i]);
+ putchar('\n');
+ return 0;
+}
```</pre>
</div>
<div created="20160816212627236" creator="lh3" modified="20160816214517684" modifier="lh3" tags="[[Library Documentations]]" title="Kexpr: parsing mathematical expressions">