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 AA47E429E48 for ; Fri, 13 Jan 2012 15:07:12 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org 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 wZBEyzfzjRMx for ; Fri, 13 Jan 2012 15:07:12 -0800 (PST) Received: from dmz-mailsec-scanner-6.mit.edu (DMZ-MAILSEC-SCANNER-6.MIT.EDU [18.7.68.35]) by olra.theworths.org (Postfix) with ESMTP id CE0D4429E25 for ; Fri, 13 Jan 2012 15:07:11 -0800 (PST) X-AuditID: 12074423-b7f9c6d0000008c3-4f-4f10b91fa8a8 Received: from mailhub-auth-4.mit.edu ( [18.7.62.39]) by dmz-mailsec-scanner-6.mit.edu (Symantec Messaging Gateway) with SMTP id 87.0A.02243.F19B01F4; Fri, 13 Jan 2012 18:07:11 -0500 (EST) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id q0DN7ADO030620; Fri, 13 Jan 2012 18:07:10 -0500 Received: from drake.mit.edu (26-4-166.dynamic.csail.mit.edu [18.26.4.166]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q0DN79cl028769 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Fri, 13 Jan 2012 18:07:09 -0500 (EST) Received: from amthrax by drake.mit.edu with local (Exim 4.77) (envelope-from ) id 1RlqDF-0003lq-0g; Fri, 13 Jan 2012 18:07:09 -0500 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH v2 1/3] count: Convert to new-style argument parsing Date: Fri, 13 Jan 2012 18:07:02 -0500 Message-Id: <1326496024-14403-2-git-send-email-amdragon@mit.edu> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1326496024-14403-1-git-send-email-amdragon@mit.edu> References: <1326258173-21163-1-git-send-email-amdragon@mit.edu> <1326496024-14403-1-git-send-email-amdragon@mit.edu> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrEIsWRmVeSWpSXmKPExsUixG6nriu/U8Df4NcPAYum6c4We/Z5WVy/ OZPZgdnj7mkuj1v3X7N7PFt1izmAOYrLJiU1J7MstUjfLoErY/d9wYKPIhWPjv9ha2A8zd/F yMkhIWAisfrHRnYIW0ziwr31bF2MXBxCAvsYJS4fPwzlbGCUeLb/ByOEc5xJYtm6aVDOfEaJ 5ys+soH0swloSGzbv5wRxBYRkJbYeXc2K4jNLOAo8fPeQyCbg0NYwEXi2v4CEJNFQFXi2dpo kApeAQeJrs/n2SCuUJA4t/oc2EWcQJ1/X3WCTRQSKJdYfGQN4wRG/gWMDKsYZVNyq3RzEzNz ilOTdYuTE/PyUot0zfRyM0v0UlNKNzGCg8pFeQfjn4NKhxgFOBiVeHglswT8hVgTy4orcw8x SnIwKYnyPtgGFOJLyk+pzEgszogvKs1JLT7EKMHBrCTCK2UKlONNSaysSi3Kh0lJc7AoifNq aL3zExJITyxJzU5NLUgtgsnKcHAoSfDy7wBqFCxKTU+tSMvMKUFIM3FwggznARrOBVLDW1yQ mFucmQ6RP8WoKCXOqwiSEABJZJTmwfXCov4VozjQK8K8vCBVPMCEAdf9CmgwE9DgshQ+kMEl iQgpqQZGtfp4jpnvC3hslPf9b4nosgw35M/yOe7C+z93p0XP3oanN/gCltt27GaM7p+1Iu8h 34JDAsnrDartjO+Hxv4IlSgP119bGHPt/8y1l8PtK8/2RZyees5nvl6TgfQSr4Vm6UdKTZU/ X5D5GPeu/Vf1R3+t4KMb45jCT+tFNK229F5607mi2V6JpTgj0VCLuag4EQD0ftmp1QIAAA== 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: Fri, 13 Jan 2012 23:07:12 -0000 --- notmuch-count.c | 53 +++++++++++++++++++++++++---------------------------- 1 files changed, 25 insertions(+), 28 deletions(-) diff --git a/notmuch-count.c b/notmuch-count.c index 20ce334..0982f99 100644 --- a/notmuch-count.c +++ b/notmuch-count.c @@ -21,6 +21,11 @@ #include "notmuch-client.h" +enum { + OUTPUT_THREADS, + OUTPUT_MESSAGES, +}; + int notmuch_count_command (void *ctx, int argc, char *argv[]) { @@ -28,35 +33,23 @@ notmuch_count_command (void *ctx, int argc, char *argv[]) notmuch_database_t *notmuch; notmuch_query_t *query; char *query_str; - int i; - notmuch_bool_t output_messages = TRUE; + int opt_index; + int output = OUTPUT_MESSAGES; - argc--; argv++; /* skip subcommand argument */ + notmuch_opt_desc_t options[] = { + { NOTMUCH_OPT_KEYWORD, &output, "output", 'o', + (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS }, + { "messages", OUTPUT_MESSAGES }, + { 0, 0 } } }, + { 0, 0, 0, 0, 0 } + }; - for (i = 0; i < argc && argv[i][0] == '-'; i++) { - if (strcmp (argv[i], "--") == 0) { - i++; - break; - } - if (STRNCMP_LITERAL (argv[i], "--output=") == 0) { - const char *opt = argv[i] + sizeof ("--output=") - 1; - if (strcmp (opt, "threads") == 0) { - output_messages = FALSE; - } else if (strcmp (opt, "messages") == 0) { - output_messages = TRUE; - } else { - fprintf (stderr, "Invalid value for --output: %s\n", opt); - return 1; - } - } else { - fprintf (stderr, "Unrecognized option: %s\n", argv[i]); - return 1; - } + opt_index = parse_arguments (argc, argv, options, 1); + + if (opt_index < 0) { + return 1; } - argc -= i; - argv += i; - config = notmuch_config_open (ctx, NULL, NULL); if (config == NULL) return 1; @@ -66,7 +59,7 @@ notmuch_count_command (void *ctx, int argc, char *argv[]) if (notmuch == NULL) return 1; - query_str = query_string_from_args (ctx, argc, argv); + query_str = query_string_from_args (ctx, argc-opt_index, argv+opt_index); if (query_str == NULL) { fprintf (stderr, "Out of memory.\n"); return 1; @@ -82,10 +75,14 @@ notmuch_count_command (void *ctx, int argc, char *argv[]) return 1; } - if (output_messages) + switch (output) { + case OUTPUT_MESSAGES: printf ("%u\n", notmuch_query_count_messages (query)); - else + break; + case OUTPUT_THREADS: printf ("%u\n", notmuch_query_count_threads (query)); + break; + } notmuch_query_destroy (query); notmuch_database_close (notmuch); -- 1.7.7.3