* [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments
@ 2017-10-14 13:16 Jani Nikula
2017-10-14 13:16 ` [PATCH 2/3] cli: use the negating boolean support for new and insert --no-hooks Jani Nikula
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Jani Nikula @ 2017-10-14 13:16 UTC (permalink / raw)
To: notmuch
Add transparent support for negating boolean and keyword flag
arguments using --no-argument style on the command line. That is, if
the option description contains a boolean or a keyword flag argument
named "argument", --no-argument will match and negate it.
For boolean arguments this obviously means the logical NOT. For
keyword flag arguments this means bitwise AND of the bitwise NOT,
i.e. masking out the specified bits instead of OR'ing them in.
For example, you can use --no-exclude instead of --exclude=false in
notmuch show. If we had keyword flag arguments with some flags
defaulting to on, say --include=tags in notmuch dump/restore, this
would allow --no-include=tags to switch that off while not affecting
other flags.
As a curiosity, you should be able to warp your brain using
--no-exclude=true meaning false and --no-exclude=false meaning true if
you wish.
Specifying both "argument" and "no-argument" style arguments in the
same option description should be avoided. In this case, --no-argument
would match whichever is specified first, and --argument would only
match "argument".
---
command-line-arguments.c | 48 +++++++++++++++++++++++++++++++++++++-----------
1 file changed, 37 insertions(+), 11 deletions(-)
diff --git a/command-line-arguments.c b/command-line-arguments.c
index 1ff5aae578c6..69ee1cb07f47 100644
--- a/command-line-arguments.c
+++ b/command-line-arguments.c
@@ -11,8 +11,9 @@
*/
static bool
-_process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
-
+_process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next,
+ const char *arg_str, bool negate)
+{
const notmuch_keyword_t *keywords;
if (next == '\0') {
@@ -24,7 +25,9 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char
if (strcmp (arg_str, keywords->name) != 0)
continue;
- if (arg_desc->opt_flags)
+ if (arg_desc->opt_flags && negate)
+ *arg_desc->opt_flags &= ~keywords->value;
+ else if (arg_desc->opt_flags)
*arg_desc->opt_flags |= keywords->value;
else
*arg_desc->opt_keyword = keywords->value;
@@ -39,7 +42,9 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char
}
static bool
-_process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
+_process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next,
+ const char *arg_str, bool negate)
+{
bool value;
if (next == '\0' || strcmp (arg_str, "true") == 0) {
@@ -51,7 +56,7 @@ _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char
return false;
}
- *arg_desc->opt_bool = value;
+ *arg_desc->opt_bool = negate ? !value : value;
return true;
}
@@ -139,6 +144,8 @@ parse_position_arg (const char *arg_str, int pos_arg_index,
return false;
}
+#define NEGATIVE_PREFIX "no-"
+
/*
* Search for a non-positional (i.e. starting with --) argument matching arg,
* parse a possible value, and assign to *output_var
@@ -155,6 +162,14 @@ parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_
assert(options);
const char *arg = _arg + 2; /* _arg starts with -- */
+ const char *negative_arg = NULL;
+
+ /* See if this is a --no-argument */
+ if (strlen (arg) > strlen (NEGATIVE_PREFIX) &&
+ strncmp (arg, NEGATIVE_PREFIX, strlen (NEGATIVE_PREFIX)) == 0) {
+ negative_arg = arg + strlen (NEGATIVE_PREFIX);
+ }
+
const notmuch_opt_desc_t *try;
const char *next_arg = NULL;
@@ -171,11 +186,22 @@ parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_
if (! try->name)
continue;
- if (strncmp (arg, try->name, strlen (try->name)) != 0)
+ char next;
+ const char *value;
+ bool negate = false;
+
+ if (strncmp (arg, try->name, strlen (try->name)) == 0) {
+ next = arg[strlen (try->name)];
+ value = arg + strlen (try->name) + 1;
+ } else if (negative_arg && (try->opt_bool || try->opt_flags) &&
+ strncmp (negative_arg, try->name, strlen (try->name)) == 0) {
+ next = negative_arg[strlen (try->name)];
+ value = negative_arg + strlen (try->name) + 1;
+ /* The argument part of --no-argument matches, negate the result. */
+ negate = true;
+ } else {
continue;
-
- char next = arg[strlen (try->name)];
- const char *value = arg + strlen(try->name) + 1;
+ }
/*
* If we have not reached the end of the argument (i.e. the
@@ -194,9 +220,9 @@ parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_
bool opt_status = false;
if (try->opt_keyword || try->opt_flags)
- opt_status = _process_keyword_arg (try, next, value);
+ opt_status = _process_keyword_arg (try, next, value, negate);
else if (try->opt_bool)
- opt_status = _process_boolean_arg (try, next, value);
+ opt_status = _process_boolean_arg (try, next, value, negate);
else if (try->opt_int)
opt_status = _process_int_arg (try, next, value);
else if (try->opt_string)
--
2.11.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 2/3] cli: use the negating boolean support for new and insert --no-hooks
2017-10-14 13:16 [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments Jani Nikula
@ 2017-10-14 13:16 ` Jani Nikula
2017-10-14 13:16 ` [PATCH 3/3] test: expand argument parsing sanity checks Jani Nikula
` (2 subsequent siblings)
3 siblings, 0 replies; 8+ messages in thread
From: Jani Nikula @ 2017-10-14 13:16 UTC (permalink / raw)
To: notmuch
This lets us use the positive hooks variable in code, increasing
clarity.
---
notmuch-insert.c | 6 +++---
notmuch-new.c | 8 ++++----
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/notmuch-insert.c b/notmuch-insert.c
index 32be74193472..6878313e188f 100644
--- a/notmuch-insert.c
+++ b/notmuch-insert.c
@@ -455,7 +455,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
const char *folder = "";
bool create_folder = false;
bool keep = false;
- bool no_hooks = false;
+ bool hooks = true;
bool synchronize_flags;
char *maildir;
char *newpath;
@@ -466,7 +466,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
{ .opt_string = &folder, .name = "folder" },
{ .opt_bool = &create_folder, .name = "create-folder" },
{ .opt_bool = &keep, .name = "keep" },
- { .opt_bool = &no_hooks, .name = "no-hooks" },
+ { .opt_bool = &hooks, .name = "hooks" },
{ .opt_inherit = notmuch_shared_options },
{ }
};
@@ -573,7 +573,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
}
}
- if (! no_hooks && status == NOTMUCH_STATUS_SUCCESS) {
+ if (hooks && status == NOTMUCH_STATUS_SUCCESS) {
/* Ignore hook failures. */
notmuch_run_hook (db_path, "post-insert");
}
diff --git a/notmuch-new.c b/notmuch-new.c
index 0f50457eb894..a6ca484101ce 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -954,7 +954,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
int opt_index;
unsigned int i;
bool timer_is_active = false;
- bool no_hooks = false;
+ bool hooks = true;
bool quiet = false, verbose = false;
notmuch_status_t status;
@@ -962,7 +962,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
{ .opt_bool = &quiet, .name = "quiet" },
{ .opt_bool = &verbose, .name = "verbose" },
{ .opt_bool = &add_files_state.debug, .name = "debug" },
- { .opt_bool = &no_hooks, .name = "no-hooks" },
+ { .opt_bool = &hooks, .name = "hooks" },
{ .opt_inherit = notmuch_shared_options },
{ }
};
@@ -995,7 +995,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
}
}
- if (!no_hooks) {
+ if (hooks) {
ret = notmuch_run_hook (db_path, "pre-new");
if (ret)
return EXIT_FAILURE;
@@ -1160,7 +1160,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
notmuch_database_destroy (notmuch);
- if (!no_hooks && !ret && !interrupted)
+ if (hooks && !ret && !interrupted)
ret = notmuch_run_hook (db_path, "post-new");
if (ret || interrupted)
--
2.11.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 3/3] test: expand argument parsing sanity checks
2017-10-14 13:16 [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments Jani Nikula
2017-10-14 13:16 ` [PATCH 2/3] cli: use the negating boolean support for new and insert --no-hooks Jani Nikula
@ 2017-10-14 13:16 ` Jani Nikula
2017-12-13 12:34 ` David Bremner
2017-10-14 19:39 ` [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments William Casarin
2017-10-14 21:16 ` William Casarin
3 siblings, 1 reply; 8+ messages in thread
From: Jani Nikula @ 2017-10-14 13:16 UTC (permalink / raw)
To: notmuch
Test the various boolean formats and --no- prefixed boolean and
keyword flag arguments.
---
test/T410-argument-parsing.sh | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/test/T410-argument-parsing.sh b/test/T410-argument-parsing.sh
index 4a2b25c6486d..243d0241b9b6 100755
--- a/test/T410-argument-parsing.sh
+++ b/test/T410-argument-parsing.sh
@@ -37,4 +37,32 @@ positional arg 1 false
EOF
test_expect_equal_file EXPECTED OUTPUT
+test_begin_subtest "--boolean=true"
+$TEST_DIRECTORY/arg-test --boolean=true > OUTPUT
+cat <<EOF > EXPECTED
+boolean 1
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "--boolean=false"
+$TEST_DIRECTORY/arg-test --boolean=false > OUTPUT
+cat <<EOF > EXPECTED
+boolean 0
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "--no-boolean"
+$TEST_DIRECTORY/arg-test --no-boolean > OUTPUT
+cat <<EOF > EXPECTED
+boolean 0
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "--no-flag"
+$TEST_DIRECTORY/arg-test --flag=one --flag=three --no-flag=three > OUTPUT
+cat <<EOF > EXPECTED
+flags 1
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
test_done
--
2.11.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments
2017-10-14 13:16 [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments Jani Nikula
2017-10-14 13:16 ` [PATCH 2/3] cli: use the negating boolean support for new and insert --no-hooks Jani Nikula
2017-10-14 13:16 ` [PATCH 3/3] test: expand argument parsing sanity checks Jani Nikula
@ 2017-10-14 19:39 ` William Casarin
2017-10-14 20:21 ` Jani Nikula
2017-10-14 21:16 ` William Casarin
3 siblings, 1 reply; 8+ messages in thread
From: William Casarin @ 2017-10-14 19:39 UTC (permalink / raw)
To: Jani Nikula, notmuch
Hey Jani,
Patches look good so far, concept ack for sure.
Jani Nikula <jani@nikula.org> writes:
> For example, you can use --no-exclude instead of --exclude=false in
> notmuch show. If we had keyword flag arguments with some flags
> defaulting to on, say --include=tags in notmuch dump/restore, this
> would allow --no-include=tags to switch that off while not affecting
> other flags.
I've been testing it a bit, I can't seem to make this work in this example:
./notmuch count --no-exclude
After some brief investigation it might be because count is using
EXCLUDE_true(1) and EXCLUDE_false(0) which are not equal to
NOTMUCH_EXCLUDE_TRUE(1) and NOTMUCH_EXCLUDE_FALSE(2), but I'm not sure.
Cheers,
William
--
https://jb55.com
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments
2017-10-14 19:39 ` [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments William Casarin
@ 2017-10-14 20:21 ` Jani Nikula
2017-10-14 21:11 ` William Casarin
0 siblings, 1 reply; 8+ messages in thread
From: Jani Nikula @ 2017-10-14 20:21 UTC (permalink / raw)
To: William Casarin, notmuch
On Sat, 14 Oct 2017, William Casarin <jb55@jb55.com> wrote:
> Hey Jani,
>
> Patches look good so far, concept ack for sure.
>
>
> Jani Nikula <jani@nikula.org> writes:
>
>> For example, you can use --no-exclude instead of --exclude=false in
>> notmuch show. If we had keyword flag arguments with some flags
>> defaulting to on, say --include=tags in notmuch dump/restore, this
>> would allow --no-include=tags to switch that off while not affecting
>> other flags.
>
> I've been testing it a bit, I can't seem to make this work in this example:
>
> ./notmuch count --no-exclude
>
> After some brief investigation it might be because count is using
> EXCLUDE_true(1) and EXCLUDE_false(0) which are not equal to
> NOTMUCH_EXCLUDE_TRUE(1) and NOTMUCH_EXCLUDE_FALSE(2), but I'm not sure.
*blush* I screwed those enums up. Here's a patch that takes care of both
issues id:20171014201836.4486-1-jani@nikula.org. It's independent of
this series.
BR,
Jani.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments
2017-10-14 20:21 ` Jani Nikula
@ 2017-10-14 21:11 ` William Casarin
0 siblings, 0 replies; 8+ messages in thread
From: William Casarin @ 2017-10-14 21:11 UTC (permalink / raw)
To: Jani Nikula, notmuch
Jani Nikula <jani@nikula.org> writes:
> *blush* I screwed those enums up. Here's a patch that takes care of both
> issues id:20171014201836.4486-1-jani@nikula.org. It's independent of
> this series.
Works, thanks.
--
https://jb55.com
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments
2017-10-14 13:16 [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments Jani Nikula
` (2 preceding siblings ...)
2017-10-14 19:39 ` [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments William Casarin
@ 2017-10-14 21:16 ` William Casarin
3 siblings, 0 replies; 8+ messages in thread
From: William Casarin @ 2017-10-14 21:16 UTC (permalink / raw)
To: Jani Nikula, notmuch
Tested ACK 1-3 + id:20171014201836.4486-1-jani@nikula.org
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] test: expand argument parsing sanity checks
2017-10-14 13:16 ` [PATCH 3/3] test: expand argument parsing sanity checks Jani Nikula
@ 2017-12-13 12:34 ` David Bremner
0 siblings, 0 replies; 8+ messages in thread
From: David Bremner @ 2017-12-13 12:34 UTC (permalink / raw)
To: Jani Nikula, notmuch
Jani Nikula <jani@nikula.org> writes:
> Test the various boolean formats and --no- prefixed boolean and
> keyword flag arguments.
series pushed to master
d
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2017-12-13 12:34 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-14 13:16 [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments Jani Nikula
2017-10-14 13:16 ` [PATCH 2/3] cli: use the negating boolean support for new and insert --no-hooks Jani Nikula
2017-10-14 13:16 ` [PATCH 3/3] test: expand argument parsing sanity checks Jani Nikula
2017-12-13 12:34 ` David Bremner
2017-10-14 19:39 ` [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments William Casarin
2017-10-14 20:21 ` Jani Nikula
2017-10-14 21:11 ` William Casarin
2017-10-14 21:16 ` William Casarin
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).