[PATCH 2/6] cli: notmuch search: use getopt_long for parsing command line options

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


Signed-off-by: Jani Nikula <jani at nikula.org>
---
 notmuch-search.c |   90 ++++++++++++++++++++++++++++-------------------------
 1 files changed, 48 insertions(+), 42 deletions(-)

diff --git a/notmuch-search.c b/notmuch-search.c
index c62a594..4f7384a 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -417,81 +417,87 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
     notmuch_database_t *notmuch;
     notmuch_query_t *query;
     char *query_str;
-    char *opt;
     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
     const search_format_t *format = &format_text;
-    int i, ret;
+    int ret;
     output_t output = OUTPUT_SUMMARY;
     int maxitems = -1; /* unlimited */
     int first = 0;
 
-    argc--; argv++; /* skip subcommand argument */
-
-    for (i = 0; i < argc && argv[i][0] == '-'; i++) {
-	if (strcmp (argv[i], "--") == 0) {
-	    i++;
+    while (1) {
+	char *p;
+	int opt;
+	static struct option options[] = {
+	    { "sort", required_argument, NULL, 0 },
+	    { "first", required_argument, NULL, 1 },
+	    { "maxitems", required_argument, NULL, 2 },
+	    { "format", required_argument, NULL, 3 },
+	    { "output", required_argument, NULL, 4 },
+	    { NULL, 0, NULL, 0 },
+	};
+
+	opt = getopt_long (argc, argv, "", options, NULL);
+	if (opt == -1)
 	    break;
-	}
-        if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
-	    opt = argv[i] + sizeof ("--sort=") - 1;
-	    if (strcmp (opt, "oldest-first") == 0) {
+
+	switch (opt) {
+	case 0:
+	    if (strcmp (optarg, "oldest-first") == 0) {
 		sort = NOTMUCH_SORT_OLDEST_FIRST;
-	    } else if (strcmp (opt, "newest-first") == 0) {
+	    } else if (strcmp (optarg, "newest-first") == 0) {
 		sort = NOTMUCH_SORT_NEWEST_FIRST;
 	    } else {
-		fprintf (stderr, "Invalid value for --sort: %s\n", opt);
+		fprintf (stderr, "Invalid value for --sort: %s\n", optarg);
 		return 1;
 	    }
-	} else if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {
-	    char *p;
-	    opt = argv[i] + sizeof ("--first=") - 1;
-	    first = strtol (opt, &p, 10);
-	    if (*opt == '\0' || p == opt || *p != '\0') {
-		fprintf (stderr, "Invalid value for --first: %s\n", opt);
+	    break;
+	case 1:
+	    first = strtol (optarg, &p, 10);
+	    if (*optarg == '\0' || p == optarg || *p != '\0') {
+		fprintf (stderr, "Invalid value for --first: %s\n", optarg);
 		return 1;
 	    }
-	} else if (STRNCMP_LITERAL (argv[i], "--maxitems=") == 0) {
-	    char *p;
-	    opt = argv[i] + sizeof ("--maxitems=") - 1;
-	    maxitems = strtoul (opt, &p, 10);
-	    if (*opt == '\0' || p == opt || *p != '\0') {
-		fprintf (stderr, "Invalid value for --maxitems: %s\n", opt);
+	    break;
+	case 2:
+	    maxitems = strtoul (optarg, &p, 10);
+	    if (*optarg == '\0' || p == optarg || *p != '\0') {
+		fprintf (stderr, "Invalid value for --maxitems: %s\n", optarg);
 		return 1;
 	    }
-	} else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
-	    opt = argv[i] + sizeof ("--format=") - 1;
-	    if (strcmp (opt, "text") == 0) {
+	    break;
+	case 3:
+	    if (strcmp (optarg, "text") == 0) {
 		format = &format_text;
-	    } else if (strcmp (opt, "json") == 0) {
+	    } else if (strcmp (optarg, "json") == 0) {
 		format = &format_json;
 	    } else {
-		fprintf (stderr, "Invalid value for --format: %s\n", opt);
+		fprintf (stderr, "Invalid value for --format: %s\n", optarg);
 		return 1;
 	    }
-	} else if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {
-	    opt = argv[i] + sizeof ("--output=") - 1;
-	    if (strcmp (opt, "summary") == 0) {
+	    break;
+	case 4:
+	    if (strcmp (optarg, "summary") == 0) {
 		output = OUTPUT_SUMMARY;
-	    } else if (strcmp (opt, "threads") == 0) {
+	    } else if (strcmp (optarg, "threads") == 0) {
 		output = OUTPUT_THREADS;
-	    } else if (strcmp (opt, "messages") == 0) {
+	    } else if (strcmp (optarg, "messages") == 0) {
 		output = OUTPUT_MESSAGES;
-	    } else if (strcmp (opt, "files") == 0) {
+	    } else if (strcmp (optarg, "files") == 0) {
 		output = OUTPUT_FILES;
-	    } else if (strcmp (opt, "tags") == 0) {
+	    } else if (strcmp (optarg, "tags") == 0) {
 		output = OUTPUT_TAGS;
 	    } else {
-		fprintf (stderr, "Invalid value for --output: %s\n", opt);
+		fprintf (stderr, "Invalid value for --output: %s\n", optarg);
 		return 1;
 	    }
-	} else {
-	    fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
+	    break;
+	case '?':
 	    return 1;
 	}
     }
 
-    argc -= i;
-    argv += i;
+    argc -= optind;
+    argv += optind;
 
     config = notmuch_config_open (ctx, NULL, NULL);
     if (config == NULL)
-- 
1.7.5.4



More information about the notmuch mailing list