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 3/6] cli: notmuch show: use getopt_long for parsing command line options
Date: Mon, 14 Nov 2011 00:47:23 +0200	[thread overview]
Message-ID: <a311200f277af6deecca4d10772e6cf1c6601c84.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-show.c |   61 ++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 35 insertions(+), 26 deletions(-)

diff --git a/notmuch-show.c b/notmuch-show.c
index 603992a..73b4557 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -923,12 +923,10 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     notmuch_database_t *notmuch;
     notmuch_query_t *query;
     char *query_string;
-    char *opt;
     const notmuch_show_format_t *format = &format_text;
     notmuch_show_params_t params;
     int mbox = 0;
     int format_specified = 0;
-    int i;
 
     params.entire_thread = 0;
     params.raw = 0;
@@ -936,37 +934,50 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     params.cryptoctx = NULL;
     params.decrypt = 0;
 
-    argc--; argv++; /* skip subcommand argument */
-
-    for (i = 0; i < argc && argv[i][0] == '-'; i++) {
-	if (strcmp (argv[i], "--") == 0) {
-	    i++;
+    while (1) {
+	int opt;
+	static struct option options[] = {
+	    { "format", required_argument, NULL, 0 },
+	    { "part", required_argument, NULL, 1 },
+	    { "entire-thread", no_argument, NULL, 2 },
+	    { "decrypt", no_argument, NULL, 3 },
+	    { "verify", no_argument, NULL, 4 },
+	    { NULL, 0, NULL, 0 },
+	};
+
+	opt = getopt_long (argc, argv, "", options, NULL);
+	if (opt == -1)
 	    break;
-	}
-	if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
-	    opt = argv[i] + sizeof ("--format=") - 1;
-	    if (strcmp (opt, "text") == 0) {
+
+	switch (opt) {
+	case 0:
+	    if (strcmp (optarg, "text") == 0) {
 		format = &format_text;
-	    } else if (strcmp (opt, "json") == 0) {
+	    } else if (strcmp (optarg, "json") == 0) {
 		format = &format_json;
 		params.entire_thread = 1;
-	    } else if (strcmp (opt, "mbox") == 0) {
+	    } else if (strcmp (optarg, "mbox") == 0) {
 		format = &format_mbox;
 		mbox = 1;
-	    } else if (strcmp (opt, "raw") == 0) {
+	    } else if (strcmp (optarg, "raw") == 0) {
 		format = &format_raw;
 		params.raw = 1;
 	    } else {
-		fprintf (stderr, "Invalid value for --format: %s\n", opt);
+		fprintf (stderr, "Invalid value for --format: %s\n", optarg);
 		return 1;
 	    }
 	    format_specified = 1;
-	} else if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
-	    params.part = atoi(argv[i] + sizeof ("--part=") - 1);
-	} else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
+	    break;
+	case 1:
+	    params.part = atoi(optarg);
+	    break;
+	case 2:
 	    params.entire_thread = 1;
-	} else if ((STRNCMP_LITERAL (argv[i], "--verify") == 0) ||
-		   (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) {
+	    break;
+	case 3:
+	    params.decrypt = 1;
+	    /* fallthrough */
+	case 4:
 	    if (params.cryptoctx == NULL) {
 		GMimeSession* session = g_object_new(g_mime_session_get_type(), NULL);
 		if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))
@@ -976,16 +987,14 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
 		g_object_unref (session);
 		session = NULL;
 	    }
-	    if (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)
-		params.decrypt = 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 ` [PATCH 2/6] cli: notmuch search: " Jani Nikula
2011-11-13 22:47 ` Jani Nikula [this message]
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=a311200f277af6deecca4d10772e6cf1c6601c84.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).