From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 89B12431FBC for ; Sat, 10 Mar 2012 03:05:43 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 0.201 X-Spam-Level: X-Spam-Status: No, score=0.201 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id kK3QfH+kCvEL for ; Sat, 10 Mar 2012 03:05:43 -0800 (PST) Received: from mail-wi0-f179.google.com (mail-wi0-f179.google.com [209.85.212.179]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id E3AC7431FAE for ; Sat, 10 Mar 2012 03:05:42 -0800 (PST) Received: by wibhn6 with SMTP id hn6so1267357wib.2 for ; Sat, 10 Mar 2012 03:05:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=abGHG+cDQHnRjuF49w/IsNTgJrmXXA/0Vjxm5mnfdW0=; b=IIfrRK0Nq5+8MWTNiudgQcDjbSfztgj/MTRq9hy1T09AQedXEFaTugISIJmvDr4nMP HSiWpkfi+MSCkLOu9Cq+4gkBoaKagVNMhWNmONYEuwZyPSll8pLYIynSkPThoeh9HIuC itMqOBrT+XVikrBDRv2ww499MnfWXKrsdlUNk/AO7BSibZVUHE3qXx3rsC0zfe/RT2xw tUcgpB+bk0BbQRmUOjYVQ5tBaNEXI7//1wP3KQVey+J5FoMTtkL81sCY0DS2e31VNoW7 r8b4dMvjwHi+J08kLvrFn0pxzoq2RFrJdLIq0i4S8joslRSah5yVyLjeY/qTvCtWmGwK HPcQ== Received: by 10.216.139.129 with SMTP id c1mr3347361wej.48.1331377541532; Sat, 10 Mar 2012 03:05:41 -0800 (PST) Received: from localhost (94-192-233-223.zone6.bethere.co.uk. [94.192.233.223]) by mx.google.com with ESMTPS id be4sm24537464wib.8.2012.03.10.03.05.40 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 10 Mar 2012 03:05:40 -0800 (PST) From: Mark Walters To: notmuch@notmuchmail.org Subject: [PATCH 1/2] cli: Parsing. Allow true/false parameter for boolean options. Date: Sat, 10 Mar 2012 11:05:32 +0000 Message-Id: <1331377533-30262-2-git-send-email-markwalters1009@gmail.com> X-Mailer: git-send-email 1.7.9.1 In-Reply-To: <1331377533-30262-1-git-send-email-markwalters1009@gmail.com> References: <1331377533-30262-1-git-send-email-markwalters1009@gmail.com> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 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, 10 Mar 2012 11:05:43 -0000 Allow NOTMUCH_OPT_BOOLEAN to take a true or false parameter. In particular it allows the user to turn off a boolean option with --option=false. --- command-line-arguments.c | 34 ++++++++++++++++++++++++++-------- 1 files changed, 26 insertions(+), 8 deletions(-) diff --git a/command-line-arguments.c b/command-line-arguments.c index e711414..76b185f 100644 --- a/command-line-arguments.c +++ b/command-line-arguments.c @@ -28,6 +28,24 @@ _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, const char *arg_str) { return FALSE; } +static notmuch_bool_t +_process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) { + + if (next == 0) { + *((notmuch_bool_t *)arg_desc->output_var) = TRUE; + return TRUE; + } + if (strcmp (arg_str, "false") == 0) { + *((notmuch_bool_t *)arg_desc->output_var) = FALSE; + return TRUE; + } + if (strcmp (arg_str, "true") == 0) { + *((notmuch_bool_t *)arg_desc->output_var) = TRUE; + return TRUE; + } + return FALSE; +} + /* Search for the {pos_arg_index}th position argument, return FALSE if that does not exist. @@ -76,14 +94,15 @@ parse_option (const char *arg, char *endptr; /* Everything but boolean arguments (switches) needs a - * delimiter, and a non-zero length value + * delimiter, and a non-zero length value. Boolean + * arguments may take an optional =true or =false value. */ - - if (try->opt_type != NOTMUCH_OPT_BOOLEAN) { - if (next != '=' && next != ':') return FALSE; - if (value[0] == 0) return FALSE; + if (next != '=' && next != ':' && next != 0) return FALSE; + if (next == 0) { + if (try->opt_type != NOTMUCH_OPT_BOOLEAN) + return FALSE; } else { - if (next != 0) return FALSE; + if (value[0] == 0) return FALSE; } if (try->output_var == NULL) @@ -94,8 +113,7 @@ parse_option (const char *arg, return _process_keyword_arg (try, value); break; case NOTMUCH_OPT_BOOLEAN: - *((notmuch_bool_t *)try->output_var) = TRUE; - return TRUE; + return _process_boolean_arg (try, next, value); break; case NOTMUCH_OPT_INT: *((int *)try->output_var) = strtol (value, &endptr, 10); -- 1.7.9.1