Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 81B08429E25 for ; Thu, 8 Mar 2012 14:16:12 -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 Cd8ez-OCKQWr for ; Thu, 8 Mar 2012 14:16:12 -0800 (PST) Received: from mail-ww0-f45.google.com (mail-ww0-f45.google.com [74.125.82.45]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id CEEC8431FAE for ; Thu, 8 Mar 2012 14:16:11 -0800 (PST) Received: by mail-ww0-f45.google.com with SMTP id ds10so878463wgb.2 for ; Thu, 08 Mar 2012 14:16:11 -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=UvY+VtrS18qmXIzo8Z6w8CC6EqsYraJnjzcaJkKkMoI=; b=Yu/b4aTB/6TkxVXo4OTvVTZvY/tDDEhwsDQi9o9Y7sz4zeorXTSS45QVOY/Vur2wXy sKsT4hmeKLIftlvGKwARFkGDGQlfadU36rJ79owAOvA2PCF9ivO/COcV9FiseGiw7hX4 8mT20dg5S45UigL/u9njgtdiLygKabfho9/OqkUDrMr8I9pO6hmjcygcyhBTetx8z8GY p0Ue2CIRwly52jh/eGnd3mYqMacPznNI1TsPiOMGWIMATpeKvgE7J0tcNdpDn476wVRY hpND6JSSfagQskTQQfFoOExEvA4jmPImm8WoVOcghPpawEv5gk3+MQiGdB6Pgbq9xOO1 JJWQ== Received: by 10.216.132.98 with SMTP id n76mr4124246wei.101.1331244971342; Thu, 08 Mar 2012 14:16:11 -0800 (PST) Received: from localhost (94-192-233-223.zone6.bethere.co.uk. [94.192.233.223]) by mx.google.com with ESMTPS id fl2sm14898801wib.4.2012.03.08.14.16.10 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 08 Mar 2012 14:16:10 -0800 (PST) From: Mark Walters To: notmuch@notmuchmail.org Subject: [PATCH 1/2] cli: Parsing. Add option NOTMUCH_OPT_INT_OR_BOOLEAN Date: Thu, 8 Mar 2012 22:15:43 +0000 Message-Id: <1331244944-7960-2-git-send-email-markwalters1009@gmail.com> X-Mailer: git-send-email 1.7.9.1 In-Reply-To: <1331244944-7960-1-git-send-email-markwalters1009@gmail.com> References: <1331244944-7960-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: Thu, 08 Mar 2012 22:16:12 -0000 Allow the option NOTMUCH_OPT_INT_OR_BOOLEAN for command line parsing which accepts --verbose=3 and --verbose with the latter setting verbose to 1. It also allows --verbose=0 so (with a little caller support) user can turn off boolean options. This means that extra options can be added to the command line programs in a backwards compatible manner. --- command-line-arguments.c | 29 +++++++++++++++++++++++++++-- command-line-arguments.h | 3 +++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/command-line-arguments.c b/command-line-arguments.c index e711414..99e13c6 100644 --- a/command-line-arguments.c +++ b/command-line-arguments.c @@ -4,6 +4,22 @@ #include "error_util.h" #include "command-line-arguments.h" +/* A helper for parsing an int to a boolean */ +notmuch_bool_t +notmuch_int_to_boolean (int i) +{ + switch (i) { + case 1: + return TRUE; + case 0: + return FALSE; + default: + INTERNAL_ERROR ("Non-boolean value %d", i); + /* UNREACHED */ + return FALSE; + } +} + /* Search the array of keywords for a given argument, assigning the output variable to the corresponding value. Return FALSE if nothing @@ -72,6 +88,7 @@ parse_option (const char *arg, if (try->name && strncmp (arg, try->name, strlen (try->name)) == 0) { char next = arg[strlen (try->name)]; const char *value= arg+strlen(try->name)+1; + enum notmuch_opt_type opt_type = try->opt_type; char *endptr; @@ -79,7 +96,14 @@ parse_option (const char *arg, * delimiter, and a non-zero length value */ - if (try->opt_type != NOTMUCH_OPT_BOOLEAN) { + if (opt_type == NOTMUCH_OPT_INT_OR_BOOLEAN) { + if (next != 0) + opt_type = NOTMUCH_OPT_INT; + else + opt_type = NOTMUCH_OPT_BOOLEAN; + } + + if (opt_type != NOTMUCH_OPT_BOOLEAN) { if (next != '=' && next != ':') return FALSE; if (value[0] == 0) return FALSE; } else { @@ -89,7 +113,7 @@ parse_option (const char *arg, if (try->output_var == NULL) INTERNAL_ERROR ("output pointer NULL for option %s", try->name); - switch (try->opt_type) { + switch (opt_type) { case NOTMUCH_OPT_KEYWORD: return _process_keyword_arg (try, value); break; @@ -107,6 +131,7 @@ parse_option (const char *arg, break; case NOTMUCH_OPT_POSITION: case NOTMUCH_OPT_END: + case NOTMUCH_OPT_INT_OR_BOOLEAN: /* should be dealt with above */ default: INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type); /*UNREACHED*/ diff --git a/command-line-arguments.h b/command-line-arguments.h index de1734a..a2fc545 100644 --- a/command-line-arguments.h +++ b/command-line-arguments.h @@ -6,6 +6,7 @@ enum notmuch_opt_type { NOTMUCH_OPT_END = 0, NOTMUCH_OPT_BOOLEAN, /* --verbose */ + NOTMUCH_OPT_INT_OR_BOOLEAN, /* --verbose or --verbose=1 */ NOTMUCH_OPT_INT, /* --frob=8 */ NOTMUCH_OPT_KEYWORD, /* --format=raw|json|text */ NOTMUCH_OPT_STRING, /* --file=/tmp/gnarf.txt */ @@ -76,5 +77,7 @@ parse_position_arg (const char *arg, int position_arg_index, const notmuch_opt_desc_t* options); +notmuch_bool_t +notmuch_int_to_boolean (int i); #endif -- 1.7.9.1