From 72b133676894c4e0a1870abc75290773c87cda61 Mon Sep 17 00:00:00 2001 From: David Bremner Date: Wed, 8 Apr 2015 04:30:42 +0900 Subject: [PATCH] [PATCH 4/4] cli: add standard option processing to config, help and setup --- 36/2aec6e132f752e5107263e02ca657de71738e2 | 180 ++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 36/2aec6e132f752e5107263e02ca657de71738e2 diff --git a/36/2aec6e132f752e5107263e02ca657de71738e2 b/36/2aec6e132f752e5107263e02ca657de71738e2 new file mode 100644 index 000000000..45cb8b5b4 --- /dev/null +++ b/36/2aec6e132f752e5107263e02ca657de71738e2 @@ -0,0 +1,180 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by arlo.cworth.org (Postfix) with ESMTP id B17516DE1B09 + for ; Tue, 7 Apr 2015 12:33:40 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at cworth.org +X-Spam-Flag: NO +X-Spam-Score: 0.419 +X-Spam-Level: +X-Spam-Status: No, score=0.419 tagged_above=-999 required=5 tests=[AWL=0.409, + T_HEADER_FROM_DIFFERENT_DOMAINS=0.01] autolearn=disabled +Received: from arlo.cworth.org ([127.0.0.1]) + by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id 62ymMWw1fqm9 for ; + Tue, 7 Apr 2015 12:33:38 -0700 (PDT) +Received: from mx.xen14.node3324.gplhost.com (gitolite.debian.net + [87.98.215.224]) + by arlo.cworth.org (Postfix) with ESMTPS id 8DCDD6DE1B00 + for ; Tue, 7 Apr 2015 12:33:38 -0700 (PDT) +Received: from remotemail by mx.xen14.node3324.gplhost.com with local (Exim + 4.80) (envelope-from ) + id 1YfZE9-0001y9-4H; Tue, 07 Apr 2015 19:32:01 +0000 +Received: (nullmailer pid 16656 invoked by uid 1000); Tue, 07 Apr 2015 + 19:30:51 -0000 +From: David Bremner +To: Mark Walters , David Bremner + , notmuch@notmuchmail.org +Subject: [PATCH 4/4] cli: add standard option processing to config, help and + setup +Date: Wed, 8 Apr 2015 04:30:42 +0900 +Message-Id: <1428435042-16503-5-git-send-email-david@tethera.net> +X-Mailer: git-send-email 2.1.4 +In-Reply-To: <1428435042-16503-1-git-send-email-david@tethera.net> +References: <871tjws8w8.fsf@qmul.ac.uk> + <1428435042-16503-1-git-send-email-david@tethera.net> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.18 +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: Tue, 07 Apr 2015 19:33:40 -0000 + +In particular this fixes a recently encountered bug where the +"--config" argument to "notmuch setup" is silently ignored, which the +unpleasant consequence of overwriting the users config file. +--- + notmuch-client.h | 2 ++ + notmuch-config.c | 9 ++++++++- + notmuch-setup.c | 3 +++ + notmuch.c | 32 +++++++++++++++++++++++++++++++- + test/random-corpus.c | 9 +++++++++ + 5 files changed, 53 insertions(+), 2 deletions(-) + +diff --git a/notmuch-client.h b/notmuch-client.h +index 8ecfac6..78680aa 100644 +--- a/notmuch-client.h ++++ b/notmuch-client.h +@@ -468,4 +468,6 @@ notmuch_database_dump (notmuch_database_t *notmuch, + #include "command-line-arguments.h" + extern const notmuch_opt_desc_t notmuch_shared_options []; + void notmuch_process_shared_options (const char* subcommand_name); ++int notmuch_minimal_options (const char* subcommand_name, ++ int argc, char **argv); + #endif +diff --git a/notmuch-config.c b/notmuch-config.c +index 2d5c297..9348278 100644 +--- a/notmuch-config.c ++++ b/notmuch-config.c +@@ -872,8 +872,15 @@ int + notmuch_config_command (notmuch_config_t *config, int argc, char *argv[]) + { + int ret; ++ int opt_index; + +- argc--; argv++; /* skip subcommand argument */ ++ opt_index = notmuch_minimal_options ("config", argc, argv); ++ if (opt_index < 0) ++ return EXIT_FAILURE; ++ ++ /* skip at least subcommand argument */ ++ argc-= opt_index; ++ argv+= opt_index; + + if (argc < 1) { + fprintf (stderr, "Error: notmuch config requires at least one argument.\n"); +diff --git a/notmuch-setup.c b/notmuch-setup.c +index 36a6171..7dd5822 100644 +--- a/notmuch-setup.c ++++ b/notmuch-setup.c +@@ -145,6 +145,9 @@ notmuch_setup_command (notmuch_config_t *config, + chomp_newline (response); \ + } while (0) + ++ if (notmuch_minimal_options ("setup", argc, argv) < 0) ++ return EXIT_FAILURE; ++ + if (notmuch_config_is_new (config)) + welcome_message_pre_setup (); + +diff --git a/notmuch.c b/notmuch.c +index 3a9da90..2198b73 100644 +--- a/notmuch.c ++++ b/notmuch.c +@@ -71,6 +71,28 @@ notmuch_process_shared_options (const char *subcommand_name) { + } + } + ++/* This is suitable for subcommands that do not actually open the ++ * database. ++ */ ++int notmuch_minimal_options (const char *subcommand_name, ++ int argc, char **argv) ++{ ++ int opt_index; ++ ++ notmuch_opt_desc_t options[] = { ++ { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 }, ++ { 0, 0, 0, 0, 0 } ++ }; ++ ++ opt_index = parse_arguments (argc, argv, options, 1); ++ ++ if (opt_index < 0) ++ return -1; ++ ++ /* We can't use argv here as it is sometimes NULL */ ++ notmuch_process_shared_options (subcommand_name); ++ return opt_index; ++} + + static command_t commands[] = { + { NULL, notmuch_command, TRUE, +@@ -250,7 +272,15 @@ _help_for (const char *topic_name) + static int + notmuch_help_command (unused (notmuch_config_t * config), int argc, char *argv[]) + { +- argc--; argv++; /* Ignore "help" */ ++ int opt_index; ++ ++ opt_index = notmuch_minimal_options ("help", argc, argv); ++ if (opt_index < 0) ++ return EXIT_FAILURE; ++ ++ /* skip at least subcommand argument */ ++ argc-= opt_index; ++ argv+= opt_index; + + if (argc == 0) { + return _help_for (NULL); +diff --git a/test/random-corpus.c b/test/random-corpus.c +index 790193d..6c467bb 100644 +--- a/test/random-corpus.c ++++ b/test/random-corpus.c +@@ -114,6 +114,15 @@ random_utf8_string (void *ctx, size_t char_count) + return buf; + } + ++/* stubs since we cannot link with notmuch.o */ ++const notmuch_opt_desc_t notmuch_shared_options[] = { ++ { 0, 0, 0, 0, 0 } ++}; ++ ++void ++notmuch_process_shared_options (unused (const char *dummy)) ++{ ++} + + int + main (int argc, char **argv) +-- +2.1.4 + -- 2.26.2