Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id F2618431FB6 for ; Sun, 13 Nov 2011 14:48:05 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id twPdfRYHl7Km for ; Sun, 13 Nov 2011 14:48:03 -0800 (PST) Received: from mail-bw0-f53.google.com (mail-bw0-f53.google.com [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 9DA5B429E44 for ; Sun, 13 Nov 2011 14:47:49 -0800 (PST) Received: by mail-bw0-f53.google.com with SMTP id q10so6354254bka.26 for ; Sun, 13 Nov 2011 14:47:49 -0800 (PST) Received: by 10.204.41.66 with SMTP id n2mr16693030bke.77.1321224469198; Sun, 13 Nov 2011 14:47:49 -0800 (PST) Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi. [80.220.92.23]) by mx.google.com with ESMTPS id z7sm28089278bka.1.2011.11.13.14.47.47 (version=SSLv3 cipher=OTHER); Sun, 13 Nov 2011 14:47:48 -0800 (PST) From: Jani Nikula To: notmuch@notmuchmail.org, david@tethera.net Subject: [PATCH 5/6] cli: notmuch reply: use getopt_long for parsing command line options Date: Mon, 14 Nov 2011 00:47:25 +0200 Message-Id: <77019f4b2cd807d61f92360ff9e6f106ecd6ce35.1321223343.git.jani@nikula.org> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: References: In-Reply-To: References: X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 13 Nov 2011 22:48:06 -0000 Signed-off-by: Jani Nikula --- notmuch-reply.c | 40 +++++++++++++++++++++++----------------- 1 files changed, 23 insertions(+), 17 deletions(-) diff --git a/notmuch-reply.c b/notmuch-reply.c index 7ac879f..45b9fe9 100644 --- a/notmuch-reply.c +++ b/notmuch-reply.c @@ -619,8 +619,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[]) notmuch_config_t *config; notmuch_database_t *notmuch; notmuch_query_t *query; - char *opt, *query_string; - int i, ret = 0; + char *query_string; + int ret = 0; int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query, notmuch_show_params_t *params); notmuch_show_params_t params; @@ -628,24 +628,30 @@ notmuch_reply_command (void *ctx, int argc, char *argv[]) params.part = -1; params.cryptoctx = NULL; - argc--; argv++; /* skip subcommand argument */ + while (1) { + int opt; + static struct option options[] = { + { "format", required_argument, NULL, 0 }, + { "decrypt", no_argument, NULL, 1 }, + { NULL, 0, NULL, 0 }, + }; - for (i = 0; i < argc && argv[i][0] == '-'; i++) { - if (strcmp (argv[i], "--") == 0) { - i++; + 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, "default") == 0) { + + switch (opt) { + case 0: + if (strcmp (optarg, "default") == 0) { reply_format_func = notmuch_reply_format_default; - } else if (strcmp (opt, "headers-only") == 0) { + } else if (strcmp (optarg, "headers-only") == 0) { reply_format_func = notmuch_reply_format_headers_only; } 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], "--decrypt") == 0)) { + break; + case 1: 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"))) @@ -655,14 +661,14 @@ notmuch_reply_command (void *ctx, int argc, char *argv[]) g_object_unref (session); session = NULL; } - } 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