Re: [PATCH 1/2] cli: convert "notmuch show" to use the new argument parser
authorJani Nikula <jani@nikula.org>
Mon, 6 Feb 2012 07:54:02 +0000 (07:54 +0000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:44:10 +0000 (09:44 -0800)
c7/8e0d22ceb7663bd1788a28ab74f660c693a9a1 [new file with mode: 0644]

diff --git a/c7/8e0d22ceb7663bd1788a28ab74f660c693a9a1 b/c7/8e0d22ceb7663bd1788a28ab74f660c693a9a1
new file mode 100644 (file)
index 0000000..2cf725f
--- /dev/null
@@ -0,0 +1,308 @@
+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 E4DBF431FAF\r
+       for <notmuch@notmuchmail.org>; Sun,  5 Feb 2012 23:54:07 -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 YBjgbDAsYSkw for <notmuch@notmuchmail.org>;\r
+       Sun,  5 Feb 2012 23:54:06 -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 C6A5C431FAE\r
+       for <notmuch@notmuchmail.org>; Sun,  5 Feb 2012 23:54:06 -0800 (PST)\r
+Received: by qadc10 with SMTP id c10so2257013qad.5\r
+       for <notmuch@notmuchmail.org>; Sun, 05 Feb 2012 23:54:06 -0800 (PST)\r
+Received: by 10.224.197.5 with SMTP id ei5mr19060677qab.73.1328514846090;\r
+       Sun, 05 Feb 2012 23:54:06 -0800 (PST)\r
+Received: from localhost (nikula.org. [92.243.24.172])\r
+       by mx.google.com with ESMTPS id dk2sm32206242qab.12.2012.02.05.23.54.04\r
+       (version=SSLv3 cipher=OTHER); Sun, 05 Feb 2012 23:54:05 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: Austin Clements <amdragon@MIT.EDU>\r
+Subject: Re: [PATCH 1/2] cli: convert "notmuch show" to use the new argument\r
+       parser\r
+In-Reply-To: <20120206041205.GP10898@mit.edu>\r
+References:\r
+ <df2e9bd7682e9c81e1b205cc1d92a1526cdd1a9b.1328308635.git.jani@nikula.org>\r
+       <20120206041205.GP10898@mit.edu>\r
+User-Agent: Notmuch/0.10.2+187~g43d4f26 (http://notmuchmail.org) Emacs/23.1.1\r
+       (i686-pc-linux-gnu)\r
+Date: Mon, 06 Feb 2012 07:54:02 +0000\r
+Message-ID: <87ty34bdnp.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Cc: notmuch@notmuchmail.org\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: Mon, 06 Feb 2012 07:54:08 -0000\r
+\r
+On Sun, 5 Feb 2012 23:12:05 -0500, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> Yikes.  I don't envy you this patch.  Two minor nits, otherwise this\r
+> and the next patch LGTM.\r
+\r
+Thanks for the review. I'll roll another version, I think it will be\r
+better with your suggestions incorporated.\r
+\r
+> Quoth Jani Nikula on Feb 04 at 12:41 am:\r
+> > Use the new notmuch argument parser to handle arguments in "notmuch\r
+> > show". There are two corner case functional changes:\r
+> > \r
+> > 1) Also set params.raw = 1 when defaulting to raw format when part is\r
+> >    requested but format is not specified.\r
+> \r
+> Huh.  So "notmuch show --part=0 <query>" was broken before.\r
+\r
+Hmm, yes, it seems so. Do you think I should make this a separate fix?\r
+\r
+BTW an alternative would be to require setting --format if --part is\r
+specified, and not adapt the default format depending on --part.\r
+\r
+> > 2) Do not set params.decrypt if crypto context creation fails.\r
+> \r
+> Technically this also behaves differently if given multiple --format\r
+> arguments, but I'll let that slide.\r
+\r
+Ugh, right, the old parsing was broken also that way. Luckily that falls\r
+in the corner case department too.\r
+\r
+> > \r
+> > Signed-off-by: Jani Nikula <jani@nikula.org>\r
+> > ---\r
+> >  notmuch-show.c |  153 +++++++++++++++++++++++++++++---------------------------\r
+> >  1 files changed, 79 insertions(+), 74 deletions(-)\r
+> > \r
+> > diff --git a/notmuch-show.c b/notmuch-show.c\r
+> > index dec799c..f93e121 100644\r
+> > --- a/notmuch-show.c\r
+> > +++ b/notmuch-show.c\r
+> > @@ -1049,6 +1049,14 @@ do_show (void *ctx,\r
+> >      return 0;\r
+> >  }\r
+> >  \r
+> > +enum {\r
+> > +    NOTMUCH_FORMAT_NOT_SPECIFIED,\r
+> > +    NOTMUCH_FORMAT_JSON,\r
+> > +    NOTMUCH_FORMAT_TEXT,\r
+> > +    NOTMUCH_FORMAT_MBOX,\r
+> > +    NOTMUCH_FORMAT_RAW\r
+> > +};\r
+> > +\r
+> >  int\r
+> >  notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+> >  {\r
+> > @@ -1056,92 +1064,98 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+> >      notmuch_database_t *notmuch;\r
+> >      notmuch_query_t *query;\r
+> >      char *query_string;\r
+> > -    char *opt;\r
+> > +    int opt_index;\r
+> >      const notmuch_show_format_t *format = &format_text;\r
+> > -    notmuch_show_params_t params;\r
+> > -    int mbox = 0;\r
+> > -    int format_specified = 0;\r
+> > -    int i;\r
+> > +    notmuch_show_params_t params = { .part = -1 };\r
+> > +    int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;\r
+> > +    notmuch_bool_t decrypt = FALSE;\r
+> > +    notmuch_bool_t verify = FALSE;\r
+> > +    notmuch_bool_t entire_thread = FALSE;\r
+> > +\r
+> > +    notmuch_opt_desc_t options[] = {\r
+> > +  { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',\r
+> > +    (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },\r
+> > +                            { "text", NOTMUCH_FORMAT_TEXT },\r
+> > +                            { "mbox", NOTMUCH_FORMAT_MBOX },\r
+> > +                            { "raw", NOTMUCH_FORMAT_RAW },\r
+> > +                            { 0, 0 } } },\r
+> > +  { NOTMUCH_OPT_INT, &params.part, "part", 'p', 0 },\r
+> > +  { NOTMUCH_OPT_BOOLEAN, &entire_thread, "entire-thread", 't', 0 },\r
+> > +  { NOTMUCH_OPT_BOOLEAN, &decrypt, "decrypt", 'd', 0 },\r
+> > +  { NOTMUCH_OPT_BOOLEAN, &verify, "verify", 'v', 0 },\r
+> > +  { 0, 0, 0, 0, 0 }\r
+> > +    };\r
+> > +\r
+> > +    opt_index = parse_arguments (argc, argv, options, 1);\r
+> > +    if (opt_index < 0) {\r
+> > +  /* diagnostics already printed */\r
+> > +  return 1;\r
+> > +    }\r
+> >  \r
+> > -    params.entire_thread = 0;\r
+> > -    params.raw = 0;\r
+> > -    params.part = -1;\r
+> > -    params.cryptoctx = NULL;\r
+> > -    params.decrypt = 0;\r
+> > +    params.entire_thread = entire_thread;\r
+> \r
+> If you make params.entire_thread a notmuch_bool_t (instead of an int),\r
+> you could pass &params.entire_thread in the notmuch_opt_desc_t and get\r
+> rid of the local variable.\r
+\r
+You're right; I was a bit lazy and didn't consider changing\r
+notmuch_show_params_t. I'll change both this and params.decrypt to\r
+notmuch_bool_t.\r
+\r
+> \r
+> >  \r
+> > -    argc--; argv++; /* skip subcommand argument */\r
+> > +    if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {\r
+> > +  /* if part was requested and format was not specified, use format=raw */\r
+> > +  if (params.part >= 0)\r
+> > +      format_sel = NOTMUCH_FORMAT_RAW;\r
+> > +  else\r
+> > +      format_sel = NOTMUCH_FORMAT_TEXT;\r
+> > +    }\r
+> >  \r
+> > -    for (i = 0; i < argc && argv[i][0] == '-'; i++) {\r
+> > -  if (strcmp (argv[i], "--") == 0) {\r
+> > -      i++;\r
+> > -      break;\r
+> > +    switch (format_sel) {\r
+> > +    case NOTMUCH_FORMAT_JSON:\r
+> > +  format = &format_json;\r
+> > +  params.entire_thread = 1;\r
+> > +  break;\r
+> > +    case NOTMUCH_FORMAT_TEXT:\r
+> > +  format = &format_text;\r
+> > +  break;\r
+> > +    case NOTMUCH_FORMAT_MBOX:\r
+> > +  if (params.part > 0) {\r
+> > +      fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");\r
+> > +      return 1;\r
+> >    }\r
+> > -  if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {\r
+> > -      opt = argv[i] + sizeof ("--format=") - 1;\r
+> > -      if (strcmp (opt, "text") == 0) {\r
+> > -          format = &format_text;\r
+> > -      } else if (strcmp (opt, "json") == 0) {\r
+> > -          format = &format_json;\r
+> > -          params.entire_thread = 1;\r
+> > -      } else if (strcmp (opt, "mbox") == 0) {\r
+> > -          format = &format_mbox;\r
+> > -          mbox = 1;\r
+> > -      } else if (strcmp (opt, "raw") == 0) {\r
+> > -          format = &format_raw;\r
+> > -          params.raw = 1;\r
+> > -      } else {\r
+> > -          fprintf (stderr, "Invalid value for --format: %s\n", opt);\r
+> > -          return 1;\r
+> > -      }\r
+> > -      format_specified = 1;\r
+> > -  } else if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {\r
+> > -      params.part = atoi(argv[i] + sizeof ("--part=") - 1);\r
+> > -  } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {\r
+> > -      params.entire_thread = 1;\r
+> > -  } else if ((STRNCMP_LITERAL (argv[i], "--verify") == 0) ||\r
+> > -             (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) {\r
+> > -      if (params.cryptoctx == NULL) {\r
+> > +  format = &format_mbox;\r
+> > +  break;\r
+> > +    case NOTMUCH_FORMAT_RAW:\r
+> > +  format = &format_raw;\r
+> > +  /* If --format=raw specified without specifying part, we can only\r
+> > +   * output single message, so set part=0 */\r
+> > +  if (params.part < 0)\r
+> > +      params.part = 0;\r
+> > +  params.raw = 1;\r
+> > +  break;\r
+> > +    }\r
+> > +\r
+> > +    if (decrypt || verify) {\r
+> >  #ifdef GMIME_ATLEAST_26\r
+> > -          /* TODO: GMimePasswordRequestFunc */\r
+> > -          if (NULL == (params.cryptoctx = g_mime_gpg_context_new(NULL, "gpg")))\r
+> > +  /* TODO: GMimePasswordRequestFunc */\r
+> > +  params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");\r
+> >  #else\r
+> > -          GMimeSession* session = g_object_new(g_mime_session_get_type(), NULL);\r
+> > -          if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))\r
+> > -#endif\r
+> > -              fprintf (stderr, "Failed to construct gpg context.\n");\r
+> > -          else\r
+> > -              g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);\r
+> > -#ifndef GMIME_ATLEAST_26\r
+> > -          g_object_unref (session);\r
+> > -          session = NULL;\r
+> > +  GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);\r
+> > +  params.cryptoctx = g_mime_gpg_context_new (session, "gpg");\r
+> >  #endif\r
+> > -      }\r
+> > -      if (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)\r
+> > -          params.decrypt = 1;\r
+> > +  if (params.cryptoctx) {\r
+> > +      g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);\r
+> > +      params.decrypt = decrypt;\r
+> \r
+> Would it be easier to pass &params.decrypt in the notmuch_opt_desc_t\r
+> and set it to FALSE in the 'else' branch of this?\r
+\r
+Agreed. That can be done if decrypt is changed to notmuch_bool_t.\r
+\r
+> \r
+> >    } else {\r
+> > -      fprintf (stderr, "Unrecognized option: %s\n", argv[i]);\r
+> > -      return 1;\r
+> > +      fprintf (stderr, "Failed to construct gpg context.\n");\r
+> >    }\r
+> > +#ifndef GMIME_ATLEAST_26\r
+> > +  g_object_unref (session);\r
+> > +#endif\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
+> >  \r
+> > -    query_string = query_string_from_args (ctx, argc, argv);\r
+> > +    query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);\r
+> >      if (query_string == NULL) {\r
+> >    fprintf (stderr, "Out of memory\n");\r
+> >    return 1;\r
+> >      }\r
+> >  \r
+> > -    if (mbox && params.part > 0) {\r
+> > -  fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");\r
+> > -  return 1;\r
+> > -    }\r
+> > -\r
+> >      if (*query_string == '\0') {\r
+> >    fprintf (stderr, "Error: notmuch show requires at least one search term.\n");\r
+> >    return 1;\r
+> > @@ -1158,15 +1172,6 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+> >    return 1;\r
+> >      }\r
+> >  \r
+> > -    /* if part was requested and format was not specified, use format=raw */\r
+> > -    if (params.part >= 0 && !format_specified)\r
+> > -  format = &format_raw;\r
+> > -\r
+> > -    /* If --format=raw specified without specifying part, we can only\r
+> > -     * output single message, so set part=0 */\r
+> > -    if (params.raw && params.part < 0)\r
+> > -  params.part = 0;\r
+> > -\r
+> >      if (params.part >= 0)\r
+> >    return do_show_single (ctx, query, format, &params);\r
+> >      else\r