aboutsummaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorYaroslav de la Peña Smirnov <yps@yaroslavps.com>2025-09-13 01:37:55 +0300
committerYaroslav de la Peña Smirnov <yps@yaroslavps.com>2025-09-13 01:37:55 +0300
commit312c20f2c868d8fdc014108152cc7782ae6ec90b (patch)
treeab21c2182b739cb91290cd0727b4d6b5c5364305 /cli
parent8c73413201a720dd24451b468155069f954dfd35 (diff)
downloadc-wares-312c20f2c868d8fdc014108152cc7782ae6ec90b.tar.gz
c-wares-312c20f2c868d8fdc014108152cc7782ae6ec90b.zip
clang-format: align with spaces
Also format cli after the changes.
Diffstat (limited to 'cli')
-rw-r--r--cli/cli-example.c6
-rw-r--r--cli/cli-opts.h12
-rw-r--r--cli/cli-test.c48
-rw-r--r--cli/cli.h60
4 files changed, 63 insertions, 63 deletions
diff --git a/cli/cli-example.c b/cli/cli-example.c
index 941ddd6..7ed4562 100644
--- a/cli/cli-example.c
+++ b/cli/cli-example.c
@@ -28,7 +28,7 @@ int calc_distance(const struct cli_cmd *, const struct cli_ctx *)
return CLI_RC_OK;
}
-struct cli_opt *speed_opts[] = {&distance.opt, &time.opt, NULL};
+struct cli_opt *speed_opts[] = {&distance.opt, &time.opt, NULL};
struct cli_opt *distance_opts[] = {&speed.opt, &time.opt, NULL};
const struct cli_cmd my_cmds[] = {
@@ -57,8 +57,8 @@ struct cli my_cli = {
.header = "This is an example CLI program",
.footer = "\nCopyright (c) 2025 Yaroslav de la Peña Smirnov "
"<yps@yaroslavps.com>",
- .cmds = my_cmds,
- .opts = global_opts,
+ .cmds = my_cmds,
+ .opts = global_opts,
};
int main(int argc, char *argv[])
diff --git a/cli/cli-opts.h b/cli/cli-opts.h
index 2266230..d429ca1 100644
--- a/cli/cli-opts.h
+++ b/cli/cli-opts.h
@@ -20,7 +20,7 @@
*/
struct cli_opt_data_size {
struct cli_opt opt;
- uint64_t bytes;
+ uint64_t bytes;
};
#define CLI_OPT_DATA_SIZE(name, sh, ln, dsc) \
@@ -40,8 +40,8 @@ int cli_opt_data_size_set(struct cli_opt *self, const char *val)
struct cli_opt_data_size *opt =
container_of(self, struct cli_opt_data_size, opt);
- char *endptr = NULL;
- errno = 0;
+ char *endptr = NULL;
+ errno = 0;
unsigned long long num = strtoull(val, &endptr, 10);
if (errno != 0)
goto invalid;
@@ -70,9 +70,9 @@ int cli_opt_data_size_set(struct cli_opt *self, const char *val)
return CLI_RC_OK;
invalid:
fprintf(stderr,
- "invalid value \"%s\", expected a number optionally followed "
- "by a multiplicative suffix (K, M, G, T).\n",
- val);
+ "invalid value \"%s\", expected a number optionally followed "
+ "by a multiplicative suffix (K, M, G, T).\n",
+ val);
return CLI_RC_ERR;
}
diff --git a/cli/cli-test.c b/cli/cli-test.c
index 3a89f53..c1d7591 100644
--- a/cli/cli-test.c
+++ b/cli/cli-test.c
@@ -21,12 +21,12 @@ CLI_OPT_FLAG(bopt, 'b', "flag-b", NULL);
CLI_OPT_FLAG(copt, 'c', "flag-c", NULL);
struct expected {
- long i;
+ long i;
unsigned long u;
- const char *s;
- bool a;
- bool b;
- bool c;
+ const char *s;
+ bool a;
+ bool b;
+ bool c;
};
struct cli_opt *options[] = {
@@ -41,12 +41,12 @@ struct cli_opt *options[] = {
static void reset_opts(void)
{
- sopt.value = 0;
- uopt.value = 0;
+ sopt.value = 0;
+ uopt.value = 0;
stropt.value = NULL;
- aopt.value = 0;
- bopt.value = 0;
- copt.value = 0;
+ aopt.value = 0;
+ bopt.value = 0;
+ copt.value = 0;
}
TEST_BEGIN(test_cli_opt_data_size_set)
@@ -54,7 +54,7 @@ TEST_BEGIN(test_cli_opt_data_size_set)
CLI_OPT_DATA_SIZE(opt, 0, NULL, NULL);
struct tcase {
const char *input;
- uint64_t expected_val;
+ uint64_t expected_val;
enum cli_rc expected_rc;
} cases[] = {
{"420", 420, CLI_RC_OK},
@@ -68,7 +68,7 @@ TEST_BEGIN(test_cli_opt_data_size_set)
};
for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
opt.bytes = 0;
- int rc = opt.opt.set(&opt.opt, cases[i].input);
+ int rc = opt.opt.set(&opt.opt, cases[i].input);
asserteq(rc, cases[i].expected_rc);
asserteq(opt.bytes, cases[i].expected_val);
}
@@ -79,10 +79,10 @@ TEST_END
TEST_BEGIN(test_parse_long)
{
struct tcase {
- int argc;
- char *argv[2];
+ int argc;
+ char *argv[2];
struct expected expected_vars;
- enum cli_rc expected_rc;
+ enum cli_rc expected_rc;
} cases[] = {
{
.argc = 2,
@@ -203,10 +203,10 @@ TEST_END
TEST_BEGIN(test_parse_short)
{
struct tcase {
- int argc;
- char *argv[2];
+ int argc;
+ char *argv[2];
struct expected expected_vars;
- enum cli_rc expected_rc;
+ enum cli_rc expected_rc;
} cases[] = {
{
.argc = 2,
@@ -323,10 +323,10 @@ TEST_END
TEST_BEGIN(test_parse_options)
{
struct tcase {
- int argc;
- char *argv[8];
+ int argc;
+ char *argv[8];
struct expected expected_vars;
- enum cli_rc expected_rc;
+ enum cli_rc expected_rc;
} cases[] = {
{
.argc = 5,
@@ -470,6 +470,6 @@ TEST_BEGIN(test_parse_options)
TEST_END
RUN_TESTS(test_cli_opt_data_size_set,
- test_parse_long,
- test_parse_short,
- test_parse_options)
+ test_parse_long,
+ test_parse_short,
+ test_parse_options)
diff --git a/cli/cli.h b/cli/cli.h
index 00848e8..7a7183c 100644
--- a/cli/cli.h
+++ b/cli/cli.h
@@ -46,8 +46,8 @@ enum cli_rc {
*/
struct cli_ctx {
struct cli_opt **opts;
- int argc;
- char *const *argv;
+ int argc;
+ char *const *argv;
};
/**
@@ -75,16 +75,16 @@ enum cli_opt_type : uint8_t {
*/
struct cli_opt {
enum cli_opt_type type;
- char shor;
- const char *lon;
- const char *desc;
+ char shor;
+ const char *lon;
+ const char *desc;
[[gnu::nonnull(1)]]
int (*set)(struct cli_opt *self, const char *val);
};
struct cli_opt_long {
struct cli_opt opt;
- long value;
+ long value;
};
#define CLI_OPT_LONG(name, sh, ln, dsc) \
@@ -103,7 +103,7 @@ 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);
- errno = 0;
+ errno = 0;
opt->value = strtol(val, NULL, 0);
if (errno != 0)
return CLI_RC_BAD_ARGS;
@@ -113,7 +113,7 @@ int cli_opt_long_set(struct cli_opt *self, const char *val)
struct cli_opt_ulong {
struct cli_opt opt;
- long value;
+ long value;
};
#define CLI_OPT_ULONG(name, sh, ln, dsc) \
@@ -132,7 +132,7 @@ 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);
- errno = 0;
+ errno = 0;
opt->value = strtoul(val, NULL, 0);
if (errno != 0)
return CLI_RC_BAD_ARGS;
@@ -142,7 +142,7 @@ int cli_opt_ulong_set(struct cli_opt *self, const char *val)
struct cli_opt_string {
struct cli_opt opt;
- const char *value;
+ const char *value;
};
#define CLI_OPT_STRING(name, sh, ln, dsc) \
@@ -170,7 +170,7 @@ int cli_opt_string_set(struct cli_opt *self, const char *val)
struct cli_opt_flag {
struct cli_opt opt;
- bool value;
+ bool value;
};
#define CLI_OPT_FLAG(name, sh, ln, dsc) \
@@ -188,7 +188,7 @@ struct cli_opt_flag {
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;
+ opt->value = true;
return CLI_RC_OK;
}
@@ -208,10 +208,10 @@ int cli_opt_flag_set(struct cli_opt *self, const char *)
* parsed from the command-line arguments.
*/
struct cli_cmd {
- const char *name;
- const char *desc;
- const char *usage;
- const char *help;
+ const char *name;
+ const char *desc;
+ const char *usage;
+ const char *help;
struct cli_opt **opts;
[[gnu::nonnull]]
int (*run)(const struct cli_cmd *self, const struct cli_ctx *ctx);
@@ -226,11 +226,11 @@ struct cli_cmd {
* @opts: array of global options; also requires a sentinel; can be NULL.
*/
struct cli {
- const char *binary;
- const char *header;
- const char *footer;
+ const char *binary;
+ const char *header;
+ const char *footer;
const struct cli_cmd *cmds;
- struct cli_opt **opts;
+ struct cli_opt **opts;
};
[[gnu::nonnull]]
@@ -265,8 +265,8 @@ static void explain_program(const struct cli *const cli)
printf("%s\n", cli->header);
printf("Usage: %s%s <command> [command options]\n\n",
- cli->opts ? "[global options] " : "",
- cli->binary);
+ cli->opts ? "[global options] " : "",
+ cli->binary);
if (cli->cmds) {
puts("Commands:");
@@ -290,16 +290,16 @@ static void explain_program(const struct cli *const cli)
[[gnu::nonnull]]
static void explain_command(const struct cli *const cli,
- const struct cli_cmd *cmd)
+ const struct cli_cmd *cmd)
{
if (cli->header)
printf("%s\n", cli->header);
printf("Usage: %s%s %s %s\n\n",
- cli->opts ? "[global options] " : "",
- cli->binary,
- cmd->name,
- cmd->usage ?: "[options]");
+ cli->opts ? "[global options] " : "",
+ cli->binary,
+ cmd->name,
+ cmd->usage ?: "[options]");
printf("%s\n\n", cmd->help);
if (cmd->opts) {
@@ -314,7 +314,7 @@ static void explain_command(const struct cli *const cli,
[[gnu::nonnull]]
static int parse_long(struct cli_ctx *ctx)
{
- char *key = ctx->argv[0] + 2;
+ char *key = ctx->argv[0] + 2;
const char *value = NULL;
for (size_t i = 0; i < strlen(key); i++) {
@@ -453,8 +453,8 @@ static int parse_options(struct cli_ctx *ctx)
[[gnu::nonnull]]
static inline int cli_cmd_run(const struct cli *const cli,
- const struct cli_cmd *cmd,
- const struct cli_ctx *global_ctx)
+ const struct cli_cmd *cmd,
+ const struct cli_ctx *global_ctx)
{
struct cli_ctx cmd_ctx = {
.opts = cmd->opts,