<li>Kdq: double-ended queue</li>
+<li>Ketopt: parsing command-line arguments</li>
+
<li>Kexpr: parsing mathematical expressions</li>
<li>Khash: generic hash table</li>
<div modified="20141201152658215" modifier="lh3" title="$:/view">
<pre>classic</pre>
</div>
-<div created="20141130022418365" creator="lh3" modified="20160822123859305" modifier="lh3" tags="TableOfContents" title="About">
+<div created="20141130022418365" creator="lh3" modified="20180831085110417" modifier="lh3" tags="TableOfContents" title="About">
<pre>[[Klib|https://github.com/attractivechaos/klib/]] is a standalone and lightweight C library distributed under [[MIT/X11 license|http://en.wikipedia.org/wiki/MIT_License]]. Most components are independent of external libraries, except the standard C library, and independent of each other. To use a component of this library, you only need to copy a couple of files to your source code tree without worrying about library dependencies.
Klib strives for efficiency and a small memory footprint. Some components, such as hash table, B-tree, vector and sorting algorithms, are among the most efficient implementations of similar algorithms or data structures in all programming languages, in terms of both speed and memory use.
* [[kdq.h|Kdq: double-ended queue]]: generic double-ended queue (de-queue).
* klist.h: generic single-linked list and memory pool.
* kstring.{h,c}: basic string library.
+* [[ketopt.h|Ketopt: parsing command-line arguments]]: command-line argument parser, similar to [[getopt_long|https://linux.die.net/man/3/getopt_long]].
* kmath.{h,c}: numerical routines including [[MT19937-64|http://en.wikipedia.org/wiki/Mersenne_twister]] pseudorandom number generator, basic nonlinear programming and a few special math functions.
* [[kson.{h,c}|Kson: simple JSON parser]]: simple [[JSON|http://www.json.org]] parser (no streaming)
-* [[kthread.c|Kthread: simple threading models]]: simple multi-threading models.
+* [[kthread.{h,c}|Kthread: simple threading models]]: simple multi-threading models.
!!!Components for more specific use cases
kdq_destroy(int, q);
return 0;
}
+```</pre>
+</div>
+<div created="20180831083058888" creator="lh3" modified="20180831084804032" 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.
+* Library code: [[ketopt.h|https://github.com/attractivechaos/klib/blob/master/ketopt.h]].
+* Dependencies: none
+
+!!Examples
+!!!Example 1: accept option `-x`, `-y arg`, `--foo`, `--bar=arg` and `--opt[=arg]`.
+```c
+#include <stdio.h>
+#include "ketopt.h"
+
+int main(int argc, char *argv[])
+{
+ static ko_longopt_t longopts[] = {
+ { "foo", ko_no_argument, 301 },
+ { "bar", ko_required_argument, 302 },
+ { "opt", ko_optional_argument, 303 },
+ { NULL, 0, 0 }
+ };
+ ketopt_t opt = KETOPT_INIT;
+ int i, c;
+ while ((c = ketopt(&opt, argc, argv, 1, "xy:", longopts)) >= 0) {
+ if (c == 'x') printf("-x\n");
+ else if (c == 'y') printf("-y %s\n", opt.arg);
+ else if (c == 301) printf("--foo\n");
+ else if (c == 302) printf("--bar %s\n", opt.arg? opt.arg : "(null)");
+ else if (c == 303) printf("--opt %s\n", opt.arg? opt.arg : "(null)");
+ else if (c == '?') printf("unknown opt: -%c\n", opt.opt? opt.opt : ':');
+ else if (c == ':') printf("missing arg: -%c\n", opt.opt? opt.opt : ':');
+ }
+ printf("Non-option arguments:");
+ for (i = opt.ind; i < argc; ++i)
+ printf(" %s", argv[i]);
+ fputc('\n', stderr);
+ return 0;
+}
+
```</pre>
</div>
<div created="20160816212627236" creator="lh3" modified="20160816214517684" modifier="lh3" tags="[[Library Documentations]]" title="Kexpr: parsing mathematical expressions">