From: Heng Li Date: Fri, 31 Aug 2018 11:48:51 +0000 (-1000) Subject: subcommand example X-Git-Url: http://www.kaiwu.me/postgresql/commit/static/gitweb.js?a=commitdiff_plain;h=37919f884218854dc0d6b2fa7b22994beb984e63;p=klib.git subcommand example --- diff --git a/index.html b/index.html index 4001d1d..20a4379 100644 --- a/index.html +++ b/index.html @@ -8249,7 +8249,7 @@ pre .xml .cdata {
lh3
-
+

 
@@ -8638,7 +8638,7 @@ int main(void) } ```
-
+
!!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.
@@ -8673,10 +8673,34 @@ int main(int argc, char *argv[])
   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;
+}
 ```