]> git.notmuchmail.org Git - notmuch/commitdiff
cli: add standard option processing to config, help and setup
authorDavid Bremner <david@tethera.net>
Sun, 5 Apr 2015 20:34:55 +0000 (05:34 +0900)
committerDavid Bremner <david@tethera.net>
Mon, 1 Jun 2015 05:32:54 +0000 (07:32 +0200)
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
notmuch-config.c
notmuch-setup.c
notmuch.c
test/random-corpus.c

index 8ecfac6b1cb4d8a92365c8e6f0af67d6c557d17b..78680aa122af56dc9cd9432982e51e713594db14 100644 (file)
@@ -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);
 #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
 #endif
index 2d5c297b7f5f2996b89c6e7c9502df90a06c93d6..934827874b1eb3548fa6b9fa1fdf4d9955174ee6 100644 (file)
@@ -872,8 +872,15 @@ int
 notmuch_config_command (notmuch_config_t *config, int argc, char *argv[])
 {
     int ret;
 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");
 
     if (argc < 1) {
        fprintf (stderr, "Error: notmuch config requires at least one argument.\n");
index 36a6171a695b2bde194eab0dab3a577c91e265fe..7dd5822a9365c0490b9860532de03c1c4c18251c 100644 (file)
@@ -145,6 +145,9 @@ notmuch_setup_command (notmuch_config_t *config,
        chomp_newline (response);                               \
     } while (0)
 
        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 ();
 
     if (notmuch_config_is_new (config))
        welcome_message_pre_setup ();
 
index 3ce30b456a369266d9831ccf089deeb66763f7ad..c528dce2d12100b66d9dffd00093faefd33f5965 100644 (file)
--- 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 *) &notmuch_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,
 
 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[])
 {
 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);
 
     if (argc == 0) {
        return _help_for (NULL);
index 790193d2ee3eed2f5b7edbb4b4e7f3637f6f23d4..b377eb40bfdce0964880e736f493f33f3e2066d0 100644 (file)
@@ -114,6 +114,23 @@ random_utf8_string (void *ctx, size_t char_count)
     return buf;
 }
 
     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
+notmuch_minimal_options (unused (const char *subcommand),
+                        unused (int argc),
+                        unused (char **argv))
+{
+    return 0;
+}
 
 int
 main (int argc, char **argv)
 
 int
 main (int argc, char **argv)