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 BFDD14150ED for ; Sun, 8 Jan 2012 13:48:55 -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 MpxXzVGxw3yp for ; Sun, 8 Jan 2012 13:48:54 -0800 (PST) Received: from mail-ee0-f53.google.com (mail-ee0-f53.google.com [74.125.83.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 1E1D14150EF for ; Sun, 8 Jan 2012 13:48:51 -0800 (PST) Received: by mail-ee0-f53.google.com with SMTP id d41so2496075eek.26 for ; Sun, 08 Jan 2012 13:48:51 -0800 (PST) Received: by 10.14.32.211 with SMTP id o59mr5166974eea.118.1326059331691; Sun, 08 Jan 2012 13:48:51 -0800 (PST) Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi. [80.220.92.23]) by mx.google.com with ESMTPS id t1sm282282935eeb.3.2012.01.08.13.48.49 (version=SSLv3 cipher=OTHER); Sun, 08 Jan 2012 13:48:50 -0800 (PST) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [PATCH v2 2/6] cli: convert "notmuch reply" to use the new argument parser Date: Sun, 8 Jan 2012 23:48:31 +0200 Message-Id: <74f31cf0b3dc11b365a3838a44f095326d6c9f99.1326058946.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, 08 Jan 2012 21:48:56 -0000 Use the new notmuch argument parser to handle arguments in "notmuch reply". There should be no functional changes. Signed-off-by: Jani Nikula --- notmuch-reply.c | 75 +++++++++++++++++++++++++++---------------------------- 1 files changed, 37 insertions(+), 38 deletions(-) diff --git a/notmuch-reply.c b/notmuch-reply.c index 1f33a86..000f6da 100644 --- a/notmuch-reply.c +++ b/notmuch-reply.c @@ -612,62 +612,61 @@ notmuch_reply_format_headers_only(void *ctx, return 0; } +enum { + FORMAT_DEFAULT, + FORMAT_HEADERS_ONLY, +}; + int 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 opt_index, 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 = { .part = -1 }; + int format = FORMAT_DEFAULT; + notmuch_bool_t decrypt = FALSE; + + notmuch_opt_desc_t options[] = { + { NOTMUCH_OPT_KEYWORD, &format, "format", 'f', + (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT }, + { "headers-only", FORMAT_HEADERS_ONLY }, + { 0, 0 } } }, + { NOTMUCH_OPT_BOOLEAN, &decrypt, "decrypt", 'd', 0 }, + { 0, 0, 0, 0, 0 } + }; - reply_format_func = notmuch_reply_format_default; - - argc--; argv++; /* skip subcommand argument */ + opt_index = parse_arguments (argc, argv, options, 1); + if (opt_index < 0) { + /* diagnostics already printed */ + return 1; + } - for (i = 0; i < argc && argv[i][0] == '-'; i++) { - if (strcmp (argv[i], "--") == 0) { - i++; - break; - } - if (STRNCMP_LITERAL (argv[i], "--format=") == 0) { - opt = argv[i] + sizeof ("--format=") - 1; - if (strcmp (opt, "default") == 0) { - reply_format_func = notmuch_reply_format_default; - } else if (strcmp (opt, "headers-only") == 0) { - reply_format_func = notmuch_reply_format_headers_only; - } else { - fprintf (stderr, "Invalid value for --format: %s\n", opt); - return 1; - } - } else if ((STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) { - 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"))) { - fprintf (stderr, "Failed to construct gpg context.\n"); - } else { - params.decrypt = TRUE; - g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE); - } - g_object_unref (session); - session = NULL; - } + if (format == FORMAT_HEADERS_ONLY) + reply_format_func = notmuch_reply_format_headers_only; + else + reply_format_func = notmuch_reply_format_default; + + if (decrypt) { + GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL); + params.cryptoctx = g_mime_gpg_context_new (session, "gpg"); + if (params.cryptoctx) { + g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE); + params.decrypt = TRUE; } else { - fprintf (stderr, "Unrecognized option: %s\n", argv[i]); - return 1; + fprintf (stderr, "Failed to construct gpg context.\n"); } + g_object_unref (session); } - argc -= i; - argv += i; - config = notmuch_config_open (ctx, NULL, NULL); if (config == NULL) return 1; - query_string = query_string_from_args (ctx, argc, argv); + query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index); if (query_string == NULL) { fprintf (stderr, "Out of memory\n"); return 1; -- 1.7.5.4