]> git.mdlowis.com Git - proto/aos.git/commitdiff
completed option parser. Just need to implement default option hook
authorMichael D. Lowis <mike.lowis@gentex.com>
Fri, 18 Dec 2020 21:50:15 +0000 (16:50 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Fri, 18 Dec 2020 21:50:15 +0000 (16:50 -0500)
inc/liba.h
lib/a/opts_parse.c
lib/a/opts_printhelp.c
lib/a/usage.c [new file with mode: 0644]

index 68d4039f524f31aa2ce86ffe442391d04ce7b24e..dfb52d1f56d944afdf4c37f8d0e2cb67b0138821 100644 (file)
 */
 
 /* rename user's main routine so GC is auto-initialized and options parsed */
-extern int usermain(int argc, char** argv);
-#define main usermain
+extern int user_main(int argc, char** argv);
+#define main user_main
+
+extern void opts_setoption(int sname, char* lname, char* arg);
+
 
 /*
     Garbage Collector Interface
index 1e18855683c7c38aee8ae4eaf7527d4a52b6b090..6f88b2724f3b9e7ee559b8b3d5140881ae91132b 100644 (file)
@@ -1,5 +1,19 @@
 #include <liba.h>
 
+extern void opts_setoption(int sname, char* lname, char* arg)
+{
+    if (sname == 'h')
+    {
+        opts_printhelp();
+        exit(1);
+    }
+    else
+    {
+        printf(" --%s=%s\n", lname, arg);
+    }
+}
+
+
 static OptionDescriptor_T* lookup_opt(int optc, OptionDescriptor_T* optv, char* flag)
 {
     OptionDescriptor_T* opt = NULL;
@@ -53,6 +67,7 @@ static void parse_longopt(int optc, OptionDescriptor_T* optv, int* currp, char**
             }
         }
         printf("handled option: '--%s=%s'\n", flag, arg);
+        opts_setoption(od->sname, od->lname, arg);
     }
 
     /* now repair the split if we made one */
@@ -65,8 +80,47 @@ static void parse_longopt(int optc, OptionDescriptor_T* optv, int* currp, char**
 
 static void parse_shortopt(int optc, OptionDescriptor_T* optv, int* currp, char** argv)
 {
-    (void)optc, (void)optv;
-    printf("short: %s\n", argv[*currp]);
+    char* argstr = argv[*currp]+1;
+    char flag[] = "\0\0";
+    for (; *argstr; argstr++)
+    {
+        /* get the flag and the possible argument */
+        flag[0] = *argstr;
+        OptionDescriptor_T* od = lookup_opt(optc, optv, flag);
+        if (!od)
+        {
+            fatal("unknown option: '%s'", flag);
+        }
+        else if (od->hasarg)
+        {
+            char* arg = argstr+1;
+            if (*arg != '\0')
+            {
+                arg += (*arg == '=' ? 1 : 0);
+                opts_setoption(od->sname, od->lname, arg);
+                break;
+            }
+            else if (!*arg)
+            {
+                arg = argv[*currp + 1];
+                if (!arg)
+                {
+                    fatal("expected argument for option: '%s'", flag);
+                }
+                opts_setoption(od->sname, od->lname, arg);
+                *currp += 1;
+                break;
+            }
+            else
+            {
+                fatal("expected argument for option: '%s'", flag);
+            }
+        }
+        else
+        {
+            opts_setoption(od->sname, od->lname, NULL);
+        }
+    }
     *currp += 1;
 }
 
index 20f1d89c66c90f8ee75a0b8ec02fe4ab80f0acfa..ad03d5d24c1a336397c96293f4eb5abe0c3ca35e 100644 (file)
@@ -34,8 +34,12 @@ void opts_printhelp(void)
         }
     }
 
+    if (Usage)
+    {
+        printf("usage: %s\n\n", Usage);
+    }
+
     /* print option help messages */
-    printf("usage: %s\n\n", Usage);
     for (Option_T* curr = opts; curr->name; curr++)
     {
         int remain = padding;
diff --git a/lib/a/usage.c b/lib/a/usage.c
new file mode 100644 (file)
index 0000000..6eac852
--- /dev/null
@@ -0,0 +1,3 @@
+#include <liba.h>
+
+char* Usage = 0;
\ No newline at end of file