about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-04-14 23:36:00 +0100
committerJames Booth <boothj5@gmail.com>2014-04-14 23:36:00 +0100
commit3e69d6b71ed57964df92f172d8f7a041e798a2e3 (patch)
tree8ae92db067261c93e6a12fd72553b46fe49909c8
parent428d9eb936db2436ae72f2bf18afba635b15cbeb (diff)
downloadprofani-tty-3e69d6b71ed57964df92f172d8f7a041e798a2e3.tar.gz
Refactored cmd_join to use parse_options
-rw-r--r--src/command/commands.c48
-rw-r--r--src/tools/parser.c4
2 files changed, 17 insertions, 35 deletions
diff --git a/src/command/commands.c b/src/command/commands.c
index c03d294d..51334934 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -1551,7 +1551,6 @@ cmd_join(gchar **args, struct cmd_help_t help)
         return TRUE;
     }
 
-    int num_args = g_strv_length(args);
     char *room = NULL;
     char *nick = NULL;
     char *passwd = NULL;
@@ -1572,40 +1571,23 @@ cmd_join(gchar **args, struct cmd_help_t help)
     }
 
     // Additional args supplied
-    if (num_args > 1) {
-        char *opt1 = args[1];
-        char *opt1val = args[2];
-        char *opt2 = args[3];
-        char *opt2val = args[4];
-        if (opt1 != NULL) {
-            if (opt1val == NULL) {
-                cons_show("Usage: %s", help.usage);
-                cons_show("");
-                return TRUE;
-            }
-            if (strcmp(opt1, "nick") == 0) {
-                nick = opt1val;
-            } else if (strcmp(opt1, "password") == 0) {
-                passwd = opt1val;
-            } else {
-                cons_show("Usage: %s", help.usage);
-                cons_show("");
-                return TRUE;
-            }
-            if (opt2 != NULL) {
-                if (strcmp(opt2, "nick") == 0) {
-                    nick = opt2val;
-                } else if (strcmp(opt2, "password") == 0) {
-                    passwd = opt2val;
-                } else {
-                    cons_show("Usage: %s", help.usage);
-                    cons_show("");
-                    return TRUE;
-                }
-            }
-        }
+    GList *opt_keys = NULL;
+    opt_keys = g_list_append(opt_keys, "nick");
+    opt_keys = g_list_append(opt_keys, "password");
+    gboolean parsed;
+
+    GHashTable *options = parse_options(args, 1, opt_keys, &parsed);
+    if (!parsed) {
+        cons_show("Usage: %s", help.usage);
+        cons_show("");
+        return TRUE;
     }
 
+    nick = g_hash_table_lookup(options, "nick");
+    passwd = g_hash_table_lookup(options, "password");
+
+    options_destroy(options);
+
     // In the case that a nick wasn't provided by the optional args...
     if (nick == NULL) {
         nick = account->muc_nick;
diff --git a/src/tools/parser.c b/src/tools/parser.c
index b4403d06..6db522aa 100644
--- a/src/tools/parser.c
+++ b/src/tools/parser.c
@@ -390,13 +390,13 @@ parse_options(gchar **args, int start, GList *keys, gboolean *res)
     GList *found_keys = NULL;
     for (curr = start; curr < g_strv_length(args); curr+= 2) {
         // check if option valid
-        if (g_list_find(keys, args[curr]) == NULL) {
+        if (g_list_find_custom(keys, args[curr], (GCompareFunc)g_strcmp0) == NULL) {
             *res = FALSE;
             return options;
         }
 
         // check if duplicate
-        if (g_list_find(found_keys, args[curr]) != NULL) {
+        if (g_list_find_custom(found_keys, args[curr], (GCompareFunc)g_strcmp0) != NULL) {
             *res = FALSE;
             return options;
         }