mirror of
https://github.com/profanity-im/profanity.git
synced 2025-01-03 14:57:42 -05:00
Check for duplicate options in option parser
This commit is contained in:
parent
8d77930ece
commit
2419737006
@ -385,14 +385,31 @@ parse_options(gchar **args, int start, GList *keys, gboolean *res)
|
||||
return options;
|
||||
}
|
||||
|
||||
// check each option is valid and has value, failure if not
|
||||
// validate options
|
||||
int curr;
|
||||
GList *found_keys = NULL;
|
||||
for (curr = start; curr < g_strv_length(args); curr+= 2) {
|
||||
if ((g_list_find(keys, args[curr]) == NULL) || (args[curr+1] == NULL)) {
|
||||
// check if option valid
|
||||
if (g_list_find(keys, args[curr]) == NULL) {
|
||||
*res = FALSE;
|
||||
return options;
|
||||
}
|
||||
|
||||
// check if duplicate
|
||||
if (g_list_find(found_keys, args[curr]) != NULL) {
|
||||
*res = FALSE;
|
||||
return options;
|
||||
}
|
||||
|
||||
// check value given
|
||||
if (args[curr+1] == NULL) {
|
||||
*res = FALSE;
|
||||
return options;
|
||||
}
|
||||
|
||||
found_keys = g_list_append(found_keys, args[curr]);
|
||||
}
|
||||
g_list_free(found_keys);
|
||||
|
||||
// create map
|
||||
options = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
|
@ -666,5 +666,25 @@ parse_options_when_unknown_opt_sets_error(void **state)
|
||||
assert_null(options);
|
||||
assert_false(res);
|
||||
|
||||
options_destroy(options);
|
||||
}
|
||||
|
||||
void
|
||||
parse_options_with_duplicated_option_sets_error(void **state)
|
||||
{
|
||||
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", "opt1", "val3", NULL };
|
||||
|
||||
GList *keys = NULL;
|
||||
keys = g_list_append(keys, "opt1");
|
||||
keys = g_list_append(keys, "opt2");
|
||||
keys = g_list_append(keys, "opt3");
|
||||
|
||||
gboolean res = TRUE;
|
||||
|
||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
||||
|
||||
assert_null(options);
|
||||
assert_false(res);
|
||||
|
||||
options_destroy(options);
|
||||
}
|
@ -46,4 +46,5 @@ void parse_options_when_opt2_no_val_sets_error(void **state);
|
||||
void parse_options_when_two_returns_map(void **state);
|
||||
void parse_options_when_opt3_no_val_sets_error(void **state);
|
||||
void parse_options_when_three_returns_map(void **state);
|
||||
void parse_options_when_unknown_opt_sets_error(void **state);
|
||||
void parse_options_when_unknown_opt_sets_error(void **state);
|
||||
void parse_options_with_duplicated_option_sets_error(void **state);
|
||||
|
@ -173,6 +173,7 @@ int main(int argc, char* argv[]) {
|
||||
unit_test(parse_options_when_opt3_no_val_sets_error),
|
||||
unit_test(parse_options_when_three_returns_map),
|
||||
unit_test(parse_options_when_unknown_opt_sets_error),
|
||||
unit_test(parse_options_with_duplicated_option_sets_error),
|
||||
|
||||
unit_test(empty_list_when_none_added),
|
||||
unit_test(contains_one_element),
|
||||
|
Loading…
Reference in New Issue
Block a user