diff options
| author | Yaroslav de la Peña Smirnov <yps@yaroslavps.com> | 2026-02-27 22:08:40 +0300 |
|---|---|---|
| committer | Yaroslav de la Peña Smirnov <yps@yaroslavps.com> | 2026-02-27 22:08:40 +0300 |
| commit | 491216939bd9824cc0da6d7ca7d7108595cb691d (patch) | |
| tree | 94a8590bd026bbe6b83d51a2d4be83c8249fc003 /cli | |
| parent | 4f6307f106f5e9dbf2937c4a2e5efa10bb8b19ad (diff) | |
| download | c-wares-master.tar.gz c-wares-master.zip | |
Diffstat (limited to 'cli')
| -rw-r--r-- | cli/cli.h | 31 |
1 files changed, 21 insertions, 10 deletions
@@ -6,7 +6,7 @@ * * `bin <command> [--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 @@ -20,6 +20,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. * @CLI_RC_ERR: an error unrelated to cli.h happened. @@ -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:"); } |
