diff --git a/inc/lektor/cmd.h b/inc/lektor/cmd.h index 5e0a17b6be084c4f42423c76cff622486e4600d9..905e132c40a2296217eea26d7426721311c15fa9 100644 --- a/inc/lektor/cmd.h +++ b/inc/lektor/cmd.h @@ -17,8 +17,12 @@ typedef void (*lkt_cmd_callback)(struct lkt_cmd_args *); struct lkt_cmd_opt { const char *name; lkt_cmd_callback call; + const char *help; + int 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); diff --git a/src/cmd.c b/src/cmd.c index 5e70e6d1dec9fefc9e7a381d377c51310f2dee5b..11a33c0ca6e9316d0a32e51f8bdee69876394ff6 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -1,11 +1,13 @@ #define _POSIX_C_SOURCE 200809L #include <lektor/cmd.h> +#include <lektor/macro.h> #include <sys/types.h> #include <stdlib.h> #include <strings.h> #include <string.h> #include <stdio.h> +#include <unistd.h> noreturn void lkt_cmd_parse(struct lkt_cmd_opt *opts, int argc, const char **argv, void (*help)(void)) @@ -60,3 +62,26 @@ not_exclusive: fprintf(stderr, "Failed to determine which option to choose, '%s' is not exclusive\n", 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); + } +} diff --git a/src/main/lktadm.c b/src/main/lktadm.c index 20fc668b6c8e599c3f8b65bb08e381525391665a..90a7c7a588993597aafe40c459c7dcf6128d2a5c 100644 --- a/src/main/lktadm.c +++ b/src/main/lktadm.c @@ -278,10 +278,10 @@ download__(struct lkt_cmd_args *args) } static struct lkt_cmd_opt options_init[] = { - { .name = "database", .call = init_database__ }, - { .name = "populate", .call = init_populate__ }, - { .name = "metadata", .call = init_metadata__ }, - { .name = "file", .call = init_file__ }, + { .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" }, LKT_OPT_NULL, }; @@ -292,16 +292,18 @@ init__(struct lkt_cmd_args *args) } static struct lkt_cmd_opt options[] = { - { .name = "init", .call = init__ }, - { .name = "get", .call = get__, }, - { .name = "download", .call = download__ }, - { .name = "cat", .call = cat__, }, - { .name = "conf", .call = conf__, }, + { .name = "init", .call = init__, .help = "The init sub-command", .sub = 1 }, + { .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" }, LKT_OPT_NULL, }; int main(int argc, const char **argv) { + lkt_cmd_help(options, argv[0]); + exit(0); lkt_cmd_parse(options, argc - 1, argv + 1, help); }