unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Jani Nikula <jani@nikula.org>
To: notmuch@notmuchmail.org, markwalters1009@gmail.com
Subject: [PATCH 1/3] command-line-arguments: allow true and false keywords for booleans
Date: Sat, 10 Mar 2012 00:33:28 +0200	[thread overview]
Message-ID: <461127379f76af5ec6f53ede0ac173e37aae392d.1331329406.git.jani@nikula.org> (raw)
In-Reply-To: <cover.1331329406.git.jani@nikula.org>
In-Reply-To: <cover.1331329406.git.jani@nikula.org>

Add support for --arg=true and --arg=false for NOTMUCH_OPT_BOOLEAN
arguments to be able to disable a boolean argument. Plain --arg
remains unchanged, meaning true.

Signed-off-by: Jani Nikula <jani@nikula.org>
---
 command-line-arguments.c |   36 ++++++++++++++++++++++++++++++------
 1 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/command-line-arguments.c b/command-line-arguments.c
index e711414..1bdb881 100644
--- a/command-line-arguments.c
+++ b/command-line-arguments.c
@@ -28,6 +28,27 @@ _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, const char *arg_str)
+{
+    notmuch_bool_t value = TRUE;
+
+    if (arg_str) {
+	if (strcmp (arg_str, "true") == 0) {
+	    value = TRUE;
+	} else if (strcmp (arg_str, "false") == 0) {
+	    value = FALSE;
+	} else {
+	    fprintf (stderr, "unknown boolean: %s\n", arg_str);
+	    return FALSE;
+	}
+    }
+
+    *((notmuch_bool_t *)arg_desc->output_var) = value;
+
+    return TRUE;
+}
+
 /*
    Search for the {pos_arg_index}th position argument, return FALSE if
    that does not exist.
@@ -79,11 +100,15 @@ parse_option (const char *arg,
 	     * delimiter, and a non-zero length value
 	     */
 
-	    if (try->opt_type != NOTMUCH_OPT_BOOLEAN) {
-		if (next != '=' && next != ':') return FALSE;
-		if (value[0] == 0) return FALSE;
+	    if (next == '=' || next == ':') {
+		if (value[0] == '\0')
+		    return FALSE;
+	    } else if (next == '\0') {
+		value = NULL;
+		if (try->opt_type != NOTMUCH_OPT_BOOLEAN)
+		    return FALSE;
 	    } else {
-		if (next != 0) return FALSE;
+		return FALSE;
 	    }
 
 	    if (try->output_var == NULL)
@@ -94,8 +119,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, value);
 		break;
 	    case NOTMUCH_OPT_INT:
 		*((int *)try->output_var) = strtol (value, &endptr, 10);
-- 
1.7.5.4

  reply	other threads:[~2012-03-09 22:33 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-08 22:15 [PATCH 0/2] cli: Parsing. Add option NOTMUCH_OPT_INT_OR_BOOLEAN Mark Walters
2012-03-08 22:15 ` [PATCH 1/2] " Mark Walters
2012-03-08 22:15 ` [PATCH 2/2] cli: make --entire-thread option notmuch-show.c INT_OR_BOOLEAN Mark Walters
2012-03-09  0:48 ` [PATCH 0/2] cli: Parsing. Add option NOTMUCH_OPT_INT_OR_BOOLEAN Jani Nikula
2012-03-09  5:11   ` Mark Walters
2012-03-09 22:33     ` [PATCH 0/3] argument parsing additions Jani Nikula
2012-03-09 22:33       ` Jani Nikula [this message]
2012-03-09 22:33       ` [PATCH 2/3] command-line-arguments: support keyword arguments with default value Jani Nikula
2012-03-09 22:33       ` [PATCH 3/3] cli: allow switching off entire thread mode in notmuch show json format Jani Nikula
2012-03-10 11:23       ` [PATCH 0/3] argument parsing additions Mark Walters
2012-03-11 19:26         ` Jani Nikula

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=461127379f76af5ec6f53ede0ac173e37aae392d.1331329406.git.jani@nikula.org \
    --to=jani@nikula.org \
    --cc=markwalters1009@gmail.com \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).