cli: Add support for parsing keyword-flag arguments

This allows having multiple --foo=bar --foo=baz options on the command
line, with the corresponding values OR'd together.

[Test added by Michal Sojka]
This commit is contained in:
Jani Nikula 2014-10-31 22:53:56 +01:00 committed by David Bremner
parent dc39811948
commit ecc4a9a644
4 changed files with 17 additions and 2 deletions

View file

@ -23,6 +23,9 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char
while (keywords->name) { while (keywords->name) {
if (strcmp (arg_str, keywords->name) == 0) { if (strcmp (arg_str, keywords->name) == 0) {
if (arg_desc->output_var) { if (arg_desc->output_var) {
if (arg_desc->opt_type == NOTMUCH_OPT_KEYWORD_FLAGS)
*((int *)arg_desc->output_var) |= keywords->value;
else
*((int *)arg_desc->output_var) = keywords->value; *((int *)arg_desc->output_var) = keywords->value;
} }
return TRUE; return TRUE;
@ -152,6 +155,7 @@ parse_option (const char *arg,
switch (try->opt_type) { switch (try->opt_type) {
case NOTMUCH_OPT_KEYWORD: case NOTMUCH_OPT_KEYWORD:
case NOTMUCH_OPT_KEYWORD_FLAGS:
return _process_keyword_arg (try, next, value); return _process_keyword_arg (try, next, value);
case NOTMUCH_OPT_BOOLEAN: case NOTMUCH_OPT_BOOLEAN:
return _process_boolean_arg (try, next, value); return _process_boolean_arg (try, next, value);

View file

@ -8,6 +8,7 @@ enum notmuch_opt_type {
NOTMUCH_OPT_BOOLEAN, /* --verbose */ NOTMUCH_OPT_BOOLEAN, /* --verbose */
NOTMUCH_OPT_INT, /* --frob=8 */ NOTMUCH_OPT_INT, /* --frob=8 */
NOTMUCH_OPT_KEYWORD, /* --format=raw|json|text */ NOTMUCH_OPT_KEYWORD, /* --format=raw|json|text */
NOTMUCH_OPT_KEYWORD_FLAGS, /* the above with values OR'd together */
NOTMUCH_OPT_STRING, /* --file=/tmp/gnarf.txt */ NOTMUCH_OPT_STRING, /* --file=/tmp/gnarf.txt */
NOTMUCH_OPT_POSITION /* notmuch dump pos_arg */ NOTMUCH_OPT_POSITION /* notmuch dump pos_arg */
}; };

View file

@ -3,9 +3,10 @@ test_description="argument parsing"
. ./test-lib.sh . ./test-lib.sh
test_begin_subtest "sanity check" test_begin_subtest "sanity check"
$TEST_DIRECTORY/arg-test pos1 --keyword=one --string=foo pos2 --int=7 > OUTPUT $TEST_DIRECTORY/arg-test pos1 --keyword=one --string=foo pos2 --int=7 --flag=one --flag=three > OUTPUT
cat <<EOF > EXPECTED cat <<EOF > EXPECTED
keyword 1 keyword 1
flags 5
int 7 int 7
string foo string foo
positional arg 1 pos1 positional arg 1 pos1

View file

@ -7,6 +7,7 @@ int main(int argc, char **argv){
int opt_index=1; int opt_index=1;
int kw_val=0; int kw_val=0;
int fl_val=0;
int int_val=0; int int_val=0;
char *pos_arg1=NULL; char *pos_arg1=NULL;
char *pos_arg2=NULL; char *pos_arg2=NULL;
@ -17,6 +18,11 @@ int main(int argc, char **argv){
(notmuch_keyword_t []){ { "one", 1 }, (notmuch_keyword_t []){ { "one", 1 },
{ "two", 2 }, { "two", 2 },
{ 0, 0 } } }, { 0, 0 } } },
{ NOTMUCH_OPT_KEYWORD_FLAGS, &fl_val, "flag", 'f',
(notmuch_keyword_t []){ { "one", 1 << 0},
{ "two", 1 << 1 },
{ "three", 1 << 2 },
{ 0, 0 } } },
{ NOTMUCH_OPT_INT, &int_val, "int", 'i', 0}, { NOTMUCH_OPT_INT, &int_val, "int", 'i', 0},
{ NOTMUCH_OPT_STRING, &string_val, "string", 's', 0}, { NOTMUCH_OPT_STRING, &string_val, "string", 's', 0},
{ NOTMUCH_OPT_POSITION, &pos_arg1, 0,0, 0}, { NOTMUCH_OPT_POSITION, &pos_arg1, 0,0, 0},
@ -31,6 +37,9 @@ int main(int argc, char **argv){
if (kw_val) if (kw_val)
printf("keyword %d\n", kw_val); printf("keyword %d\n", kw_val);
if (fl_val)
printf("flags %d\n", fl_val);
if (int_val) if (int_val)
printf("int %d\n", int_val); printf("int %d\n", int_val);