Re: [PATCH 1/3] count: Convert to new-style argument parsing
authorJani Nikula <jani@nikula.org>
Wed, 11 Jan 2012 08:17:05 +0000 (08:17 +0000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:41:53 +0000 (09:41 -0800)
a3/54890383b462e77f1526a26969d4a367a66c31 [new file with mode: 0644]

diff --git a/a3/54890383b462e77f1526a26969d4a367a66c31 b/a3/54890383b462e77f1526a26969d4a367a66c31
new file mode 100644 (file)
index 0000000..9157dcd
--- /dev/null
@@ -0,0 +1,177 @@
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 58894431FD0\r
+       for <notmuch@notmuchmail.org>; Wed, 11 Jan 2012 00:17:14 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id Tjj4mk9yjBqh for <notmuch@notmuchmail.org>;\r
+       Wed, 11 Jan 2012 00:17:13 -0800 (PST)\r
+Received: from mail-qw0-f46.google.com (mail-qw0-f46.google.com\r
+       [209.85.216.46]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 9702A431FB6\r
+       for <notmuch@notmuchmail.org>; Wed, 11 Jan 2012 00:17:13 -0800 (PST)\r
+Received: by qats34 with SMTP id s34so2564896qat.5\r
+       for <notmuch@notmuchmail.org>; Wed, 11 Jan 2012 00:17:12 -0800 (PST)\r
+Received: by 10.224.116.16 with SMTP id k16mr26394533qaq.28.1326269829950;\r
+       Wed, 11 Jan 2012 00:17:09 -0800 (PST)\r
+Received: from localhost (nikula.org. [92.243.24.172])\r
+       by mx.google.com with ESMTPS id o8sm2215121qaj.0.2012.01.11.00.17.07\r
+       (version=SSLv3 cipher=OTHER); Wed, 11 Jan 2012 00:17:08 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 1/3] count: Convert to new-style argument parsing\r
+In-Reply-To: <1326258173-21163-2-git-send-email-amdragon@mit.edu>\r
+References: <20120109043101.GH20796@mit.edu>\r
+       <1326258173-21163-1-git-send-email-amdragon@mit.edu>\r
+       <1326258173-21163-2-git-send-email-amdragon@mit.edu>\r
+User-Agent: Notmuch/0.10.2+129~g8a76c81 (http://notmuchmail.org) Emacs/23.1.1\r
+       (i686-pc-linux-gnu)\r
+Date: Wed, 11 Jan 2012 08:17:05 +0000\r
+Message-ID: <87zkdulkla.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Wed, 11 Jan 2012 08:17:14 -0000\r
+\r
+On Wed, 11 Jan 2012 00:02:51 -0500, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> ---\r
+>  notmuch-count.c |   53 +++++++++++++++++++++++++----------------------------\r
+>  1 files changed, 25 insertions(+), 28 deletions(-)\r
+> \r
+> diff --git a/notmuch-count.c b/notmuch-count.c\r
+> index 20ce334..fb7401b 100644\r
+> --- a/notmuch-count.c\r
+> +++ b/notmuch-count.c\r
+> @@ -21,6 +21,11 @@\r
+>  \r
+>  #include "notmuch-client.h"\r
+>  \r
+> +typedef enum {\r
+> +    OUTPUT_THREADS,\r
+> +    OUTPUT_MESSAGES,\r
+> +} output_t;\r
+> +\r
+>  int\r
+>  notmuch_count_command (void *ctx, int argc, char *argv[])\r
+>  {\r
+> @@ -28,35 +33,23 @@ notmuch_count_command (void *ctx, int argc, char *argv[])\r
+>      notmuch_database_t *notmuch;\r
+>      notmuch_query_t *query;\r
+>      char *query_str;\r
+> -    int i;\r
+> -    notmuch_bool_t output_messages = TRUE;\r
+> +    int opt_index;\r
+> +    output_t output = OUTPUT_MESSAGES;\r
+>  \r
+> -    argc--; argv++; /* skip subcommand argument */\r
+> +    notmuch_opt_desc_t options[] = {\r
+> +    { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',\r
+> +      (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },\r
+> +                              { "messages", OUTPUT_MESSAGES },\r
+> +                              { 0, 0 } } },\r
+\r
+To be pedantic, parse_arguments() expects 'output_var' to be a pointer\r
+to int for NOTMUCH_OPT_KEYWORD. sizeof(enum) is implementation\r
+dependent. I would forget about output_t typedef, use plain enum, and\r
+int type for 'output'.\r
+\r
+In fact, this is exactly what I did in commits\r
+e6d89ad723f775952d89d4f81b6072617c5caf18 and\r
+be5619cca32452f1a398add85908d78d6e72f469. In the latter I could have\r
+used notmuch_bool_t because it's a typedeffed int, but then I would've\r
+depended on some external typedef not changing (it probably won't, but\r
+this is defensive programming).\r
+\r
+Otherwise, looks good.\r
+\r
+BR,\r
+Jani.\r
+\r
+\r
+> +    { 0, 0, 0, 0, 0 }\r
+> +    };\r
+>  \r
+> -    for (i = 0; i < argc && argv[i][0] == '-'; i++) {\r
+> -    if (strcmp (argv[i], "--") == 0) {\r
+> -        i++;\r
+> -        break;\r
+> -    }\r
+> -    if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {\r
+> -        const char *opt = argv[i] + sizeof ("--output=") - 1;\r
+> -        if (strcmp (opt, "threads") == 0) {\r
+> -            output_messages = FALSE;\r
+> -        } else if (strcmp (opt, "messages") == 0) {\r
+> -            output_messages = TRUE;\r
+> -        } else {\r
+> -            fprintf (stderr, "Invalid value for --output: %s\n", opt);\r
+> -            return 1;\r
+> -        }\r
+> -    } else {\r
+> -        fprintf (stderr, "Unrecognized option: %s\n", argv[i]);\r
+> -        return 1;\r
+> -    }\r
+> +    opt_index = parse_arguments (argc, argv, options, 1);\r
+> +\r
+> +    if (opt_index < 0) {\r
+> +    return 1;\r
+>      }\r
+>  \r
+> -    argc -= i;\r
+> -    argv += i;\r
+> -\r
+>      config = notmuch_config_open (ctx, NULL, NULL);\r
+>      if (config == NULL)\r
+>      return 1;\r
+> @@ -66,7 +59,7 @@ notmuch_count_command (void *ctx, int argc, char *argv[])\r
+>      if (notmuch == NULL)\r
+>      return 1;\r
+>  \r
+> -    query_str = query_string_from_args (ctx, argc, argv);\r
+> +    query_str = query_string_from_args (ctx, argc-opt_index, argv+opt_index);\r
+>      if (query_str == NULL) {\r
+>      fprintf (stderr, "Out of memory.\n");\r
+>      return 1;\r
+> @@ -82,10 +75,14 @@ notmuch_count_command (void *ctx, int argc, char *argv[])\r
+>      return 1;\r
+>      }\r
+>  \r
+> -    if (output_messages)\r
+> +    switch (output) {\r
+> +    case OUTPUT_MESSAGES:\r
+>      printf ("%u\n", notmuch_query_count_messages (query));\r
+> -    else\r
+> +    break;\r
+> +    case OUTPUT_THREADS:\r
+>      printf ("%u\n", notmuch_query_count_threads (query));\r
+> +    break;\r
+> +    }\r
+>  \r
+>      notmuch_query_destroy (query);\r
+>      notmuch_database_close (notmuch);\r
+> -- \r
+> 1.7.7.3\r
+> \r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r