diff options
author | Yaroslav de la Peña Smirnov <yps@yaroslavps.com> | 2025-09-12 23:43:49 +0300 |
---|---|---|
committer | Yaroslav de la Peña Smirnov <yps@yaroslavps.com> | 2025-09-12 23:43:49 +0300 |
commit | 0a8ee7a194e598d9c277b019503f7119b32bd17f (patch) | |
tree | 671a416c1fedec1235fa0fd8dce7fa6ed91095c3 /cli/cli-example.c | |
parent | 5a2a20eda7ca20bca2285e4940c4d1140301e8df (diff) | |
download | c-wares-0a8ee7a194e598d9c277b019503f7119b32bd17f.tar.gz c-wares-0a8ee7a194e598d9c277b019503f7119b32bd17f.zip |
cli: make it possible to extend option types
Make it possible to extend options with custom value parsers by embeding
`struct cli_opt` into a custom struct and setting the `set()` "method".
Diffstat (limited to 'cli/cli-example.c')
-rw-r--r-- | cli/cli-example.c | 75 |
1 files changed, 16 insertions, 59 deletions
diff --git a/cli/cli-example.c b/cli/cli-example.c index 8149be0..941ddd6 100644 --- a/cli/cli-example.c +++ b/cli/cli-example.c @@ -3,67 +3,33 @@ */ #include "cli.h" -long distance; -unsigned long time; -long speed; +CLI_OPT_LONG(distance, 'd', "distance", "the distance (km/miles)"); +CLI_OPT_LONG(speed, 's', "speed", "the speed ((km/miles)ph)"); +CLI_OPT_LONG(time, 't', "time", "the time (seconds)"); +CLI_OPT_FLAG(murica, 'M', "murica", "Use American™ measurements"); -bool murica; - -int calc_speed(const struct cli_ctx *ctx) +int calc_speed(const struct cli_cmd *, const struct cli_ctx *) { - if (time == 0) { + if (time.value == 0) { fprintf(stderr, "time cannot be zero\n"); return CLI_RC_BAD_ARGS; } - double speed = (double)distance / ((double)time / 3600); - printf("%lf%s\n", speed, murica ? "mph" : "km/h"); + double speed = (double)distance.value / ((double)time.value / 3600); + printf("%lf%s\n", speed, murica.value ? "mph" : "km/h"); return CLI_RC_OK; } -int calc_distance(const struct cli_ctx *ctx) +int calc_distance(const struct cli_cmd *, const struct cli_ctx *) { - double distance = speed * ((double)time / 3600); - printf("%lf%s\n", distance, murica ? "miles" : "km"); + double distance = speed.value * ((double)time.value / 3600); + printf("%lf%s\n", distance, murica.value ? "miles" : "km"); return CLI_RC_OK; } -const struct cli_opt speed_opts[] = { - { - .type = CLI_OT_INT, - .shor = 'd', - .lon = "distance", - .desc = "the distance (km/miles)", - .value.i = &distance, - }, - { - .type = CLI_OT_UINT, - .shor = 't', - .lon = "time", - .desc = "the time (seconds)", - .value.u = &time, - }, - {0}, -}; - -const struct cli_opt distance_opts[] = { - { - .type = CLI_OT_INT, - .shor = 's', - .lon = "speed", - .desc = "the speed ((km/miles)ph)", - .value.i = &speed, - }, - { - .type = CLI_OT_UINT, - .shor = 't', - .lon = "time", - .desc = "the time (seconds)", - .value.u = &time, - }, - {0}, -}; +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[] = { { @@ -72,7 +38,7 @@ const struct cli_cmd my_cmds[] = { .help = "Tell me the distance and the time, and I'll tell you your speed", .opts = speed_opts, - .func = calc_speed, + .run = calc_speed, }, { .name = "distance", @@ -80,21 +46,12 @@ const struct cli_cmd my_cmds[] = { .help = "Tell me the speed and the time, and I'll tell you your distance", .opts = distance_opts, - .func = calc_distance, + .run = calc_distance, }, {0}, }; -struct cli_opt global_opts[] = { - { - .type = CLI_OT_FLAG, - .shor = 'M', - .lon = "murica", - .desc = "Use American™ measurements", - .value.f = &murica, - }, - {0}, -}; +struct cli_opt *global_opts[] = {&murica.opt, NULL}; struct cli my_cli = { .header = "This is an example CLI program", |