From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id 859846DE10F9 for ; Tue, 19 Sep 2017 13:39:53 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" X-Spam-Flag: NO X-Spam-Score: 0.037 X-Spam-Level: X-Spam-Status: No, score=0.037 tagged_above=-999 required=5 tests=[AWL=0.057, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id u4FCdJlY8Gi7 for ; Tue, 19 Sep 2017 13:39:52 -0700 (PDT) Received: from mail-lf0-f68.google.com (mail-lf0-f68.google.com [209.85.215.68]) by arlo.cworth.org (Postfix) with ESMTPS id 0A45C6DE0C51 for ; Tue, 19 Sep 2017 13:39:48 -0700 (PDT) Received: by mail-lf0-f68.google.com with SMTP id y15so388322lfd.0 for ; Tue, 19 Sep 2017 13:39:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nikula-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=eKrFQSw60lqbmXb0tM0+qzZv/FS0i/U/1irpJXRVDIQ=; b=l7c9rnJPd9jsQtjuas2JTzi6TXsMypJm55WRWrrertiJWt/mce8/TTGuBL+cReWSwo s1X3PnS2RPCtk5IGumcmO0ufhrLqAECwvERuvhwOy4djG/LWq86cTdryI6R0umpUwVdB g14BUCfAfZsmU137JVW+wJOCCdN3HL6kRotAk6dg05JHHlpNEG5rc8UC9KEqDC7cSQrB vbp+v9FgO54vVRi+4ZkwuxqopRhm1cq6I1r4A/TDQ36aHSg3H15+HEUJGumNkeq2+j7h kG2uJmKngV067wwsXmtoONBiVrA3MuFWP7RAsb20xDJNTvzlvC3wkrqKq9rGespq1FqY TPCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=eKrFQSw60lqbmXb0tM0+qzZv/FS0i/U/1irpJXRVDIQ=; b=c2owl9IvbjKAW4KLUnvdTeLbJK8gibVXDJgLxfHS79pFlhLDrA/MAS/bEtVpX75l8j ixawGlRmpxSmvbzi6X+1DwDfugQXSxcBfpzeysexyqSiL8VH4Qw/rtkzsiWfjjYsxNQD 0CM3r3UKSexyFag+JMs5/+jORnryS5RP9ar2CxjOo5WBVQXMa7qNkiZ3bMp7+bH9ZHxe Edh1w1Qs1RYXZsI9BFXEjiFQa9QlzTM/R9sggPSICLn6X2ydXmhAN+yeVoQKOj+zS+Ma MK6+a1ziRwvkhi+esG7gLYVECwKA3XLlEO7NCVdrhptyQSB9T4BguyZ/fd0EVWGpGtSN QHWA== X-Gm-Message-State: AHPjjUgHjB8A8m9R6nSzgwdI6oyI91S19x7lhToF61BoIXTu/kU+v9bA OINfws+G6/ryFZp2ClC03iVbsEeJgDM= X-Google-Smtp-Source: AOwi7QBjBJVqiDDJN/kEIOOGKqZtGxCIaEDptvql8UwYfeVcYXBqxVGLtjN70tjw0DKjSc0suOSRUg== X-Received: by 10.46.95.93 with SMTP id t90mr1405297ljb.2.1505853585682; Tue, 19 Sep 2017 13:39:45 -0700 (PDT) Received: from localhost (mobile-access-5d6a60-234.dhcp.inet.fi. [93.106.96.234]) by smtp.gmail.com with ESMTPSA id v25sm50926ljb.3.2017.09.19.13.39.44 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 19 Sep 2017 13:39:45 -0700 (PDT) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [PATCH 6/9] cli: add support for --no- prefixed boolean and keyword flag arguments Date: Tue, 19 Sep 2017 23:39:26 +0300 Message-Id: X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 19 Sep 2017 20:39:53 -0000 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 | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/command-line-arguments.c b/command-line-arguments.c index a79afcaf8a15..d61d345285b9 100644 --- a/command-line-arguments.c +++ b/command-line-arguments.c @@ -11,7 +11,8 @@ */ static notmuch_bool_t -_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, notmuch_bool_t negate) { const notmuch_keyword_t *keywords; @@ -25,7 +26,9 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char continue; if (arg_desc->output_var) { - if (arg_desc->opt_type == NOTMUCH_OPT_KEYWORD_FLAGS) + if (arg_desc->opt_type == NOTMUCH_OPT_KEYWORD_FLAGS && negate) + *((int *)arg_desc->output_var) &= ~keywords->value; + else if (arg_desc->opt_type == NOTMUCH_OPT_KEYWORD_FLAGS) *((int *)arg_desc->output_var) |= keywords->value; else *((int *)arg_desc->output_var) = keywords->value; @@ -41,7 +44,8 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char } static notmuch_bool_t -_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, notmuch_bool_t negate) { notmuch_bool_t value; if (next == '\0' || strcmp (arg_str, "true") == 0) { @@ -53,7 +57,7 @@ _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char return FALSE; } - *((notmuch_bool_t *)arg_desc->output_var) = value; + *((notmuch_bool_t *)arg_desc->output_var) = negate ? !value : value; return TRUE; } @@ -116,6 +120,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 @@ -132,6 +138,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; @@ -148,11 +162,24 @@ 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; + notmuch_bool_t 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 && + strncmp (negative_arg, try->name, strlen (try->name)) == 0 && + (try->opt_type == NOTMUCH_OPT_BOOLEAN || + try->opt_type == NOTMUCH_OPT_KEYWORD_FLAGS)) { + 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 @@ -176,10 +203,10 @@ parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_ switch (try->opt_type) { case NOTMUCH_OPT_KEYWORD: case NOTMUCH_OPT_KEYWORD_FLAGS: - opt_status = _process_keyword_arg (try, next, value); + opt_status = _process_keyword_arg (try, next, value, negate); break; case NOTMUCH_OPT_BOOLEAN: - opt_status = _process_boolean_arg (try, next, value); + opt_status = _process_boolean_arg (try, next, value, negate); break; case NOTMUCH_OPT_INT: opt_status = _process_int_arg (try, next, value); -- 2.11.0