From 9ca0309ece4668a5bab49e61c04f5eeac6b9d050 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Fri, 1 May 2020 18:33:49 +0200 Subject: [PATCH] Suppress help from executables (there are manpages for that) --- inc/lektor/cmd.h | 6 +-- src/cmd.c | 38 +---------------- src/main/lkt.c | 102 +++++++++++++++++----------------------------- src/main/lktadm.c | 22 +++++----- 4 files changed, 51 insertions(+), 117 deletions(-) diff --git a/inc/lektor/cmd.h b/inc/lektor/cmd.h index de187726..94aaad43 100644 --- a/inc/lektor/cmd.h +++ b/inc/lektor/cmd.h @@ -16,12 +16,8 @@ typedef void (*lkt_cmd_callback)(struct lkt_cmd_args *); struct lkt_cmd_opt { const char *name; lkt_cmd_callback call; - const char *help; - struct lkt_cmd_opt *sub; }; /* Parse the command line with the list of options. No parameter may be NULL. Does not return. */ -noreturn void lkt_cmd_parse(struct lkt_cmd_opt *opts, int argc, const char **argv, void (*help)(void)); - -void lkt_cmd_help(struct lkt_cmd_opt *opts, const char *name); +noreturn void lkt_cmd_parse(struct lkt_cmd_opt *opts, int argc, const char **argv); diff --git a/src/cmd.c b/src/cmd.c index ae46c8b2..b34047ed 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -10,7 +10,7 @@ #include <unistd.h> noreturn void -lkt_cmd_parse(struct lkt_cmd_opt *opts, int argc, const char **argv, void (*help)(void)) +lkt_cmd_parse(struct lkt_cmd_opt *opts, int argc, const char **argv) { int count = 0, is_ok, offset = 0; struct lkt_cmd_opt *it = opts; @@ -57,45 +57,9 @@ not_found: exit(EXIT_FAILURE); no_args: - lkt_cmd_help(opts, NULL); - if (help) - help(); exit(EXIT_SUCCESS); not_exclusive: LOG_ERROR_SCT("COMMAND", "Failed to determine option, '%s' not exclusive", argv[0]); exit(EXIT_FAILURE); } - -void -lkt_cmd_help(struct lkt_cmd_opt *opts, const char *name) -{ - struct lkt_cmd_opt *it = opts; - int offset = 0, max_len = 1; - - /* Get the maximan argument len */ - while (it && it->name) { - int len = strlen(it->name); - max_len = MAX(max_len, len); - it = opts + (++offset); - } - - /* Print the options */ - it = opts; - offset = 0; - printf("COMMAND %s:\n", name); - while (it && it->name) { - printf("\t%*s %s\n", max_len, it->name, it->help); - it = opts + (++offset); - } - - /* Prints the sub commands help */ - it = opts; - offset = 0; - write(1, "\n", sizeof(char)); - while (it && it->name) { - if (it->sub) - lkt_cmd_help(it->sub, it->name); - it = opts + (++offset); - } -} diff --git a/src/main/lkt.c b/src/main/lkt.c index 6ed06b22..f16b2d93 100644 --- a/src/main/lkt.c +++ b/src/main/lkt.c @@ -28,32 +28,6 @@ #define STR_MATCH(str1, str2) (! strcasecmp(str1, str2)) #define STR_NMATCH(str1, str2, n) (! strncasecmp(str1, str2, n)) -/* The help. */ - -noreturn void -help(void) -{ - static const char *help_str = - "OPTIONS:\n" - " host named of the lektor's host, can be resolved\n" - " port port on which lektor is listening\n" - "\n" - " options most be passed as one word (no spaced), such as the following:\n" - " % lkt host=sakura port=6601 play\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); -} - static noreturn inline void fail(const char *message) { @@ -974,40 +948,40 @@ search_queue__(struct lkt_cmd_args *args) /* Parsing stuff. */ static struct lkt_cmd_opt options_queue[] = { - { .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" }, + { .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__ }, LKT_OPT_DEFAULT(queue_list__), }; static struct lkt_cmd_opt options_plt[] = { - { .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" }, + { .name = "add", .call = plt_add__ }, + { .name = "delete", .call = plt_delete__ }, + { .name = "destroy", .call = plt_destroy__ }, + { .name = "create", .call = plt_create__ }, LKT_OPT_NULL, }; static struct lkt_cmd_opt options_search[] = { - { .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" }, + { .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__ }, LKT_OPT_NULL, }; static struct lkt_cmd_opt options_admin[] = { - { .name = "ping", .call = ping__, .help = "Only pings the server" }, - { .name = "kill", .call = kill__, .help = "Kills the lektord server" }, - { .name = "restart", .call = restart__, .help = "Restarts the lektord server" }, - { .name = "rescan", .call = rescan__, .help = "Scan the filesystem and update the database with present files" }, - { .name = "update", .call = update__, .help = "Update the database with files present on Kurisu" }, + { .name = "ping", .call = ping__ }, + { .name = "kill", .call = kill__ }, + { .name = "restart", .call = restart__ }, + { .name = "rescan", .call = rescan__ }, + { .name = "update", .call = update__ }, LKT_OPT_NULL, }; @@ -1017,7 +991,7 @@ admin__(struct lkt_cmd_args *args) if (args->argc == 0) fail("Invalid argument, you must specify a sub command for the admin command"); - lkt_cmd_parse(options_admin, args->argc, args->argv, help); + lkt_cmd_parse(options_admin, args->argc, args->argv); } noreturn void @@ -1026,7 +1000,7 @@ queue__(struct lkt_cmd_args *args) if (args->argc == 0) queue_list__(args); - lkt_cmd_parse(options_queue, args->argc, args->argv, help); + lkt_cmd_parse(options_queue, args->argc, args->argv); } noreturn void @@ -1035,7 +1009,7 @@ search__(struct lkt_cmd_args *args) if (args->argc == 0) fail("Invalid argument, you must specify a sub command for the search command"); - lkt_cmd_parse(options_search, args->argc, args->argv, help); + lkt_cmd_parse(options_search, args->argc, args->argv); } noreturn void @@ -1044,21 +1018,21 @@ plt__(struct lkt_cmd_args *args) if (args->argc == 0) fail("Invalid argument, you must specify a sub command for the playlist command"); - lkt_cmd_parse(options_plt, args->argc, args->argv, help); + lkt_cmd_parse(options_plt, args->argc, args->argv); } static struct lkt_cmd_opt options_[] = { - { .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 }, - { .name = "admin", .call = admin__, .help = "Administration commands", .sub = options_admin }, + { .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 = "admin", .call = admin__ }, LKT_OPT_NULL, }; @@ -1124,5 +1098,5 @@ main(int argc, const char **argv) password = args.pwd; /* Communication with lektor. */ - lkt_cmd_parse(options_, args.argc, args.argv, help); + lkt_cmd_parse(options_, args.argc, args.argv); } diff --git a/src/main/lktadm.c b/src/main/lktadm.c index 352d21ed..13100cd5 100644 --- a/src/main/lktadm.c +++ b/src/main/lktadm.c @@ -229,30 +229,30 @@ download__(struct lkt_cmd_args *args) } static struct lkt_cmd_opt options_init[] = { - { .name = "database", .call = init_database__, .help = "Create an empty database" }, - { .name = "populate", .call = init_populate__, .help = "Populate database from kara on fs" }, - { .name = "metadata", .call = init_metadata__, .help = "Set mdt for all kara in fs" }, - { .name = "file", .call = init_file__, .help = "Set mdt for a kara by its path" }, + { .name = "database", .call = init_database__ }, + { .name = "populate", .call = init_populate__ }, + { .name = "metadata", .call = init_metadata__ }, + { .name = "file", .call = init_file__ }, LKT_OPT_NULL, }; noreturn void init__(struct lkt_cmd_args *args) { - lkt_cmd_parse(options_init, args->argc, args->argv, NULL); + lkt_cmd_parse(options_init, args->argc, args->argv); } static struct lkt_cmd_opt options[] = { - { .name = "init", .call = init__, .help = "The init sub-command", .sub = options_init }, - { .name = "get", .call = get__, .help = "Get the mdt of a kara by its id" }, - { .name = "download", .call = download__, .help = "Download a kara by its id" }, - { .name = "cat", .call = cat__, .help = "Prints the mdt of a kara on the fs" }, - { .name = "conf", .call = conf__, .help = "Prints out the default config" }, + { .name = "init", .call = init__ }, + { .name = "get", .call = get__ }, + { .name = "download", .call = download__ }, + { .name = "cat", .call = cat__ }, + { .name = "conf", .call = conf__ }, LKT_OPT_NULL, }; int main(int argc, const char **argv) { - lkt_cmd_parse(options, argc - 1, argv + 1, NULL); + lkt_cmd_parse(options, argc - 1, argv + 1); } -- GitLab