unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Jani Nikula <jani@nikula.org>
To: notmuch@notmuchmail.org, david@tethera.net
Subject: [PATCH 2/6] cli: notmuch search: use getopt_long for parsing command line options
Date: Mon, 14 Nov 2011 00:47:22 +0200	[thread overview]
Message-ID: <aa33f9cf06c1909012946c5a9a7c0ff501a394c1.1321223343.git.jani@nikula.org> (raw)
In-Reply-To: <cover.1321223343.git.jani@nikula.org>
In-Reply-To: <cover.1321223343.git.jani@nikula.org>

Signed-off-by: Jani Nikula <jani@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

  parent reply	other threads:[~2011-11-13 22:47 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-13 22:47 [PATCH 0/6] cli getoptification Jani Nikula
2011-11-13 22:47 ` [PATCH 1/6] cli: notmuch new: use getopt_long for parsing command line options Jani Nikula
2011-11-13 22:47 ` Jani Nikula [this message]
2011-11-13 22:47 ` [PATCH 3/6] cli: notmuch show: " Jani Nikula
2011-11-13 22:47 ` [PATCH 4/6] cli: notmuch count: " Jani Nikula
2011-11-13 22:47 ` [PATCH 5/6] cli: notmuch reply: " Jani Nikula
2011-11-13 22:47 ` [PATCH 6/6] test: emacs-large-search-buffer: expected results according to getopt_long Jani Nikula
2011-12-01  1:15   ` [RFC PATCH 1/3] notmuch-opts.[ch]: new argument parsing framework for notmuch David Bremner
2011-12-01  1:15     ` [PATCH 2/3] notmuch-dump: convert to notmuch-opts option-parsing David Bremner
2011-12-01  1:15     ` [PATCH 3/3] notmuch-dump.c: add a format option David Bremner
2011-12-02  6:41       ` [RFC Patch v2 1/3] notmuch-opts.[ch]: new argument parsing framework for notmuch David Bremner
2011-12-02  6:41         ` [RFC Patch v2 2/3] notmuch-dump: convert to notmuch-opts argument handling David Bremner
2011-12-02  6:41         ` [RFC Patch v2 3/3] notmuch-dump: add --format option David Bremner

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=aa33f9cf06c1909012946c5a9a7c0ff501a394c1.1321223343.git.jani@nikula.org \
    --to=jani@nikula.org \
    --cc=david@tethera.net \
    --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).