dav prints command usage info if argument count is wrong

Sun, 22 Oct 2017 13:45:13 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 22 Oct 2017 13:45:13 +0200
changeset 332
5587282ebe1c
parent 331
9ca1e4706acc
child 333
000cdd124115

dav prints command usage info if argument count is wrong

dav/main.c file | annotate | diff | comparison | revisions
--- a/dav/main.c	Sun Oct 22 12:58:07 2017 +0200
+++ b/dav/main.c	Sun Oct 22 13:45:13 2017 +0200
@@ -155,31 +155,48 @@
     return ret;
 }
 
+static char *cmdusageinfo[] = {
+    "list [-altdepcR] [-u <date>] <url>",
+    "get [-pcRK] [-o <file>] [-u <date>] <url>",
+    "put [-pcR] [-k <key>] [-L <lock>] <url> <file>",
+    "mkdir [-pc] [-k <key>] [-L <lock>] <url>",
+    "remove [-pc] [-L <lock>] <url>",
+    "copy [-pcO] [-L <lock>] <url> <url>",
+    "move [-pcO] [-L <lock>] <url> <url>",
+    "get-property [-pc] [-n <uri>] <url> <property>",
+    "set-property [-pc] [-L <lock>] [-n <uri>] <url> <property> [value]",
+    "remove-property [-pc] [-n <uri>] <url> <property>",
+    "lock [-pc] [-T timeout] <url>",
+    "unlock [-pc] [-L <lock>] <url>",
+    "info [-pc] <url>",
+    "date [url]",
+    NULL
+};
+
+char* find_usage_str(char *cmd) {
+    sstr_t c = sstr(cmd);
+    for(int i=0;;i++) {
+        char *str = cmdusageinfo[i];
+        if(!str) {
+            break;
+        }
+        sstr_t u = sstr(str);
+        if(sstrprefix(u, c)) {
+            return str;
+        }
+    }
+    return NULL;
+}
+
 void print_usage(char *cmd) {
-    fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd);
-    fprintf(stderr, "Commands:\n");
-    fprintf(stderr, "        list [-altdepcR] [-u <date>] <url>\n");
-    fprintf(
-            stderr,
-            "        get [-pcRK] [-o <file>] [-u <date>] <url>\n");
-    fprintf(stderr, "        put [-pcR] [-k <key>] [-L <lock>] <url> <file>\n");
-    fprintf(stderr, "        mkdir [-pc] [-k <key>] [-L <lock>] <url>\n");
-    fprintf(stderr, "        remove [-pc] [-L <lock>] <url>\n");
-    fprintf(stderr, "        copy [-pcO] [-L <lock>] <url> <url>\n");
-    fprintf(stderr, "        move [-pcO] [-L <lock>] <url> <url>\n");
-    fprintf(
-            stderr,
-            "        get-property [-pc] [-n <uri>] <url> <property>\n");
-    fprintf(
-            stderr,
-            "        set-property [-pc] [-L <lock>] [-n <uri>] <url> <property> [value]\n");
-    fprintf(
-            stderr,
-            "        remove-property [-pc] [-n <uri>] <url> <property>\n");
-    fprintf(stderr, "        lock [-pc] [-T timeout] <url>\n");
-    fprintf(stderr, "        unlock [-pc] [-L <lock>] <url>\n");
-    fprintf(stderr, "        info [-pc] <url>\n");
-    fprintf(stderr, "        date [url]\n");
+    for(int i=0;;i++) {
+        char *str = cmdusageinfo[i];
+        if(!str) {
+            break;
+        }
+        fprintf(stderr, "        %s\n", str);
+    }
+    
     fprintf(stderr, "\n");
     fprintf(stderr, "Options:\n");
     fprintf(stderr,
@@ -364,6 +381,7 @@
 int cmd_list(CmdArgs *a) {
     if(a->argc != 1) {
         fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str("list"));
         return -1;
     }
     
@@ -633,6 +651,7 @@
     if(a->argc != 1) {
         // TODO: change this, when get supports retrieval of multiple files
         fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str("get"));
         return -1;
     }
     
@@ -790,6 +809,7 @@
     if(a->argc != 2) {
         // TODO: change, when put supports multiple files (however it should do)
         fprintf(stderr, "Too %s arguments\n", a->argc < 2 ? "few":"many");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str("put"));
         return -1;
     }
     
@@ -966,6 +986,7 @@
     if(a->argc != 1) {
         // TODO: change, when removal of multiple files is supported
         fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str("remove"));
         return -1;
     }
     
@@ -1009,6 +1030,7 @@
     if(a->argc != 1) {
         // TODO: change, when creation of multiple dirs is supported
         fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str("mkdir"));
         return -1;
     }
     
@@ -1066,6 +1088,7 @@
     if(a->argc != 2) {
         // TODO: change, when creation of multiple dirs is supported
         fprintf(stderr, "Too %s arguments\n", a->argc < 2 ? "few":"many");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str(cp ? "copy" : "move"));
         return -1;
     }
     
@@ -1169,6 +1192,7 @@
         return 0;
     } else {
         fprintf(stderr, "Too many arguments\n");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str("date"));
         return -1;
     }
     return 0;
@@ -1177,6 +1201,7 @@
 int cmd_get_property(CmdArgs *a) {
     if(a->argc < 2) {
         fprintf(stderr, "Too few arguments\n");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str("get-property"));
         return -1;
     }
     
@@ -1240,6 +1265,7 @@
 int cmd_set_property(CmdArgs *a) {
     if(a->argc < 2) {
         fprintf(stderr, "Too few arguments\n");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str("set-property"));
         return -1;
     }
     
@@ -1294,6 +1320,7 @@
 int cmd_remove_property(CmdArgs *a) {
     if(a->argc < 2) {
         fprintf(stderr, "Too few arguments\n");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str("remove-property"));
         return -1;
     }
     
@@ -1339,6 +1366,7 @@
 int cmd_lock(CmdArgs *a) {
     if(a->argc != 1) {
         fprintf(stderr, "Too %s arguments\n", a->argc > 1 ? "many" : "few");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str("lock"));
         return -1;
     }
     
@@ -1410,6 +1438,7 @@
 int cmd_unlock(CmdArgs *a) {
     if(a->argc != 1) {
         fprintf(stderr, "Too %s arguments\n", a->argc > 1 ? "many" : "few");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str("unlock"));
         return -1;
     }
     
@@ -1468,6 +1497,7 @@
 int cmd_info(CmdArgs *a) {
     if(a->argc < 1) {
         fprintf(stderr, "Too few arguments\n");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str("info"));
         return -1;
     }
     

mercurial