* [PATCH 1/3] cli/show: convert keyword options to booleans [not found] <20170509021719.13086-1-david@tethera.net> @ 2017-10-11 14:35 ` David Bremner 0 siblings, 0 replies; 2+ messages in thread From: David Bremner @ 2017-10-11 14:35 UTC (permalink / raw) To: notmuch There are two keyword options here that impliment boolean options. It is simpler to use the built-in boolean argument handling, and also more robust against divergence in parsing boolean and keyword arguments. --- notmuch-show.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/notmuch-show.c b/notmuch-show.c index 7021008e..2f1904db 100644 --- a/notmuch-show.c +++ b/notmuch-show.c @@ -1028,8 +1028,8 @@ enum { /* The following is to allow future options to be added more easily */ enum { - EXCLUDE_TRUE, - EXCLUDE_FALSE, + EXCLUDE_FALSE = FALSE, + EXCLUDE_TRUE = TRUE, }; int @@ -1060,15 +1060,8 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]) { "raw", NOTMUCH_FORMAT_RAW }, { 0, 0 } } }, { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 }, - { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x', - (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE }, - { "false", EXCLUDE_FALSE }, - { 0, 0 } } }, - { NOTMUCH_OPT_KEYWORD, &entire_thread, "entire-thread", 't', - (notmuch_keyword_t []){ { "true", ENTIRE_THREAD_TRUE }, - { "false", ENTIRE_THREAD_FALSE }, - { "", ENTIRE_THREAD_TRUE }, - { 0, 0 } } }, + { NOTMUCH_OPT_BOOLEAN, &exclude, "exclude", 'x', 0 }, + { NOTMUCH_OPT_BOOLEAN, &entire_thread, "entire-thread", 't', 0 }, { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 }, { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.decrypt, "decrypt", 'd', 0 }, { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.verify, "verify", 'v', 0 }, -- 2.11.0 ^ permalink raw reply related [flat|nested] 2+ messages in thread
* v3 of spaces command line argument separator @ 2017-07-01 15:18 David Bremner 2017-07-01 15:18 ` [PATCH 1/3] cli/show: convert keyword options to booleans David Bremner 0 siblings, 1 reply; 2+ messages in thread From: David Bremner @ 2017-07-01 15:18 UTC (permalink / raw) To: notmuch, notmuch This obsoletes: id:20170605102923.1268-1-david@tethera.net Compared to the previous version this - rewrites the switch in parse_option in a less fancy way - fixes the error message for unrecognize options - adds some tests for --arg:val diff --git a/command-line-arguments.c b/command-line-arguments.c index 91e9e06f..dc517b06 100644 --- a/command-line-arguments.c +++ b/command-line-arguments.c @@ -172,22 +172,31 @@ parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_ if (try->output_var == NULL) INTERNAL_ERROR ("output pointer NULL for option %s", try->name); + notmuch_bool_t opt_status = FALSE; switch (try->opt_type) { case NOTMUCH_OPT_KEYWORD: case NOTMUCH_OPT_KEYWORD_FLAGS: - return _process_keyword_arg (try, next, value) ? opt_index + 1 : (-1); + opt_status = _process_keyword_arg (try, next, value); + break; case NOTMUCH_OPT_BOOLEAN: - return _process_boolean_arg (try, next, value) ? opt_index + 1 : (-1); + opt_status = _process_boolean_arg (try, next, value); + break; case NOTMUCH_OPT_INT: - return _process_int_arg (try, next, value) ? opt_index + 1 : (-1); + opt_status = _process_int_arg (try, next, value); + break; case NOTMUCH_OPT_STRING: - return _process_string_arg (try, next, value) ? opt_index + 1 : (-1); + opt_status = _process_string_arg (try, next, value); + break; case NOTMUCH_OPT_POSITION: case NOTMUCH_OPT_END: default: INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type); /*UNREACHED*/ } + if (opt_status) + return opt_index+1; + else + return -1; } return -1; } @@ -211,13 +220,14 @@ parse_arguments (int argc, char **argv, } } else { + int prev_opt_index = opt_index; + if (strlen (argv[opt_index]) == 2) return opt_index+1; opt_index = parse_option (argc, argv, options, opt_index); - /* this is currently broken as -1 is never returned */ if (opt_index < 0) { - fprintf (stderr, "Unrecognized option: %s\n", argv[opt_index]); + fprintf (stderr, "Unrecognized option: %s\n", argv[prev_opt_index]); more_args = FALSE; } } diff --git a/test/T690-command-line-args.sh b/test/T690-command-line-args.sh index 01de88eb..a4f4b5f5 100755 --- a/test/T690-command-line-args.sh +++ b/test/T690-command-line-args.sh @@ -6,7 +6,11 @@ test_description="command line arguments" add_message test_begin_subtest 'bad option to show' -test_expect_code 1 'notmuch show --this-is-not-an-option' +notmuch show --frobnicate >& OUTPUT +cat <<EOF > EXPECTED +Unrecognized option: --frobnicate +EOF +test_expect_equal_file EXPECTED OUTPUT test_begin_subtest 'string option with space' cp /dev/null EXPECTED @@ -18,6 +22,11 @@ cp /dev/null EXPECTED notmuch dump --output=foo.txt '*' >& OUTPUT test_expect_equal_file EXPECTED OUTPUT +test_begin_subtest 'string option with :' +cp /dev/null EXPECTED +notmuch dump --output:foo.txt '*' >& OUTPUT +test_expect_equal_file EXPECTED OUTPUT + test_begin_subtest 'single keyword option with space' cat <<EOF > EXPECTED id:msg-001@notmuch-test-suite @@ -25,6 +34,20 @@ EOF notmuch search --output messages '*' >& OUTPUT test_expect_equal_file EXPECTED OUTPUT +test_begin_subtest 'single keyword option with =' +cat <<EOF > EXPECTED +id:msg-001@notmuch-test-suite +EOF +notmuch search --output=messages '*' >& OUTPUT +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest 'single keyword option with :' +cat <<EOF > EXPECTED +id:msg-001@notmuch-test-suite +EOF +notmuch search --output:messages '*' >& OUTPUT +test_expect_equal_file EXPECTED OUTPUT + test_begin_subtest 'multiple keyword options with space' cat <<EOF > EXPECTED ["msg-001@notmuch-test-suite"] @@ -32,6 +55,13 @@ EOF notmuch search --output messages --format json '*' >& OUTPUT test_expect_equal_file EXPECTED OUTPUT +test_begin_subtest 'multiple keyword options with =' +cat <<EOF > EXPECTED +["msg-001@notmuch-test-suite"] +EOF +notmuch search --output=messages --format=json '*' >& OUTPUT +test_expect_equal_file EXPECTED OUTPUT + test_begin_subtest 'mixed space and = delimiters' cat <<EOF > EXPECTED ["msg-001@notmuch-test-suite"] @@ -39,11 +69,11 @@ EOF notmuch search --output messages --format=json '*' >& OUTPUT test_expect_equal_file EXPECTED OUTPUT -test_begin_subtest 'keyword option with =' +test_begin_subtest 'mixed space and : delimiters' cat <<EOF > EXPECTED ["msg-001@notmuch-test-suite"] EOF -notmuch search --output=messages --format=json '*' >& OUTPUT +notmuch search --output:messages --format json '*' >& OUTPUT test_expect_equal_file EXPECTED OUTPUT test_begin_subtest 'show --entire-thread' ^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH 1/3] cli/show: convert keyword options to booleans 2017-07-01 15:18 v3 of spaces command line argument separator David Bremner @ 2017-07-01 15:18 ` David Bremner 0 siblings, 0 replies; 2+ messages in thread From: David Bremner @ 2017-07-01 15:18 UTC (permalink / raw) To: notmuch, notmuch There are two keyword options here that impliment boolean options. It is simpler to use the built-in boolean argument handling, and also more robust against divergence in parsing boolean and keyword arguments. --- notmuch-show.c | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/notmuch-show.c b/notmuch-show.c index 3ce4b63c..7104db1f 100644 --- a/notmuch-show.c +++ b/notmuch-show.c @@ -1009,18 +1009,6 @@ static const notmuch_show_format_t *formatters[] = { [NOTMUCH_FORMAT_RAW] = &format_raw, }; -enum { - ENTIRE_THREAD_DEFAULT = -1, - ENTIRE_THREAD_FALSE = FALSE, - ENTIRE_THREAD_TRUE = TRUE, -}; - -/* The following is to allow future options to be added more easily */ -enum { - EXCLUDE_TRUE, - EXCLUDE_FALSE, -}; - int notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]) { @@ -1036,8 +1024,11 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]) .output_body = TRUE, }; int format = NOTMUCH_FORMAT_NOT_SPECIFIED; - int exclude = EXCLUDE_TRUE; - int entire_thread = ENTIRE_THREAD_DEFAULT; + int exclude = TRUE; + + /* This value corresponds to neither true nor false being passed + * on the command line */ + int entire_thread = -1; notmuch_bool_t single_message; notmuch_opt_desc_t options[] = { @@ -1049,15 +1040,8 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]) { "raw", NOTMUCH_FORMAT_RAW }, { 0, 0 } } }, { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 }, - { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x', - (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE }, - { "false", EXCLUDE_FALSE }, - { 0, 0 } } }, - { NOTMUCH_OPT_KEYWORD, &entire_thread, "entire-thread", 't', - (notmuch_keyword_t []){ { "true", ENTIRE_THREAD_TRUE }, - { "false", ENTIRE_THREAD_FALSE }, - { "", ENTIRE_THREAD_TRUE }, - { 0, 0 } } }, + { NOTMUCH_OPT_BOOLEAN, &exclude, "exclude", 'x', 0 }, + { NOTMUCH_OPT_BOOLEAN, &entire_thread, "entire-thread", 't', 0 }, { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 }, { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.decrypt, "decrypt", 'd', 0 }, { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.verify, "verify", 'v', 0 }, @@ -1102,7 +1086,7 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]) /* Default is entire-thread = FALSE except for format=json and * format=sexp. */ - if (entire_thread == ENTIRE_THREAD_DEFAULT) { + if (entire_thread != FALSE && entire_thread != TRUE) { if (format == NOTMUCH_FORMAT_JSON || format == NOTMUCH_FORMAT_SEXP) params.entire_thread = TRUE; else @@ -1182,7 +1166,7 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]) } } - if (exclude == EXCLUDE_FALSE) { + if (exclude == FALSE) { notmuch_query_set_omit_excluded (query, FALSE); params.omit_excluded = FALSE; } -- 2.11.0 ^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2017-10-11 14:35 UTC | newest] Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- [not found] <20170509021719.13086-1-david@tethera.net> 2017-10-11 14:35 ` [PATCH 1/3] cli/show: convert keyword options to booleans David Bremner 2017-07-01 15:18 v3 of spaces command line argument separator David Bremner 2017-07-01 15:18 ` [PATCH 1/3] cli/show: convert keyword options to booleans David Bremner
Code repositories for project(s) associated with this public inbox https://yhetil.org/notmuch.git/ This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).