From f1246cd9b2c8bbd398f65a5f93e2c98819fca6dc Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Tue, 21 Apr 2020 18:49:30 +0200
Subject: [PATCH] Integrated help and specify a post help

---
 src/cmd.c         |   6 ++-
 src/main/lkt.c    | 107 ++++++++++++++++++----------------------------
 src/main/lktadm.c |   4 +-
 3 files changed, 48 insertions(+), 69 deletions(-)

diff --git a/src/cmd.c b/src/cmd.c
index 37dc689c..e13fd767 100644
--- a/src/cmd.c
+++ b/src/cmd.c
@@ -19,6 +19,7 @@ lkt_cmd_parse(struct lkt_cmd_opt *opts, int argc, const char **argv, void (*help
     if (argc == 0 || *argv == NULL)
         goto no_args;
 
+    /* Find the command */
     while (it && it->name) {
         is_ok = (! strncasecmp(argv[0], it->name, strlen(argv[0])));
         call[is_ok] = it->call;
@@ -56,7 +57,10 @@ not_found:
     exit(EXIT_FAILURE);
 
 no_args:
-    help();
+    lkt_cmd_help(opts, NULL);
+    if (help)
+        help();
+    exit(EXIT_SUCCESS);
 
 not_exclusive:
     fprintf(stderr, "Failed to determine which option to choose, '%s' is not exclusive\n", argv[0]);
diff --git a/src/main/lkt.c b/src/main/lkt.c
index 8781e704..cb12d22a 100644
--- a/src/main/lkt.c
+++ b/src/main/lkt.c
@@ -35,28 +35,23 @@ noreturn void
 help(void)
 {
     static const char *help_str =
-        "USAGE: lkt [OPTIONS] <COMMAND> [ARGS [...]]\n"
+        "OPTIONS:\n"
+        "  host         named of the lektor's host, can be resolved\n"
+        "  port         port on which lektor is listening\n"
         "\n"
-        "  OPTIONS:\n"
-        "    host         named of the lektor's host, can be resolved.\n"
-        "    port         port on which lektor is listening.\n"
+        "  options most be passed as one word (no spaced), such as the following:\n"
+        "  % lkt host=sakura port=6601 play\n"
         "\n"
-        "    options most be passed as one word (no spaced), such as the following:\n"
-        "    % lkt host=sakura port=6601 play\n"
-        "\n"
-        "  COMMANDS:\n"
-        "    help           display this help message.\n"
-        "    play [?idx]    toggle play/pause state of lektor, may start at a certain index.\n"
-        "    stop           stop the playback but not the window if it exists.\n"
-        "    status         get the status of lektor.\n"
-        "    current        get the currently playing song.\n"
-        "    prev           play previous kara in the queue.\n"
-        "    next           play the next kara in the queue.\n"
-        "    shuffle        shuffle lektor's playlist and play it from the begening.\n"
-        "    queue          tge queue sub command.\n"
-        "    plt            the playlist sub command.\n"
-        "    search         the search sub command.\n"
+        "QUERY:\n"
+        "  A query is passed in argument of a COMMAND and is composed of:\n"
+        "   - The first word must be the type\n"
+        "   - the rest is used for the sqlite regex\n"
+        "  Supported types are: title, [a]ny, source, [auth]or, [lang]uage, type, title\n"
         "\n"
+        "RANGE:\n"
+        "  A range is specified like BEGIN:END which implies from BEGIN to END included\n"
+        "\n";
+    /*
         "  QUEUE COMMANDS:\n"
         "    pos <arg>      the qrgument can be a position or a range.\n"
         "    <count>        prints the next songs in the queue.\n"
@@ -81,26 +76,11 @@ help(void)
         "    count <query>          count the number of songs that matches the query.\n"
         "    queue <query>          prints kara that matches from the queue.\n"
         "\n"
-        "  QUERY:\n"
-        "    A query is passed in argument of a COMMAND and is composed of:\n"
-        "     - The first word must be the type\n"
-        "     - the rest is used for the sqlite regex\n"
-        "    Supported types are: title, [a]ny, source, [auth]or, [lang]uage, type, title\n"
-        "\n"
-        "  RANGE:\n"
-        "    A range is specified like BEGIN:END which implies from BEGIN to END included\n"
-        "\n";
+        */
     write(1, help_str, strlen(help_str));
     exit(EXIT_SUCCESS);
 }
 
-noreturn void
-help__(struct lkt_cmd_args *args)
-{
-    (void) args;
-    help();
-}
-
 static noreturn inline void
 fail(const char *message)
 {
@@ -940,33 +920,31 @@ search_queue__(struct lkt_cmd_args *args)
 /* Parsing stuff. */
 
 static struct lkt_cmd_opt options_queue[] = {
-    { .name = "pos",        .call = queue_pos__     },
-    { .name = "pop",        .call = queue_pop__     },
-    { .name = "add",        .call = queue_add__     },
-    { .name = "seek",       .call = queue_seek__    },
-    { .name = "delete",     .call = queue_delete__  },
-    { .name = "clear",      .call = queue_clear__   },
-    { .name = "crop",       .call = queue_crop__    },
+    { .name = "pos",        .call = queue_pos__,    .help = "Display the content of the queue by a range or a position" },
+    { .name = "pop",        .call = queue_pop__,    .help = "Pop the current kara in the queue"                         },
+    { .name = "add",        .call = queue_add__,    .help = "Add karas to the queue by a query"                         },
+    { .name = "seek",       .call = queue_seek__,   .help = "Seek a kara in the queu by a query"                        },
+    { .name = "delete",     .call = queue_delete__, .help = "Delete a kara by its id in the queue"                      },
+    { .name = "clear",      .call = queue_clear__,  .help = "Clear the queue"                                           },
+    { .name = "crop",       .call = queue_crop__,   .help = "Crop the queue"                                            },
     LKT_OPT_DEFAULT(queue_list__),
 };
 
 static struct lkt_cmd_opt options_plt[] = {
-    { .name = "help",       .call = help__          },
-    { .name = "add",        .call = plt_add__       },
-    { .name = "delete",     .call = plt_delete__    },
-    { .name = "destroy",    .call = plt_destroy__   },
-    { .name = "create",     .call = plt_create__    },
+    { .name = "add",        .call = plt_add__,      .help = "Add something to a playlist"               },
+    { .name = "delete",     .call = plt_delete__,   .help = "Delete karas from a playlist with a query" },
+    { .name = "destroy",    .call = plt_destroy__,  .help = "Delete a playlist"                         },
+    { .name = "create",     .call = plt_create__,   .help = "Create a playlist"                         },
     LKT_OPT_NULL,
 };
 
 static struct lkt_cmd_opt options_search[] = {
-    { .name = "help",       .call = help__          },
-    { .name = "get",        .call = search_get__    },
-    { .name = "add",        .call = search_add__    },
-    { .name = "insert",     .call = search_insert__ },
-    { .name = "plt",        .call = search_plt__    },
-    { .name = "count",      .call = search_count__  },
-    { .name = "queue",      .call = search_queue__  },
+    { .name = "get",        .call = search_get__,       .help = "Prints the results of the query"                                       },
+    { .name = "add",        .call = search_add__,       .help = "Prints and add the results to the queue"                               },
+    { .name = "insert",     .call = search_insert__,    .help = "Prints and inserts at the top of the queue the results of the query"   },
+    { .name = "plt",        .call = search_plt__,       .help = "Search inside a playlist and prints the results"                       },
+    { .name = "count",      .call = search_count__,     .help = "Count the number of karas matching the query and prints it"            },
+    { .name = "queue",      .call = search_queue__,     .help = "Search in the queue and prints the results of the query"               },
     LKT_OPT_NULL,
 };
 
@@ -998,17 +976,16 @@ plt__(struct lkt_cmd_args *args)
 }
 
 static struct lkt_cmd_opt options_[] = {
-    { .name = "help",       .call = help__    },
-    { .name = "current",    .call = current__ },
-    { .name = "play",       .call = play__    },
-    { .name = "next",       .call = next__    },
-    { .name = "previous",   .call = prev__    },
-    { .name = "queue",      .call = queue__   },
-    { .name = "shuffle",    .call = shuffle__ },
-    { .name = "status",     .call = status__  },
-    { .name = "stop",       .call = stop__    },
-    { .name = "plt",        .call = plt__     },
-    { .name = "search",     .call = search__  },
+    { .name = "current",  .call = current__, .help = "Get the current playing song"                         },
+    { .name = "play",     .call = play__,    .help = "Toggle play/pause, may starts at a specified index"   },
+    { .name = "next",     .call = next__,    .help = "Play the next kara in the queue"                      },
+    { .name = "previous", .call = prev__,    .help = "Play the previous kara in the queue"                  },
+    { .name = "queue",    .call = queue__,   .help = "The queue sub-command",          .sub = options_queue },
+    { .name = "shuffle",  .call = shuffle__, .help = "Shuffle the queue"                                    },
+    { .name = "status",   .call = status__,  .help = "Get the status of lektord"                            },
+    { .name = "stop",     .call = stop__,    .help = "Stop playing the queue, won't close lektord's window" },
+    { .name = "plt",      .call = plt__,     .help = "The playlist sub-command",         .sub = options_plt },
+    { .name = "search",   .call = search__,  .help = "The search sub-command",        .sub = options_search },
     LKT_OPT_NULL,
 };
 
diff --git a/src/main/lktadm.c b/src/main/lktadm.c
index 2834dc2e..41da3dd8 100644
--- a/src/main/lktadm.c
+++ b/src/main/lktadm.c
@@ -303,7 +303,5 @@ static struct lkt_cmd_opt options[] = {
 int
 main(int argc, const char **argv)
 {
-    lkt_cmd_help(options, argv[0]);
-    exit(0);
-    lkt_cmd_parse(options, argc - 1, argv + 1, help);
+    lkt_cmd_parse(options, argc - 1, argv + 1, NULL);
 }
-- 
GitLab