unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Mark Walters <markwalters1009@gmail.com>
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	[thread overview]
Message-ID: <1331244944-7960-2-git-send-email-markwalters1009@gmail.com> (raw)
In-Reply-To: <1331244944-7960-1-git-send-email-markwalters1009@gmail.com>

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

  reply	other threads:[~2012-03-08 22:16 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 ` Mark Walters [this message]
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       ` [PATCH 1/3] command-line-arguments: allow true and false keywords for booleans Jani Nikula
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=1331244944-7960-2-git-send-email-markwalters1009@gmail.com \
    --to=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).