[cig-commits] r6894 - cs/cigma/trunk/tools

luis at geodynamics.org luis at geodynamics.org
Wed May 16 10:18:17 PDT 2007


Author: luis
Date: 2007-05-16 10:18:17 -0700 (Wed, 16 May 2007)
New Revision: 6894

Added:
   cs/cigma/trunk/tools/skel-cmd.c
Log:
Template file for new commands which relies on the argtable2 library
for parsing the command line arguments.


Added: cs/cigma/trunk/tools/skel-cmd.c
===================================================================
--- cs/cigma/trunk/tools/skel-cmd.c	2007-05-16 17:16:06 UTC (rev 6893)
+++ cs/cigma/trunk/tools/skel-cmd.c	2007-05-16 17:18:17 UTC (rev 6894)
@@ -0,0 +1,79 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <argtable2.h>
+
+int skel_command(const char *outfile, int counter,
+                 const char **args, int nargs)
+{
+    int i;
+    printf("skel:\n");
+    printf("\toutfile = \"%s\"\n", outfile);
+    printf("\tcounter = %d\n", counter);
+    printf("\tnargs   = %d\n", nargs);
+    for (i = 0; i < nargs; i++)
+    {
+        printf("\targs[%d] = \"%s\"\n", i, args[i]);
+    }
+    
+    return 0;
+}
+
+int skel_main(int argc, char *argv[])
+{
+    const char *progname = "cigma skel";
+
+    struct arg_lit  *help     = arg_lit0 ("h", "help", "display this help and exit");
+    struct arg_file *outfile  = arg_file0("o", NULL,  "<output>", "output file");
+    struct arg_int  *N        = arg_int0 ("n", NULL,  "<count>", "counter");
+    struct arg_str  *new_argv = arg_strn (NULL, NULL, "<arg>", 0, argc+2, "command line arguments");
+    struct arg_end  *end      = arg_end(20);
+
+    void *argtable[] = {help, outfile, N, new_argv, end};
+
+    int nerrors;
+    int exitcode = 0;
+
+    /* verify the argtable[] entries were allocated successfully */
+    if (arg_nullcheck(argtable) != 0)
+    {
+        fprintf(stderr, "%s: failed to allocate argtable\n", progname);
+        exitcode = 1;
+        goto exit;
+    }
+
+    /* set any command line default values prior to parsing */
+
+    outfile->filename[0] = "-";
+    N->ival[0] = 9;
+
+    /* Parse the command line as defined by argtable[] */
+    nerrors = arg_parse(argc, argv, argtable);
+
+    /* special case: '--help' takes precedence over error reporting */
+    if (help->count > 0)
+    {
+        printf("Usage: %s", progname);
+        arg_print_syntax(stdout, argtable, "\n\n");
+        arg_print_glossary(stdout, argtable, "  %-25s %s\n");
+        exitcode = 0;
+        goto exit;
+    }
+
+    /* If the parser returned any errors then display them and exit */
+    if (nerrors > 0)
+    {
+        /* Display the error details contained in the arg_end struct. */
+        arg_print_errors(stdout, end, progname);
+        exitcode = 1;
+        goto exit;
+    }
+
+    /* normal case: take the command line options */
+    exitcode = skel_command(outfile->filename[0], N->ival[0],
+                            new_argv->sval, new_argv->count);
+
+exit:
+    arg_freetable(argtable, sizeof(argtable)/sizeof(argtable[0]));
+
+    return exitcode;
+}



More information about the cig-commits mailing list