From: Heng Li Date: Fri, 31 Aug 2018 08:53:35 +0000 (-1000) Subject: documented ketopt.h X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=6b13480d2fba583897518d76b6cc4cc5c09ca6ca;p=klib.git documented ketopt.h --- diff --git a/index.html b/index.html index e84b024..3513f3a 100644 --- a/index.html +++ b/index.html @@ -189,6 +189,8 @@ Error message and password prompt
  • Kdq: double-ended queue
  • +
  • Ketopt: parsing command-line arguments
  • +
  • Kexpr: parsing mathematical expressions
  • Khash: generic hash table
  • @@ -8382,7 +8384,7 @@ pre .xml .cdata {
    classic
    -
    +
    [[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.
    @@ -8397,9 +8399,10 @@ Klib strives for efficiency and a small memory footprint. Some components, such
     * [[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
     
    @@ -8633,6 +8636,47 @@ int main(void)
     	kdq_destroy(int, q);
     	return 0;
     }
    +```
    +
    +
    +
    !!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;
    +}
    +
     ```