/** * silly simple example program */ #include "cli.h" 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"); int calc_speed(const struct cli_cmd *, const struct cli_ctx *) { if (time.value == 0) { fprintf(stderr, "time cannot be zero\n"); return CLI_RC_BAD_ARGS; } 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_cmd *, const struct cli_ctx *) { double distance = speed.value * ((double)time.value / 3600); printf("%lf%s\n", distance, murica.value ? "miles" : "km"); return CLI_RC_OK; } 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[] = { { .name = "speed", .desc = "calculate speed", .help = "Tell me the distance and the time, and I'll tell you your speed", .opts = speed_opts, .run = calc_speed, }, { .name = "distance", .desc = "calculate distance", .help = "Tell me the speed and the time, and I'll tell you your distance", .opts = distance_opts, .run = calc_distance, }, {0}, }; struct cli_opt *global_opts[] = {&murica.opt, NULL}; struct cli my_cli = { .header = "This is an example CLI program", .footer = "\nCopyright (c) 2025 Yaroslav de la Peña Smirnov " "", .cmds = my_cmds, .opts = global_opts, }; int main(int argc, char *argv[]) { return cli_run(&my_cli, argc, argv); }