[rfc patch 2/2] cli: add space seperator for keyword, string, and int arguments

David Bremner david at tethera.net
Tue Mar 28 05:00:42 PDT 2017


Defer the complication of optional boolean arguments for later (never?).
---
 command-line-arguments.c       | 11 +++++++++++
 test/T690-command-line-args.sh | 40 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/command-line-arguments.c b/command-line-arguments.c
index 72b950e9..91e9e06f 100644
--- a/command-line-arguments.c
+++ b/command-line-arguments.c
@@ -133,6 +133,11 @@ parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_
 
     const char *arg = _arg + 2; /* _arg starts with -- */
     const notmuch_opt_desc_t *try;
+
+    const char *next_arg = NULL;
+    if (opt_index < argc - 1  && strncmp (argv[opt_index + 1], "--", 2) != 0)
+	next_arg = argv[opt_index + 1];
+
     for (try = options; try->opt_type != NOTMUCH_OPT_END; try++) {
 	if (try->opt_type == NOTMUCH_OPT_INHERIT) {
 	    int new_index = parse_option (argc, argv, try->output_var, opt_index);
@@ -158,6 +163,12 @@ parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_
 	if (next != '=' && next != ':' && next != '\0')
 	    continue;
 
+	if (next == '\0' && next_arg != NULL && try->opt_type != NOTMUCH_OPT_BOOLEAN) {
+	    next = ' ';
+	    value = next_arg;
+	    opt_index ++;
+	}
+
 	if (try->output_var == NULL)
 	    INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
 
diff --git a/test/T690-command-line-args.sh b/test/T690-command-line-args.sh
index 01592749..8d98abe0 100755
--- a/test/T690-command-line-args.sh
+++ b/test/T690-command-line-args.sh
@@ -3,9 +3,47 @@
 test_description="command line arguments"
 . ./test-lib.sh || exit 1
 
-NOTMUCH_NEW > /dev/null
+add_message
 
 test_begin_subtest 'bad option to show'
 test_expect_code 1 'notmuch show --this-is-not-an-option'
 
+test_begin_subtest 'string option with space'
+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 at 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 at 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 at notmuch-test-suite"]
+EOF
+notmuch search --output messages --format=json '*' >& OUTPUT
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest 'keyword option with ='
+cat <<EOF > EXPECTED
+["msg-001 at notmuch-test-suite"]
+EOF
+notmuch search --output=messages --format=json '*' >& OUTPUT
+test_expect_equal_file EXPECTED OUTPUT
+
 test_done
-- 
2.11.0



More information about the notmuch mailing list