* @brief Creates a new CLI parser instance. * * This function allocates and initializes a new `CliParser` structure, which can be used * to manage command-line options, commands, and their associated handlers. * * @param progName A string representing the name of the program. This name will be used in * usage messages. Must not be NULL. * * @return A pointer to the newly cr
| 52 | *end++ = '\0'; |
| 53 | } |
| 54 | *save = end; |
| 55 | return p; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /* Does option o answer to longOpt or shortOpt? */ |
| 60 | static bool cli_option_is(const CliOption* o, const char* longOpt, char shortOpt) { |
| 61 | return (longOpt && o->longOpt && strcmp(o->longOpt, longOpt) == 0) || |
| 62 | (shortOpt && o->shortOpt == shortOpt); |
| 63 | } |
| 64 | |
| 65 | /* Is name already used by a command, either as its name or as an alias? */ |
| 66 | static bool cli_command_name_taken(const CliParser* parser, const char* name) { |
| 67 | for (size_t i = 0; i < parser->numCommands; i++) { |
| 68 | if (parser->commands[i].name && strcmp(parser->commands[i].name, name) == 0) { |
| 69 | return true; |
| 70 | } |
| 71 | for (size_t j = 0; j < parser->commands[i].numAliases; j++) { |
| 72 | if (parser->commands[i].aliases[j] && strcmp(parser->commands[i].aliases[j], name) == 0) { |
| 73 | return true; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | static void cli_free_option_strings(CliOption* o) { |
| 81 | free((void*)o->longOpt); |
| 82 | free((void*)o->description); |
| 83 | free(o->customErrorMessage); |
| 84 | free(o->validationErrorMessage); |
| 85 | o->longOpt = NULL; |
| 86 | o->description = NULL; |
| 87 | o->customErrorMessage = NULL; |
| 88 | o->validationErrorMessage = NULL; |
| 89 | } |
| 90 | |
| 91 | static void cli_free_command_strings(CliCommand* c) { |
| 92 | free((void*)c->name); |
| 93 | free((void*)c->description); |
| 94 | if (c->aliases) { |
| 95 | for (size_t j = 0; j < c->numAliases; j++) { |
| 96 | free((void*)c->aliases[j]); |
| 97 | } |
| 98 | free((void*)c->aliases); |
| 99 | } |
| 100 | c->name = NULL; |
| 101 | c->description = NULL; |
| 102 | c->aliases = NULL; |
| 103 | c->numAliases = 0; |
| 104 | } |
nothing calls this directly
no test coverage detected