v3 of spaces command line argument separator

classic Classic list List threaded Threaded
5 messages Options
David Bremner-2 David Bremner-2
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

v3 of spaces command line argument separator

This obsoletes:

     id:[hidden email]

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'
_______________________________________________
notmuch mailing list
[hidden email]
https://notmuchmail.org/mailman/listinfo/notmuch
David Bremner-2 David Bremner-2
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[PATCH 1/3] cli/show: convert keyword options to booleans

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, &notmuch_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, &params.part, "part", 'p', 0 },
  { NOTMUCH_OPT_BOOLEAN, &params.crypto.decrypt, "decrypt", 'd', 0 },
  { NOTMUCH_OPT_BOOLEAN, &params.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

_______________________________________________
notmuch mailing list
[hidden email]
https://notmuchmail.org/mailman/listinfo/notmuch
David Bremner-2 David Bremner-2
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[PATCH 2/3] cli: change api of parse_option

In reply to this post by David Bremner-2
The idea is to allow it (in a future commit) advance to the next argv
element to get a value
---
 command-line-arguments.c       | 47 +++++++++++++++++++++++++++---------------
 command-line-arguments.h       |  4 ++--
 test/T690-command-line-args.sh | 15 ++++++++++++++
 3 files changed, 47 insertions(+), 19 deletions(-)
 create mode 100755 test/T690-command-line-args.sh

diff --git a/command-line-arguments.c b/command-line-arguments.c
index de6b4536..a489612f 100644
--- a/command-line-arguments.c
+++ b/command-line-arguments.c
@@ -121,18 +121,24 @@ parse_position_arg (const char *arg_str, int pos_arg_index,
  * parse a possible value, and assign to *output_var
  */
 
-notmuch_bool_t
-parse_option (const char *_arg, const notmuch_opt_desc_t *options)
+int
+parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_index)
 {
+    assert(argv);
+
+    const char *_arg = argv[opt_index];
+
     assert(_arg);
     assert(options);
 
     const char *arg = _arg + 2; /* _arg starts with -- */
     const notmuch_opt_desc_t *try;
     for (try = options; try->opt_type != NOTMUCH_OPT_END; try++) {
- if (try->opt_type == NOTMUCH_OPT_INHERIT &&
-    parse_option (_arg, try->output_var))
-    return TRUE;
+ if (try->opt_type == NOTMUCH_OPT_INHERIT) {
+    int new_index = parse_option (argc, argv, try->output_var, opt_index);
+    if (new_index >= 0)
+ return new_index;
+ }
 
  if (! try->name)
     continue;
@@ -155,24 +161,33 @@ parse_option (const char *_arg, const notmuch_opt_desc_t *options)
  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_status = _process_keyword_arg (try, next, value);
+    break;
  case NOTMUCH_OPT_BOOLEAN:
-    return _process_boolean_arg (try, next, value);
+    opt_status = _process_boolean_arg (try, next, value);
+    break;
  case NOTMUCH_OPT_INT:
-    return _process_int_arg (try, next, value);
+    opt_status = _process_int_arg (try, next, value);
+    break;
  case NOTMUCH_OPT_STRING:
-    return _process_string_arg (try, next, value);
+    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 FALSE;
+    return -1;
 }
 
 /* See command-line-arguments.h for description */
@@ -194,18 +209,16 @@ parse_arguments (int argc, char **argv,
     }
 
  } else {
+    int prev_opt_index = opt_index;
 
     if (strlen (argv[opt_index]) == 2)
  return opt_index+1;
 
-    more_args = parse_option (argv[opt_index], options);
-    if (more_args) {
- opt_index++;
-    } else {
- fprintf (stderr, "Unrecognized option: %s\n", argv[opt_index]);
- opt_index = -1;
+    opt_index = parse_option (argc, argv, options, opt_index);
+    if (opt_index < 0) {
+ fprintf (stderr, "Unrecognized option: %s\n", argv[prev_opt_index]);
+ more_args = FALSE;
     }
-
  }
     }
 
diff --git a/command-line-arguments.h b/command-line-arguments.h
index 309aaf2b..4c4d240e 100644
--- a/command-line-arguments.h
+++ b/command-line-arguments.h
@@ -70,8 +70,8 @@ parse_arguments (int argc, char **argv, const notmuch_opt_desc_t *options, int o
  * functions.
  */
 
-notmuch_bool_t
-parse_option (const char *arg, const notmuch_opt_desc_t* options);
+int
+parse_option (int argc, char **argv, const notmuch_opt_desc_t* options, int opt_index);
 
 notmuch_bool_t
 parse_position_arg (const char *arg,
diff --git a/test/T690-command-line-args.sh b/test/T690-command-line-args.sh
new file mode 100755
index 00000000..8711bec2
--- /dev/null
+++ b/test/T690-command-line-args.sh
@@ -0,0 +1,15 @@
+#!/usr/bin/env bash
+
+test_description="command line arguments"
+. ./test-lib.sh || exit 1
+
+NOTMUCH_NEW > /dev/null
+
+test_begin_subtest 'bad option to show'
+notmuch show --frobnicate >& OUTPUT
+cat <<EOF > EXPECTED
+Unrecognized option: --frobnicate
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+test_done
--
2.11.0

_______________________________________________
notmuch mailing list
[hidden email]
https://notmuchmail.org/mailman/listinfo/notmuch
David Bremner-2 David Bremner-2
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[PATCH 3/3] cli: add space separator for keyword, string, and int arguments

In reply to this post by David Bremner-2
Defer the complication of optional boolean arguments for later (never?).
---
 command-line-arguments.c       | 11 +++++++
 doc/man1/notmuch.rst           | 14 ++++++++
 test/T030-config.sh            |  8 +++++
 test/T690-command-line-args.sh | 72 +++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 104 insertions(+), 1 deletion(-)

diff --git a/command-line-arguments.c b/command-line-arguments.c
index a489612f..dc517b06 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/doc/man1/notmuch.rst b/doc/man1/notmuch.rst
index fbd7f381..5e238ae4 100644
--- a/doc/man1/notmuch.rst
+++ b/doc/man1/notmuch.rst
@@ -125,6 +125,20 @@ users to have their own notmuch related tools to be run via the
 notmuch command. By design, this does not allow notmuch's own commands
 to be overriden using external commands.
 
+OPTION SYNTAX
+-------------
+
+All options accepting an argument can be used with '=' or ':' as a
+separator. For the cases where it's not ambiguous (in particular
+excluding boolean options), a space can also be used. The following
+are all equivalent:
+
+::
+
+   notmuch --config=alt-config config get user.name
+   notmuch --config:alt-config config get user.name
+   notmuch --config alt-config config get user.name
+
 ENVIRONMENT
 ===========
 
diff --git a/test/T030-config.sh b/test/T030-config.sh
index 0915abdb..79a24948 100755
--- a/test/T030-config.sh
+++ b/test/T030-config.sh
@@ -69,6 +69,14 @@ notmuch --config=alt-config config set user.name "Another Name"
 test_expect_equal "$(notmuch --config=alt-config config get user.name)" \
     "Another Name"
 
+test_begin_subtest "Top level --config:FILE option"
+test_expect_equal "$(notmuch --config:alt-config config get user.name)" \
+    "Another Name"
+
+test_begin_subtest "Top level --config<space>FILE option"
+test_expect_equal "$(notmuch --config  alt-config config get user.name)" \
+    "Another Name"
+
 test_begin_subtest "Top level --config=FILE option changed the right file"
 test_expect_equal "$(notmuch config get user.name)" \
     "Notmuch Test Suite"
diff --git a/test/T690-command-line-args.sh b/test/T690-command-line-args.sh
index 8711bec2..a4f4b5f5 100755
--- a/test/T690-command-line-args.sh
+++ b/test/T690-command-line-args.sh
@@ -3,7 +3,7 @@
 test_description="command line arguments"
 . ./test-lib.sh || exit 1
 
-NOTMUCH_NEW > /dev/null
+add_message
 
 test_begin_subtest 'bad option to show'
 notmuch show --frobnicate >& OUTPUT
@@ -12,4 +12,74 @@ Unrecognized option: --frobnicate
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
+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 '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
+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"]
+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"]
+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"]
+EOF
+notmuch search --output:messages --format json '*' >& OUTPUT
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest 'show --entire-thread'
+test_expect_success 'notmuch show --entire-thread tag:test > /dev/null'
+
+test_begin_subtest 'show --exclude'
+test_expect_success 'notmuch show --exclude tag:test > /dev/null'
+
 test_done
--
2.11.0

_______________________________________________
notmuch mailing list
[hidden email]
https://notmuchmail.org/mailman/listinfo/notmuch
David Bremner-2 David Bremner-2
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: v3 of spaces command line argument separator

In reply to this post by David Bremner-2
David Bremner <[hidden email]> writes:

> This obsoletes:
>
>      id:[hidden email]

Merged this version to master.

d
_______________________________________________
notmuch mailing list
[hidden email]
https://notmuchmail.org/mailman/listinfo/notmuch
Loading...