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 B5243429E35 for ; Sun, 13 Nov 2011 14:47:46 -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 NE4nu0Dq5vHM for ; Sun, 13 Nov 2011 14:47:46 -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 0B752429E21 for ; Sun, 13 Nov 2011 14:47:45 -0800 (PST) Received: by mail-bw0-f53.google.com with SMTP id q10so6354254bka.26 for ; Sun, 13 Nov 2011 14:47:45 -0800 (PST) Received: by 10.205.128.19 with SMTP id hc19mr16953500bkc.9.1321224465567; Sun, 13 Nov 2011 14:47:45 -0800 (PST) Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi. [80.220.92.23]) by mx.google.com with ESMTPS id a4sm18095799bkq.13.2011.11.13.14.47.43 (version=SSLv3 cipher=OTHER); Sun, 13 Nov 2011 14:47:44 -0800 (PST) From: Jani Nikula To: notmuch@notmuchmail.org, david@tethera.net Subject: [PATCH 4/6] cli: notmuch count: use getopt_long for parsing command line options Date: Mon, 14 Nov 2011 00:47:24 +0200 Message-Id: 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:47:46 -0000 Signed-off-by: Jani Nikula --- notmuch-count.c | 33 ++++++++++++++++++--------------- 1 files changed, 18 insertions(+), 15 deletions(-) diff --git a/notmuch-count.c b/notmuch-count.c index 20ce334..4987ca8 100644 --- a/notmuch-count.c +++ b/notmuch-count.c @@ -28,34 +28,37 @@ 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; - argc--; argv++; /* skip subcommand argument */ + while (1) { + int opt; + static struct option options[] = { + { "output", required_argument, NULL, 0 }, + { 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], "--output=") == 0) { - const char *opt = argv[i] + sizeof ("--output=") - 1; - if (strcmp (opt, "threads") == 0) { + + switch (opt) { + case 0: + if (strcmp (optarg, "threads") == 0) { output_messages = FALSE; - } else if (strcmp (opt, "messages") == 0) { + } else if (strcmp (optarg, "messages") == 0) { output_messages = TRUE; } 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