[RFC PATCH 1/2] cli: add support for global options that get passed to sub-commands

Subject: [RFC PATCH 1/2] cli: add support for global options that get passed to sub-commands

Date: Sat, 31 Aug 2013 13:34:30 +0300

To: notmuch@notmuchmail.org

Cc:

From: Jani Nikula


This allows sub-commands to have access to global options.
---
 notmuch-client.h |   21 +++++++++++++++++++++
 notmuch-config.c |   29 +++++++++++++++++++++++++++++
 notmuch.c        |   10 ++++++++++
 3 files changed, 60 insertions(+)

diff --git a/notmuch-client.h b/notmuch-client.h
index afb0ddf..5656426 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -320,6 +320,27 @@ notmuch_config_set_search_exclude_tags (notmuch_config_t *config,
 				      const char *list[],
 				      size_t length);
 
+/* global options */
+typedef struct _notmuch_global_options {
+    notmuch_bool_t debug;
+    notmuch_bool_t verbose;
+    notmuch_bool_t no_hooks;
+} notmuch_global_options_t;
+
+void
+notmuch_global_set (notmuch_config_t *config,
+		    const notmuch_global_options_t *global_options);
+
+notmuch_bool_t
+notmuch_global_get_debug (notmuch_config_t *config);
+
+notmuch_bool_t
+notmuch_global_get_verbose (notmuch_config_t *config);
+
+notmuch_bool_t
+notmuch_global_get_no_hooks (notmuch_config_t *config);
+
+
 int
 notmuch_run_hook (const char *db_path, const char *hook);
 
diff --git a/notmuch-config.c b/notmuch-config.c
index 6845e3c..4cb29eb 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -118,6 +118,9 @@ struct _notmuch_config {
     notmuch_bool_t maildir_synchronize_flags;
     const char **search_exclude_tags;
     size_t search_exclude_tags_length;
+
+    /* global options */
+    notmuch_global_options_t global_options;
 };
 
 static int
@@ -883,3 +886,29 @@ notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,
 			    "maildir", "synchronize_flags", synchronize_flags);
     config->maildir_synchronize_flags = synchronize_flags;
 }
+
+/* global options */
+void
+notmuch_global_set (notmuch_config_t *config,
+		    const notmuch_global_options_t *global_options)
+{
+    config->global_options = *global_options;
+}
+
+notmuch_bool_t
+notmuch_global_get_debug (notmuch_config_t *config)
+{
+    return config->global_options.debug;
+}
+
+notmuch_bool_t
+notmuch_global_get_verbose (notmuch_config_t *config)
+{
+    return config->global_options.verbose;
+}
+
+notmuch_bool_t
+notmuch_global_get_no_hooks (notmuch_config_t *config)
+{
+    return config->global_options.no_hooks;
+}
diff --git a/notmuch.c b/notmuch.c
index 78d29a8..11cdfd7 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -252,11 +252,19 @@ main (int argc, char *argv[])
     notmuch_bool_t print_help=FALSE, print_version=FALSE;
     int opt_index;
     int ret = 0;
+    notmuch_global_options_t global_options = {
+	.debug = FALSE,
+	.verbose = FALSE,
+	.no_hooks = FALSE,
+    };
 
     notmuch_opt_desc_t options[] = {
 	{ NOTMUCH_OPT_BOOLEAN, &print_help, "help", 'h', 0 },
 	{ NOTMUCH_OPT_BOOLEAN, &print_version, "version", 'v', 0 },
 	{ NOTMUCH_OPT_STRING, &config_file_name, "config", 'c', 0 },
+	{ NOTMUCH_OPT_BOOLEAN, &global_options.debug, "debug", 'd', 0 },
+	{ NOTMUCH_OPT_BOOLEAN, &global_options.verbose, "verbose", 'V', 0 },
+	{ NOTMUCH_OPT_BOOLEAN, &global_options.no_hooks, "no-hooks", 'N', 0 },
 	{ 0, 0, 0, 0, 0 }
     };
 
@@ -300,6 +308,8 @@ main (int argc, char *argv[])
     if (!config)
 	return 1;
 
+    notmuch_global_set (config, &global_options);
+
     ret = (command->function)(config, argc - opt_index, argv + opt_index);
 
     notmuch_config_close (config);
-- 
1.7.10.4


Thread: