]> git.notmuchmail.org Git - notmuch/commitdiff
cli: search: Convert --output to keyword-flag argument
authorMichal Sojka <sojkam1@fel.cvut.cz>
Fri, 31 Oct 2014 21:53:57 +0000 (22:53 +0100)
committerDavid Bremner <david@tethera.net>
Sat, 1 Nov 2014 07:02:35 +0000 (08:02 +0100)
This converts "notmuch search" to use the recently introduced
keyword-flag argument parser. At this point, it only makes the code
slightly less readable but following commits that add new --output
keywords will profit from this.

notmuch-search.c

index 0c3e9724163d863c7af9e622a00064f581644de8..ce46877895701e07d6e899daf187dd2c7eaeb37c 100644 (file)
 #include "string-util.h"
 
 typedef enum {
-    OUTPUT_SUMMARY,
-    OUTPUT_THREADS,
-    OUTPUT_MESSAGES,
-    OUTPUT_FILES,
-    OUTPUT_TAGS
+    OUTPUT_SUMMARY     = 1 << 0,
+    OUTPUT_THREADS     = 1 << 1,
+    OUTPUT_MESSAGES    = 1 << 2,
+    OUTPUT_FILES       = 1 << 3,
+    OUTPUT_TAGS                = 1 << 4,
 } output_t;
 
 typedef struct {
@@ -338,7 +338,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
     notmuch_database_t *notmuch;
     search_options_t opt = {
        .sort = NOTMUCH_SORT_NEWEST_FIRST,
-       .output = OUTPUT_SUMMARY,
+       .output = 0,
        .offset = 0,
        .limit = -1, /* unlimited */
        .dupe = -1,
@@ -367,7 +367,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
                                  { "text0", NOTMUCH_FORMAT_TEXT0 },
                                  { 0, 0 } } },
        { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },
-       { NOTMUCH_OPT_KEYWORD, &opt.output, "output", 'o',
+       { NOTMUCH_OPT_KEYWORD_FLAGS, &opt.output, "output", 'o',
          (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
                                  { "threads", OUTPUT_THREADS },
                                  { "messages", OUTPUT_MESSAGES },
@@ -390,6 +390,9 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
        return EXIT_FAILURE;
 
+    if (! opt.output)
+       opt.output = OUTPUT_SUMMARY;
+
     switch (format_sel) {
     case NOTMUCH_FORMAT_TEXT:
        opt.format = sprinter_text_create (config, stdout);
@@ -455,19 +458,17 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
        notmuch_query_set_omit_excluded (opt.query, exclude);
     }
 
-    switch (opt.output) {
-    default:
-    case OUTPUT_SUMMARY:
-    case OUTPUT_THREADS:
+    if (opt.output == OUTPUT_SUMMARY ||
+       opt.output == OUTPUT_THREADS)
        ret = do_search_threads (&opt);
-       break;
-    case OUTPUT_MESSAGES:
-    case OUTPUT_FILES:
+    else if (opt.output == OUTPUT_MESSAGES ||
+            opt.output == OUTPUT_FILES)
        ret = do_search_messages (&opt);
-       break;
-    case OUTPUT_TAGS:
+    else if (opt.output == OUTPUT_TAGS)
        ret = do_search_tags (notmuch, &opt);
-       break;
+    else {
+       fprintf (stderr, "Error: the combination of outputs is not supported.\n");
+       ret = 1;
     }
 
     notmuch_query_destroy (opt.query);