[PATCH 1/2] cli: Parsing. Add option NOTMUCH_OPT_INT_OR_BOOLEAN
Mark Walters
markwalters1009 at gmail.com
Thu Mar 8 14:15:43 PST 2012
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
More information about the notmuch
mailing list