Skip to content
Extraits de code Groupes Projets
Vérifiée Valider b80455f2 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

Embed help inside the lkt_cmd_opt

parent 0fa2e54c
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!64Resolve "Installation"
......@@ -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);
#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);
}
}
......@@ -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);
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter