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 4A319429E35 for ; Sun, 13 Nov 2011 14:47:43 -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 aIF2gzVJAjhv for ; Sun, 13 Nov 2011 14:47:41 -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 11AEA429E21 for ; Sun, 13 Nov 2011 14:47:38 -0800 (PST) Received: by mail-bw0-f53.google.com with SMTP id q10so6354254bka.26 for ; Sun, 13 Nov 2011 14:47:38 -0800 (PST) Received: by 10.205.127.139 with SMTP id ha11mr9075269bkc.111.1321224458469; Sun, 13 Nov 2011 14:47:38 -0800 (PST) Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi. [80.220.92.23]) by mx.google.com with ESMTPS id x14sm28066514bkf.10.2011.11.13.14.47.36 (version=SSLv3 cipher=OTHER); Sun, 13 Nov 2011 14:47:37 -0800 (PST) From: Jani Nikula To: notmuch@notmuchmail.org, david@tethera.net Subject: [PATCH 2/6] cli: notmuch search: use getopt_long for parsing command line options Date: Mon, 14 Nov 2011 00:47:22 +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:43 -0000 Signed-off-by: Jani Nikula --- notmuch-search.c | 90 ++++++++++++++++++++++++++++------------------------- 1 files changed, 48 insertions(+), 42 deletions(-) diff --git a/notmuch-search.c b/notmuch-search.c index c62a594..4f7384a 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -417,81 +417,87 @@ notmuch_search_command (void *ctx, int argc, char *argv[]) notmuch_database_t *notmuch; notmuch_query_t *query; char *query_str; - char *opt; notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST; const search_format_t *format = &format_text; - int i, ret; + int ret; output_t output = OUTPUT_SUMMARY; int maxitems = -1; /* unlimited */ int first = 0; - argc--; argv++; /* skip subcommand argument */ - - for (i = 0; i < argc && argv[i][0] == '-'; i++) { - if (strcmp (argv[i], "--") == 0) { - i++; + while (1) { + char *p; + int opt; + static struct option options[] = { + { "sort", required_argument, NULL, 0 }, + { "first", required_argument, NULL, 1 }, + { "maxitems", required_argument, NULL, 2 }, + { "format", required_argument, NULL, 3 }, + { "output", required_argument, NULL, 4 }, + { NULL, 0, NULL, 0 }, + }; + + opt = getopt_long (argc, argv, "", options, NULL); + if (opt == -1) break; - } - if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) { - opt = argv[i] + sizeof ("--sort=") - 1; - if (strcmp (opt, "oldest-first") == 0) { + + switch (opt) { + case 0: + if (strcmp (optarg, "oldest-first") == 0) { sort = NOTMUCH_SORT_OLDEST_FIRST; - } else if (strcmp (opt, "newest-first") == 0) { + } else if (strcmp (optarg, "newest-first") == 0) { sort = NOTMUCH_SORT_NEWEST_FIRST; } else { - fprintf (stderr, "Invalid value for --sort: %s\n", opt); + fprintf (stderr, "Invalid value for --sort: %s\n", optarg); return 1; } - } else if (STRNCMP_LITERAL (argv[i], "--first=") == 0) { - char *p; - opt = argv[i] + sizeof ("--first=") - 1; - first = strtol (opt, &p, 10); - if (*opt == '\0' || p == opt || *p != '\0') { - fprintf (stderr, "Invalid value for --first: %s\n", opt); + break; + case 1: + first = strtol (optarg, &p, 10); + if (*optarg == '\0' || p == optarg || *p != '\0') { + fprintf (stderr, "Invalid value for --first: %s\n", optarg); return 1; } - } else if (STRNCMP_LITERAL (argv[i], "--maxitems=") == 0) { - char *p; - opt = argv[i] + sizeof ("--maxitems=") - 1; - maxitems = strtoul (opt, &p, 10); - if (*opt == '\0' || p == opt || *p != '\0') { - fprintf (stderr, "Invalid value for --maxitems: %s\n", opt); + break; + case 2: + maxitems = strtoul (optarg, &p, 10); + if (*optarg == '\0' || p == optarg || *p != '\0') { + fprintf (stderr, "Invalid value for --maxitems: %s\n", optarg); return 1; } - } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) { - opt = argv[i] + sizeof ("--format=") - 1; - if (strcmp (opt, "text") == 0) { + break; + case 3: + if (strcmp (optarg, "text") == 0) { format = &format_text; - } else if (strcmp (opt, "json") == 0) { + } else if (strcmp (optarg, "json") == 0) { format = &format_json; } 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], "--output=") == 0) { - opt = argv[i] + sizeof ("--output=") - 1; - if (strcmp (opt, "summary") == 0) { + break; + case 4: + if (strcmp (optarg, "summary") == 0) { output = OUTPUT_SUMMARY; - } else if (strcmp (opt, "threads") == 0) { + } else if (strcmp (optarg, "threads") == 0) { output = OUTPUT_THREADS; - } else if (strcmp (opt, "messages") == 0) { + } else if (strcmp (optarg, "messages") == 0) { output = OUTPUT_MESSAGES; - } else if (strcmp (opt, "files") == 0) { + } else if (strcmp (optarg, "files") == 0) { output = OUTPUT_FILES; - } else if (strcmp (opt, "tags") == 0) { + } else if (strcmp (optarg, "tags") == 0) { output = OUTPUT_TAGS; } 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