Simplified parse_options to take first option as argument
This commit is contained in:
@@ -71,7 +71,7 @@ cmd_connect(gchar **args, struct cmd_help_t help)
|
|||||||
gchar *opt_keys[] = { "server", "port", NULL };
|
gchar *opt_keys[] = { "server", "port", NULL };
|
||||||
gboolean parsed;
|
gboolean parsed;
|
||||||
|
|
||||||
GHashTable *options = parse_options(args, 1, opt_keys, &parsed);
|
GHashTable *options = parse_options(&args[1], opt_keys, &parsed);
|
||||||
if (!parsed) {
|
if (!parsed) {
|
||||||
cons_show("Usage: %s", help.usage);
|
cons_show("Usage: %s", help.usage);
|
||||||
cons_show("");
|
cons_show("");
|
||||||
@@ -1572,7 +1572,7 @@ cmd_join(gchar **args, struct cmd_help_t help)
|
|||||||
gchar *opt_keys[] = { "nick", "password", NULL };
|
gchar *opt_keys[] = { "nick", "password", NULL };
|
||||||
gboolean parsed;
|
gboolean parsed;
|
||||||
|
|
||||||
GHashTable *options = parse_options(args, 1, opt_keys, &parsed);
|
GHashTable *options = parse_options(&args[1], opt_keys, &parsed);
|
||||||
if (!parsed) {
|
if (!parsed) {
|
||||||
cons_show("Usage: %s", help.usage);
|
cons_show("Usage: %s", help.usage);
|
||||||
cons_show("");
|
cons_show("");
|
||||||
|
|||||||
@@ -374,7 +374,7 @@ get_start(char *string, int tokens)
|
|||||||
}
|
}
|
||||||
|
|
||||||
GHashTable *
|
GHashTable *
|
||||||
parse_options(gchar **args, int start, gchar **opt_keys, gboolean *res)
|
parse_options(gchar **args, gchar **opt_keys, gboolean *res)
|
||||||
{
|
{
|
||||||
GList *keys = NULL;
|
GList *keys = NULL;
|
||||||
int i;
|
int i;
|
||||||
@@ -385,7 +385,7 @@ parse_options(gchar **args, int start, gchar **opt_keys, gboolean *res)
|
|||||||
GHashTable *options = NULL;
|
GHashTable *options = NULL;
|
||||||
|
|
||||||
// no options found, success
|
// no options found, success
|
||||||
if (args[start] == NULL) {
|
if (args[0] == NULL) {
|
||||||
options = g_hash_table_new(g_str_hash, g_str_equal);
|
options = g_hash_table_new(g_str_hash, g_str_equal);
|
||||||
*res = TRUE;
|
*res = TRUE;
|
||||||
g_list_free(keys);
|
g_list_free(keys);
|
||||||
@@ -395,7 +395,7 @@ parse_options(gchar **args, int start, gchar **opt_keys, gboolean *res)
|
|||||||
// validate options
|
// validate options
|
||||||
int curr;
|
int curr;
|
||||||
GList *found_keys = NULL;
|
GList *found_keys = NULL;
|
||||||
for (curr = start; curr < g_strv_length(args); curr+= 2) {
|
for (curr = 0; curr < g_strv_length(args); curr+= 2) {
|
||||||
// check if option valid
|
// check if option valid
|
||||||
if (g_list_find_custom(keys, args[curr], (GCompareFunc)g_strcmp0) == NULL) {
|
if (g_list_find_custom(keys, args[curr], (GCompareFunc)g_strcmp0) == NULL) {
|
||||||
*res = FALSE;
|
*res = FALSE;
|
||||||
@@ -425,7 +425,7 @@ parse_options(gchar **args, int start, gchar **opt_keys, gboolean *res)
|
|||||||
// create map
|
// create map
|
||||||
options = g_hash_table_new(g_str_hash, g_str_equal);
|
options = g_hash_table_new(g_str_hash, g_str_equal);
|
||||||
*res = TRUE;
|
*res = TRUE;
|
||||||
for (curr = start; curr < g_strv_length(args); curr+=2) {
|
for (curr = 0; curr < g_strv_length(args); curr+=2) {
|
||||||
g_hash_table_insert(options, args[curr], args[curr+1]);
|
g_hash_table_insert(options, args[curr], args[curr+1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ gchar** parse_args(const char * const inp, int min, int max, gboolean *result);
|
|||||||
gchar** parse_args_with_freetext(const char * const inp, int min, int max, gboolean *result);
|
gchar** parse_args_with_freetext(const char * const inp, int min, int max, gboolean *result);
|
||||||
int count_tokens(char *string);
|
int count_tokens(char *string);
|
||||||
char* get_start(char *string, int tokens);
|
char* get_start(char *string, int tokens);
|
||||||
GHashTable* parse_options(gchar **args, int start, gchar **keys, gboolean *res);
|
GHashTable* parse_options(gchar **args, gchar **keys, gboolean *res);
|
||||||
void options_destroy(GHashTable *options);
|
void options_destroy(GHashTable *options);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -512,7 +512,7 @@ parse_options_when_none_returns_empty_hasmap(void **state)
|
|||||||
|
|
||||||
gboolean res = FALSE;
|
gboolean res = FALSE;
|
||||||
|
|
||||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
GHashTable *options = parse_options(&args[2], keys, &res);
|
||||||
|
|
||||||
assert_true(options != NULL);
|
assert_true(options != NULL);
|
||||||
assert_int_equal(0, g_hash_table_size(options));
|
assert_int_equal(0, g_hash_table_size(options));
|
||||||
@@ -529,7 +529,7 @@ parse_options_when_opt1_no_val_sets_error(void **state)
|
|||||||
|
|
||||||
gboolean res = TRUE;
|
gboolean res = TRUE;
|
||||||
|
|
||||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
GHashTable *options = parse_options(&args[2], keys, &res);
|
||||||
|
|
||||||
assert_null(options);
|
assert_null(options);
|
||||||
assert_false(res);
|
assert_false(res);
|
||||||
@@ -545,7 +545,7 @@ parse_options_when_one_returns_map(void **state)
|
|||||||
|
|
||||||
gboolean res = FALSE;
|
gboolean res = FALSE;
|
||||||
|
|
||||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
GHashTable *options = parse_options(&args[2], keys, &res);
|
||||||
|
|
||||||
assert_int_equal(1, g_hash_table_size(options));
|
assert_int_equal(1, g_hash_table_size(options));
|
||||||
assert_true(g_hash_table_contains(options, "opt1"));
|
assert_true(g_hash_table_contains(options, "opt1"));
|
||||||
@@ -563,7 +563,7 @@ parse_options_when_opt2_no_val_sets_error(void **state)
|
|||||||
|
|
||||||
gboolean res = TRUE;
|
gboolean res = TRUE;
|
||||||
|
|
||||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
GHashTable *options = parse_options(&args[2], keys, &res);
|
||||||
|
|
||||||
assert_null(options);
|
assert_null(options);
|
||||||
assert_false(res);
|
assert_false(res);
|
||||||
@@ -579,7 +579,7 @@ parse_options_when_two_returns_map(void **state)
|
|||||||
|
|
||||||
gboolean res = FALSE;
|
gboolean res = FALSE;
|
||||||
|
|
||||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
GHashTable *options = parse_options(&args[2], keys, &res);
|
||||||
|
|
||||||
assert_int_equal(2, g_hash_table_size(options));
|
assert_int_equal(2, g_hash_table_size(options));
|
||||||
assert_true(g_hash_table_contains(options, "opt1"));
|
assert_true(g_hash_table_contains(options, "opt1"));
|
||||||
@@ -599,7 +599,7 @@ parse_options_when_opt3_no_val_sets_error(void **state)
|
|||||||
|
|
||||||
gboolean res = TRUE;
|
gboolean res = TRUE;
|
||||||
|
|
||||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
GHashTable *options = parse_options(&args[2], keys, &res);
|
||||||
|
|
||||||
assert_null(options);
|
assert_null(options);
|
||||||
assert_false(res);
|
assert_false(res);
|
||||||
@@ -615,7 +615,7 @@ parse_options_when_three_returns_map(void **state)
|
|||||||
|
|
||||||
gboolean res = FALSE;
|
gboolean res = FALSE;
|
||||||
|
|
||||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
GHashTable *options = parse_options(&args[2], keys, &res);
|
||||||
|
|
||||||
assert_int_equal(3, g_hash_table_size(options));
|
assert_int_equal(3, g_hash_table_size(options));
|
||||||
assert_true(g_hash_table_contains(options, "opt1"));
|
assert_true(g_hash_table_contains(options, "opt1"));
|
||||||
@@ -637,7 +637,7 @@ parse_options_when_unknown_opt_sets_error(void **state)
|
|||||||
|
|
||||||
gboolean res = TRUE;
|
gboolean res = TRUE;
|
||||||
|
|
||||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
GHashTable *options = parse_options(&args[2], keys, &res);
|
||||||
|
|
||||||
assert_null(options);
|
assert_null(options);
|
||||||
assert_false(res);
|
assert_false(res);
|
||||||
@@ -653,7 +653,7 @@ parse_options_with_duplicated_option_sets_error(void **state)
|
|||||||
|
|
||||||
gboolean res = TRUE;
|
gboolean res = TRUE;
|
||||||
|
|
||||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
GHashTable *options = parse_options(&args[2], keys, &res);
|
||||||
|
|
||||||
assert_null(options);
|
assert_null(options);
|
||||||
assert_false(res);
|
assert_false(res);
|
||||||
|
|||||||
Reference in New Issue
Block a user