]> git.ozlabs.org Git - ccan/blobdiff - ccan/opt/_info
opt: new module to parse commandline options.
[ccan] / ccan / opt / _info
diff --git a/ccan/opt/_info b/ccan/opt/_info
new file mode 100644 (file)
index 0000000..54ae077
--- /dev/null
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <string.h>
+#include "config.h"
+
+/**
+ * opt - simple command line parsing
+ *
+ * Simple but powerful command line parsing, built on top of getopt_long.
+ *
+ * Example:
+ * #include <ccan/opt/opt.h>
+ * #include <stdio.h>
+ * #include <stdlib.h>
+ * 
+ * static bool someflag;
+ * static int verbose;
+ * static char *somestring;
+ * 
+ * static struct opt_table opts[] = {
+ *     { OPT_WITHOUT_ARG("verbose", 'v', opt_inc_intval, &verbose),
+ *       "Verbose mode (can be specified more than once)" },
+ *     { OPT_WITHOUT_ARG("someflag", 0, opt_set_bool, &someflag),
+ *       "Set someflag" },
+ *     { OPT_WITH_ARG("somestring", 0, opt_set_charp, &somestring),
+ *       "Set somestring to <arg>" },
+ *     { OPT_WITHOUT_ARG("usage", 0, opt_usage_and_exit,
+ *                       "args...\nA silly test program."),
+ *       "Print this message." },
+ *     OPT_ENDTABLE
+ * };
+ * 
+ * int main(int argc, char *argv[])
+ * {
+ *     int i;
+ * 
+ *     opt_register_table(opts);
+ *     // For fun, register an extra one.
+ *     opt_register_noarg("no-someflag", 0, opt_set_invbool, &someflag,
+ *                        "Unset someflag");
+ *     if (!opt_parse(&argc, argv, opt_log_stderr))
+ *             exit(1);
+ * 
+ *     printf("someflag = %i, verbose = %i, somestring = %s\n",
+ *            someflag, verbose, somestring);
+ *     printf("%u args left over:", argc - 1);
+ *     for (i = 1; i < argc; i++)
+ *             printf(" %s", argv[i]);
+ *     printf("\n");
+ *     return 0;
+ * }
+ *
+ * Licence: GPL (3 or any later version)
+ * Author: Rusty Russell <rusty@rustcorp.com.au>
+ */
+int main(int argc, char *argv[])
+{
+       if (argc != 2)
+               return 1;
+
+       if (strcmp(argv[1], "depends") == 0) {
+               return 0;
+       }
+
+       return 1;
+}