From 491216939bd9824cc0da6d7ca7d7108595cb691d Mon Sep 17 00:00:00 2001 From: Yaroslav de la Peña Smirnov Date: Fri, 27 Feb 2026 22:08:40 +0300 Subject: cli: improve help formatting --- cli/cli.h | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'cli') diff --git a/cli/cli.h b/cli/cli.h index 05f8c6f..6e8aa6d 100644 --- a/cli/cli.h +++ b/cli/cli.h @@ -6,7 +6,7 @@ * * `bin [--key value] [-k value] [--flag] [-f] [--] [non-option-arg]` * - * Copyright (c) 2025 - Yaroslav de la Peña Smirnov + * Copyright (c) 2025-2026 - Yaroslav de la Peña Smirnov */ #ifndef CLI_H #define CLI_H @@ -19,6 +19,13 @@ #include "../utils.h" +/** + * You might want to modify these for your program if you have longer commands + * or keys so that they are prettily printed on the help output. + */ +#define FMT_MAX_CMD_LEN "16" +#define FMT_MAX_KEY_LEN "12" + /** * enum cli_rc - Return codes for CLI parsing results. * @CLI_RC_OK: everything was fine. @@ -99,7 +106,8 @@ struct cli_opt_long { }, \ } -int cli_opt_long_set(struct cli_opt *self, const char *val) +[[maybe_unused]] +static int cli_opt_long_set(struct cli_opt *self, const char *val) { struct cli_opt_long *opt = container_of(self, struct cli_opt_long, opt); @@ -129,7 +137,8 @@ struct cli_opt_ulong { }, \ } -int cli_opt_ulong_set(struct cli_opt *self, const char *val) +[[maybe_unused]] +static int cli_opt_ulong_set(struct cli_opt *self, const char *val) { struct cli_opt_ulong *opt = container_of(self, struct cli_opt_ulong, opt); @@ -159,7 +168,8 @@ struct cli_opt_string { }, \ } -int cli_opt_string_set(struct cli_opt *self, const char *val) +[[maybe_unused]] +static int cli_opt_string_set(struct cli_opt *self, const char *val) { struct cli_opt_string *opt = container_of(self, struct cli_opt_string, opt); @@ -187,7 +197,8 @@ struct cli_opt_flag { }, \ } -int cli_opt_flag_set(struct cli_opt *self, const char *) +[[maybe_unused]] +static int cli_opt_flag_set(struct cli_opt *self, const char *) { struct cli_opt_flag *opt = container_of(self, struct cli_opt_flag, opt); opt->value = true; @@ -248,12 +259,12 @@ static void list_options(struct cli_opt **opts) const struct cli_opt *opt = opts[i]; if (opt->lon) { if (opt->shor) { - printf("\t--%s, -%c\t", opt->lon, opt->shor); + printf(" --%-" FMT_MAX_KEY_LEN "s -%c\t", opt->lon, opt->shor); } else { - printf("\t--%s\t\t", opt->lon); + printf(" --%s\t\t", opt->lon); } } else { - printf("\t-%c\t\t", opt->shor); + printf(" %-" FMT_MAX_KEY_LEN "s -%c\t", "", opt->shor); } printf("%s\n", opt->desc); @@ -273,13 +284,13 @@ static void explain_program(const struct cli *const cli) if (cli->cmds) { puts("Commands:"); for (const struct cli_cmd *cmd = cli->cmds; cmd->name; cmd++) { - printf("\t%s\t\t%s\n", cmd->name, cmd->desc); + printf(" %-" FMT_MAX_CMD_LEN "s\t%s\n", cmd->name, cmd->desc); } } if (cli->opts) { if (cli->cmds) { - puts("Global options:"); + puts("\nGlobal options:"); } else { puts("Options:"); } -- cgit v1.2.3