[PATCH 1/6] cli: notmuch new: use getopt_long for parsing command line options

Jani Nikula jani at nikula.org
Sun Nov 13 14:47:21 PST 2011


Signed-off-by: Jani Nikula <jani at nikula.org>
---
 notmuch-client.h |    1 +
 notmuch-new.c    |   20 +++++++++++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index b50cb38..eb16f0d 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -43,6 +43,7 @@
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <unistd.h>
+#include <getopt.h>
 #include <dirent.h>
 #include <errno.h>
 #include <signal.h>
diff --git a/notmuch-new.c b/notmuch-new.c
index 81a9350..145aa64 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -815,16 +815,26 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
     add_files_state.verbose = 0;
     add_files_state.output_is_a_tty = isatty (fileno (stdout));
 
-    argc--; argv++; /* skip subcommand argument */
+    while (1) {
+	int opt;
+	static struct option options[] = {
+	    { "verbose", no_argument, NULL, 0 },
+	    { NULL, 0, NULL, 0 },
+	};
+
+	opt = getopt_long (argc, argv, "", options, NULL);
+	if (opt == -1)
+	    break;
 
-    for (i = 0; i < argc && argv[i][0] == '-'; i++) {
-	if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
+	switch (opt) {
+	case 0:
 	    add_files_state.verbose = 1;
-	} else {
-	    fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
+	    break;
+	case '?':
 	    return 1;
 	}
     }
+
     config = notmuch_config_open (ctx, NULL, NULL);
     if (config == NULL)
 	return 1;
-- 
1.7.5.4



More information about the notmuch mailing list