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 32DC36DE0FBD for ; Sat, 14 Oct 2017 06:16:13 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: 0.018 X-Spam-Level: X-Spam-Status: No, score=0.018 tagged_above=-999 required=5 tests=[AWL=0.038, 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 vx2uOo11auZU for ; Sat, 14 Oct 2017 06:16:12 -0700 (PDT) Received: from mail-lf0-f66.google.com (mail-lf0-f66.google.com [209.85.215.66]) by arlo.cworth.org (Postfix) with ESMTPS id 325556DE0FB6 for ; Sat, 14 Oct 2017 06:16:12 -0700 (PDT) Received: by mail-lf0-f66.google.com with SMTP id b190so12371151lfg.9 for ; Sat, 14 Oct 2017 06:16:12 -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; bh=01MUJqHDnYGzHuU8sp2z+VqAEwmkgRA/GDdWaGdI8fM=; b=iGlse2e2THykK6P0HhKQ9I682ph3/vQrbey1uVeitUxzkNstM16IrZ7r+ntZ4Ax8Y/ eQtVZJ9684AkiVu84iVMgnJkiDFfG5J0ee1R0hyYBbNnNtqJNu2v5H+SVgz6ysVKROKK ZJqCqrcvH6Nml3IKPWjTmEMQeAivy1RWQT4GDduGxFMmd0kWz7eEiCQ3y2eiUC8U0ysa kjaDarr3yo7yztrR6l4HYebdoTRa/8N3aCDLERu+dEaaYch5iAcMAukPOnGrm5tTjNSa lW/92/+ueQQ3tRozkvl7klq3ZZnlavcbZ+iI0vNOPLhcg5iNh5kXqDD0FUBXOZKqX7F3 Uc9Q== 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; bh=01MUJqHDnYGzHuU8sp2z+VqAEwmkgRA/GDdWaGdI8fM=; b=H+k+5uhN/ofgFbBXuWYefNCdR/lJnGsdG+nFVgqO3AgtpTF5j1R0dmuoJw0fY+WEXL DuLhRMEcuSG7H5mtCcNXJSucSxDLIbXaT1Vl+0SXKrG05wtU/DYlCK1PzuGTCf2doQd9 WJReJpybumsmRfu2AN1G1odmCiWKZ1YuM10/lrNish7uTs4oWayrTY5PMUlL5kvQofAJ hy1W1ijsgbytM/ISsC8VarXyhU0XL/gwdvOWrp3RuuXNc7wU8KVimlus1q2c5zftT6NX CrOys9dCs8uhCvyaY1h7hckco5B7mHn/OdzILCXWk1tq0G5xNrjQa8Kd4+cbgz0rAvnC KGPQ== X-Gm-Message-State: AMCzsaUdz9RD8y9oYVcnIo58rReGRD1fxJkChcBN0FvVhGr8R7EwKdg/ AK7hw3yvpUA8VtD2d67nxrr4fTloQyg= X-Google-Smtp-Source: AOwi7QDGrycC0NsW2ygI9blH99hjfQ6zgaggqOAz+/jc5gix78RWDkvsgL2waImvOregRjoB0H6ILw== X-Received: by 10.46.32.150 with SMTP id g22mr1885416lji.62.1507986970262; Sat, 14 Oct 2017 06:16:10 -0700 (PDT) Received: from localhost (mobile-access-5d6a0c-19.dhcp.inet.fi. [93.106.12.19]) by smtp.gmail.com with ESMTPSA id e35sm783806lji.83.2017.10.14.06.16.09 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 14 Oct 2017 06:16:09 -0700 (PDT) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [PATCH 1/3] cli: add support for --no- prefixed boolean and keyword flag arguments Date: Sat, 14 Oct 2017 16:16:06 +0300 Message-Id: <20171014131608.17587-1-jani@nikula.org> X-Mailer: git-send-email 2.11.0 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: Sat, 14 Oct 2017 13:16:13 -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 | 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