]> git.notmuchmail.org Git - notmuch/commitdiff
cli: refactor notmuch_help_command
authorDavid Bremner <david@tethera.net>
Sun, 5 Apr 2015 14:59:18 +0000 (23:59 +0900)
committerDavid Bremner <david@tethera.net>
Mon, 1 Jun 2015 05:32:54 +0000 (07:32 +0200)
Create a new private entry point _help_for so that we can call help
without simulating a command line invokation to set up the arguments.

notmuch.c

index f81922ed090dcb12d27d3cda434e2493f60393c7..12f3e7986a0ee57da1bfb637147e26c61a9819b6 100644 (file)
--- a/notmuch.c
+++ b/notmuch.c
@@ -177,21 +177,19 @@ exec_man (const char *page)
 }
 
 static int
 }
 
 static int
-notmuch_help_command (unused (notmuch_config_t * config), int argc, char *argv[])
+_help_for (const char *topic_name)
 {
     command_t *command;
     help_topic_t *topic;
     unsigned int i;
 
 {
     command_t *command;
     help_topic_t *topic;
     unsigned int i;
 
-    argc--; argv++; /* Ignore "help" */
-
-    if (argc == 0) {
+    if (!topic_name) {
        printf ("The notmuch mail system.\n\n");
        usage (stdout);
        return EXIT_SUCCESS;
     }
 
        printf ("The notmuch mail system.\n\n");
        usage (stdout);
        return EXIT_SUCCESS;
     }
 
-    if (strcmp (argv[0], "help") == 0) {
+    if (strcmp (topic_name, "help") == 0) {
        printf ("The notmuch help system.\n\n"
                "\tNotmuch uses the man command to display help. In case\n"
                "\tof difficulties check that MANPATH includes the pages\n"
        printf ("The notmuch help system.\n\n"
                "\tNotmuch uses the man command to display help. In case\n"
                "\tof difficulties check that MANPATH includes the pages\n"
@@ -200,7 +198,7 @@ notmuch_help_command (unused (notmuch_config_t * config), int argc, char *argv[]
        return EXIT_SUCCESS;
     }
 
        return EXIT_SUCCESS;
     }
 
-    command = find_command (argv[0]);
+    command = find_command (topic_name);
     if (command) {
        char *page = talloc_asprintf (NULL, "notmuch-%s", command->name);
        exec_man (page);
     if (command) {
        char *page = talloc_asprintf (NULL, "notmuch-%s", command->name);
        exec_man (page);
@@ -208,7 +206,7 @@ notmuch_help_command (unused (notmuch_config_t * config), int argc, char *argv[]
 
     for (i = 0; i < ARRAY_SIZE (help_topics); i++) {
        topic = &help_topics[i];
 
     for (i = 0; i < ARRAY_SIZE (help_topics); i++) {
        topic = &help_topics[i];
-       if (strcmp (argv[0], topic->name) == 0) {
+       if (strcmp (topic_name, topic->name) == 0) {
            char *page = talloc_asprintf (NULL, "notmuch-%s", topic->name);
            exec_man (page);
        }
            char *page = talloc_asprintf (NULL, "notmuch-%s", topic->name);
            exec_man (page);
        }
@@ -216,10 +214,22 @@ notmuch_help_command (unused (notmuch_config_t * config), int argc, char *argv[]
 
     fprintf (stderr,
             "\nSorry, %s is not a known command. There's not much I can do to help.\n\n",
 
     fprintf (stderr,
             "\nSorry, %s is not a known command. There's not much I can do to help.\n\n",
-            argv[0]);
+            topic_name);
     return EXIT_FAILURE;
 }
 
     return EXIT_FAILURE;
 }
 
+static int
+notmuch_help_command (unused (notmuch_config_t * config), int argc, char *argv[])
+{
+    argc--; argv++; /* Ignore "help" */
+
+    if (argc == 0) {
+       return _help_for (NULL);
+    }
+
+    return _help_for (argv[0]);
+}
+
 /* Handle the case of "notmuch" being invoked with no command
  * argument. For now we just call notmuch_setup_command, but we plan
  * to be more clever about this in the future.
 /* Handle the case of "notmuch" being invoked with no command
  * argument. For now we just call notmuch_setup_command, but we plan
  * to be more clever about this in the future.