diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..f335c66 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,27 @@ +# Changelog + +## Upcoming + +### Fixed + +- Optly no longer includes ``. It is POSIX-only and MSVC does not + ship it, so the header failed to compile on Windows. Nothing in optly used + it. +- The `#warning` about an old Logcie version is only emitted on GCC and Clang. + `#warning` is not standard C before C23, so on other compilers the fallback + path warned about itself. +- Internal function names no longer contain a double underscore, which is + reserved by C and C++. +- The usage example in the header printed `address->values[1]`. The first + positional value is at index 0. +- Assorted typos in the header docs. + +### Changed + +- Readme is updated, fixed a lot of typos. + +### Known issues + +- `optly_flag_value_string()` returns char *, but the pointer is only writable + when the flag was actually present - a missing flag yields a string literal. + The return type becomes const char * in v3. diff --git a/README.md b/README.md index f211644..72dc50b 100644 --- a/README.md +++ b/README.md @@ -70,11 +70,11 @@ int main(int argc, char **argv) optly_parse_args(argc, argv, &cmd); - printf("threads: %u\n", optly_uint32(&cmd, "threads")); + printf("threads: %u\n", optly_flag_value_uint32(&cmd, "threads")); if (cmd.next_command) { printf("command: %s\n", cmd.next_command->name); - printf("port: %u\n", optly_uint16(cmd.next_command, "port")); + printf("port: %u\n", optly_flag_value_uint16(cmd.next_command, "port")); } } ``` @@ -87,23 +87,38 @@ Run: ## Error handling -This library reports its errors by itself, no complicated error handling required. +`optly_parse_args()` collects every problem it finds instead of stopping at the +first one, and returns them: -By default reporting is disabled, but Optly provides [logcie](https://github.com/Strongleong/logcie/tree/custom-format?tab=readme-ov-file#usage-in-libraries) interface. - -Just put logcie somewhre next to optly in your project, or define logging macros +``` c +OptlyErrors errs = optly_parse_args(argc, argv, &cmd); -```c -#define OPTLY_LOG_BACKEND(level, ...) fprintf(stderr, ##level ": " __VA_ARGS__); +for (size_t i = 0; i < optly_errors_count(&errs); i++) { + OptlyError e = optly_errors_at(&errs, i); + printf("%s%s%s\n", optly_error_message(e.kind), e.arg ? ": " : "", e.arg ? e.arg : ""); +} ``` -You can disable or enable certan logging levels in compiile time: +`optly_error_print(&errs)` does the same thing if the default wording is fine. -```c -#define OPTLY_LOG_DEBUG // define to nothing +**By default optly calls `exit()` when parsing fails.** A CLI parser runs once, +at startup, and bad arguments mean the program should not continue. To take +that decision yourself -- in tests, or when you want to print your own message +-- define: + +``` c +#define OPTLY_NO_EXIT ``` -To control logging in runtime check out [logcie](https://github.com/strongleong/logcie). +Optly also logs each error as it happens. Out of the box that goes to `stderr` +via `fprintf`. If [logcie](https://github.com/Strongleong/logcie) is included +before optly, its module logging is used instead, so your application decides +where optly's output goes. To route it somewhere else entirely, define +`OPTLY_LOG` before including optly: + +``` c +#define OPTLY_LOG(level, ...) my_logger(#level, __VA_ARGS__) +``` ## Flags @@ -139,30 +154,38 @@ Each command may define its own: - subcommands - positional arguments -# Positional Arguments +## Positional Arguments -Example: - - app build file1 file2 - -Access them via: +Declare them on the command that accepts them: ``` c -OptlyPositional *p = optly_get_positional(cmd, "files"); +.positionals = optly_positionals( + optly_positional("files", "Files to build", .min = 1, .max = 0) +) ``` -## Usage Helpers +`min` is how many are required, `max` how many are allowed; `max = 0` means any +number. Everything after a bare `--` is treated as positional, even if it looks +like a flag: -Print global usage: + app build file1 file2 + app build -- --not-a-flag + +Access them via: ``` c -optly_usage(argv[0], cmd.commands, cmd.flags); +OptlyPositional *p = optly_get_positional(&cmd, "files"); + +for (size_t i = 0; i < p->count; i++) { + printf("%s\n", p->values[i]); +} ``` -Print command usage: +## Usage Helpers ``` c -optly_command_usage(argv[0], command); +optly_usage(&cmd); // usage for the whole program +optly_usage(cmd.next_command); // usage for the selected command ``` @@ -188,6 +211,35 @@ If help/version command/flag would be found during parsing usage would be automatically called and `exit(0)` is called. Note that user defined flags with `-h`/`-v` would interfere with generated flags. +Their short forms can be moved with `OPTLY_HELP_SHORT_FLAG` and +`OPTLY_VERSION_SHORT_FLAG`. + +`OPTLY_GEN_VERSION_FLAG` and `OPTLY_GEN_VERSION_COMMAND` add a fourth parameter +to `optly_parse_args()`, the version string: + +``` c +optly_parse_args(argc, argv, &cmd, "1.0.0"); +``` + +## Configuration + +Define these before including optly: + +| Macro | Default | Effect | +| ----- | ------- | ------ | +| `OPTLY_NO_EXIT` | off | Never call `exit()`; return the errors instead | +| `OPTLY_MAX_POSITIONALS` | 64 | Values one positional can hold | +| `OPTLY_MAX_ERRORS` | 32 | Errors collected before further ones are dropped | +| `OPTLY_FLAG_BUFFER_LENGTH` | 256 | Buffer used while formatting help output | +| `OPTLY_LOG` | `fprintf` to stderr | Where optly's own messages go | +| `OPTLY_HELP_SHORT_FLAG` | `"-h"` | Short flag for generated help | +| `OPTLY_VERSION_SHORT_FLAG` | `"-v"` | Short flag for generated version | +| `OPTLYDEF` | empty | Linkage of the public functions | + +## C only + +Optly is a C library. It is not tested as C++ and does not try to compile as +C++ -- use argparse, CLI11 or cxxopts there. ## Design Goals diff --git a/optly.h b/optly.h index b34a27c..f6f0f5b 100644 --- a/optly.h +++ b/optly.h @@ -44,7 +44,7 @@ // Values are unions, so you need to specify member of value with correct type some way { "threads", 't', "Worker threads", false, {.as_uint32 = 4}, OPTLY_TYPE_UINT32 }, - // Flag arrays should alwasy ends with NULL_FLAG. Try to not forget about it :) + // Flag arrays should always ends with NULL_FLAG. Try to not forget about it :) NULL_FLAG, }, @@ -83,7 +83,7 @@ ) ), - // Positioanl arguments can be defined lilke this + // Positional arguments can be defined like this optly_positionals(optly_positional("address", "Address to listen on", .min = 0, .max = 1)), ), } @@ -111,7 +111,7 @@ OptlyPositional *address = optly_get_positional(&cmd, "address"); if (address && address->count == 1) { - printf("Address: %s\n", address->values[1]); + printf("Address: %s\n", address->values[0]); } else { printf("Address: 0.0.0.0\n"); } @@ -221,10 +221,16 @@ The best way to control logging is to use Logcie library (https://github.com/strongleong/logcie). Or have a look at OPTLY_LOG family of macros. - Licese + C only ------ - MIT/Public domain - choose whitchever you prefer + Optly is a C library. It is not tested as C++ and does not try to compli as C++. User argparse, + CLI11 or cxxorts there. + + License + ------ + + MIT/Public domain - choose whichever you prefer */ #ifndef OPTLY_H @@ -472,7 +478,6 @@ OPTLYDEF OptlyPositional *optly_get_positional(OptlyCommand *command, const char #include #include #include -#include // Logcie integration @@ -489,7 +494,7 @@ OPTLYDEF OptlyPositional *optly_get_positional(OptlyCommand *command, const char #else -#if defined(LOGCIE) && LOGCIE_VERSION_NUMBER < 1200 +#if defined(LOGCIE) && LOGCIE_VERSION_NUMBER < 1200 && (defined (__GNUC__) || defined(__clang__)) #warning "Your Logcie version is too old. Falling back to fprintf logging." #endif @@ -535,7 +540,7 @@ OPTLYDEF void optly_error_print(const OptlyErrors *errs) { } } -static void optly__push_error(OptlyErrors *errs, OptlyErrorKind err, const char *arg) { +static void optly_push_error(OptlyErrors *errs, OptlyErrorKind err, const char *arg) { if (!errs) { return; } @@ -560,19 +565,19 @@ OPTLYDEF OptlyError optly_errors_at(const OptlyErrors *errs, size_t i) { return errs->items[i]; } -static bool optly__has_flags(OptlyCommand *cmd) { +static bool optly_has_flags(OptlyCommand *cmd) { return cmd && cmd->flags && !optly_is_flag_null(cmd->flags); } -static bool optly__has_commands(OptlyCommand *cmd) { +static bool optly_has_commands(OptlyCommand *cmd) { return cmd && cmd->commands && !optly_is_command_null(cmd->commands); } -static bool optly__has_positionals(OptlyCommand *cmd) { +static bool optly_has_positionals(OptlyCommand *cmd) { return cmd && cmd->positionals && cmd->positionals->name; } -static void optly__usage_positionals_signature(OptlyPositional *pos) { +static void optly_usage_positionals_signature(OptlyPositional *pos) { if (!pos) return; for (; pos->name; pos++) { @@ -586,24 +591,24 @@ static void optly__usage_positionals_signature(OptlyPositional *pos) { } } -static void optly__usage_signature(OptlyCommand *cmd) { +static void optly_usage_signature(OptlyCommand *cmd) { fprintf(stderr, "Usage: %s", cmd->name); - if (optly__has_flags(cmd)) + if (optly_has_flags(cmd)) fprintf(stderr, " [FLAGS]"); - if (optly__has_positionals(cmd)) - optly__usage_positionals_signature(cmd->positionals); + if (optly_has_positionals(cmd)) + optly_usage_positionals_signature(cmd->positionals); - if (optly__has_commands(cmd)) + if (optly_has_commands(cmd)) fprintf(stderr, " "); fprintf(stderr, "\n"); } -static uint8_t type_name_pad = 8; +static const uint8_t type_name_pad = 8; -static const char *optly__flag_type_name(OptlyFlagType type) { +static const char *optly_flag_type_name(OptlyFlagType type) { switch (type) { case OPTLY_TYPE_BOOL: return ""; case OPTLY_TYPE_CHAR: return ""; @@ -624,7 +629,7 @@ static const char *optly__flag_type_name(OptlyFlagType type) { return ""; } -static size_t optly__flag_print_width(OptlyFlag *flags) { +static size_t optly_flag_print_width(OptlyFlag *flags) { size_t max = 0; for (OptlyFlag *flag = flags; !optly_is_flag_null(flag); flag++) { @@ -646,7 +651,7 @@ static size_t optly__flag_print_width(OptlyFlag *flags) { return max; } -static void optly__print_default_value(OptlyFlag *flag) { +static void optly_print_default_value(OptlyFlag *flag) { if (flag->type == OPTLY_TYPE_BOOL) return; fprintf(stderr, " (default: "); @@ -670,7 +675,7 @@ static void optly__print_default_value(OptlyFlag *flag) { fprintf(stderr, ")"); } -static size_t optly__command_print_width(OptlyCommand *commands) { +static size_t optly_command_print_width(OptlyCommand *commands) { size_t max = 0; for (OptlyCommand *cmd = commands; !optly_is_command_null(cmd); cmd++) { @@ -684,11 +689,11 @@ static size_t optly__command_print_width(OptlyCommand *commands) { return max; } -static void optly__usage_commands_list(OptlyCommand *commands) { +static void optly_usage_commands_list(OptlyCommand *commands) { if (!commands) return; fprintf(stderr, "\nCOMMANDS\n"); - size_t pad = optly__command_print_width(commands); + size_t pad = optly_command_print_width(commands); for (OptlyCommand *cmd = commands; !optly_is_command_null(cmd); cmd++) { fprintf(stderr, " %-*s %s\n", (int)pad, cmd->name, cmd->description ? cmd->description : ""); @@ -703,17 +708,17 @@ static void optly__usage_commands_list(OptlyCommand *commands) { #endif } -static void optly__usage_flags(OptlyFlag *flags) { +static void optly_usage_flags(OptlyFlag *flags) { if (!flags) return; fprintf(stderr, "\nFLAGS\n"); - size_t pad = optly__flag_print_width(flags); + size_t pad = optly_flag_print_width(flags); for (OptlyFlag *flag = flags; !optly_is_flag_null(flag); flag++) { char buf[OPTLY_FLAG_BUFFER_LENGTH + 16]; - // const char *type = optly__flag_type_name(flag->type); + // const char *type = optly_flag_type_name(flag->type); char type_buf[OPTLY_FLAG_BUFFER_LENGTH]; if (flag->type == OPTLY_TYPE_ENUM && flag->value.as_enum) { @@ -731,7 +736,7 @@ static void optly__usage_flags(OptlyFlag *flags) { snprintf(type_buf + offset, sizeof(type_buf) - offset, "]"); } else { - snprintf(type_buf, sizeof(type_buf), "%s", optly__flag_type_name(flag->type)); + snprintf(type_buf, sizeof(type_buf), "%s", optly_flag_type_name(flag->type)); } const char *type = type_buf; @@ -753,7 +758,7 @@ static void optly__usage_flags(OptlyFlag *flags) { fprintf(stderr, " (default: %s)", flag->value.as_enum[0]); } } else if (flag->value.as_string != NULL) { - optly__print_default_value(flag); + optly_print_default_value(flag); } fprintf(stderr, "\n"); @@ -768,7 +773,7 @@ static void optly__usage_flags(OptlyFlag *flags) { #endif } -static void optly__usage_positionals(OptlyPositional *pos) { +static void optly_usage_positionals(OptlyPositional *pos) { if (!pos) return; fprintf(stderr, "\nPOSITIONAL ARGUMENTS\n"); @@ -787,11 +792,11 @@ OPTLYDEF void optly_usage(OptlyCommand *command) { fprintf(stderr, "%s\n\n", command->description); } - optly__usage_signature(command); + optly_usage_signature(command); - optly__usage_commands_list(command->commands); - optly__usage_positionals(command->positionals); - optly__usage_flags(command->flags); + optly_usage_commands_list(command->commands); + optly_usage_positionals(command->positionals); + optly_usage_flags(command->flags); #ifdef OPTLY_GET_HELP_COMMAND fprintf(stderr, "\nRun '%s help ' for more information.\n", command->name); @@ -801,7 +806,7 @@ OPTLYDEF void optly_usage(OptlyCommand *command) { /** * Check if argument matches a flag definition. */ -static bool optly__flag_matches(const char *arg, const OptlyFlag *flag) { +static bool optly_flag_matches(const char *arg, const OptlyFlag *flag) { bool is_short = arg[1] != '-'; return (!is_short && strcmp(arg + 2, flag->fullname) == 0) || @@ -811,9 +816,9 @@ static bool optly__flag_matches(const char *arg, const OptlyFlag *flag) { /** * Find a flag by argument. */ -static OptlyFlag *optly__find_flag(const char *arg, OptlyFlag *flags) { +static OptlyFlag *optly_find_flag(const char *arg, OptlyFlag *flags) { for (OptlyFlag *f = flags; !optly_is_flag_null(f); f++) { - if (optly__flag_matches(arg, f)) { + if (optly_flag_matches(arg, f)) { return f; } } @@ -821,12 +826,12 @@ static OptlyFlag *optly__find_flag(const char *arg, OptlyFlag *flags) { return NULL; } -static void optly__flag_set_value(OptlyFlag *flag, char *value, OptlyErrors *errs) { +static void optly_flag_set_value(OptlyFlag *flag, char *value, OptlyErrors *errs) { assert(flag); if (flag->type != OPTLY_TYPE_BOOL && !value) { OPTLY_LOG(FATAL, "Flag --%s requires value", flag->fullname); - optly__push_error(errs, OPTLY_ERR_MISSING_VALUE, flag->fullname); + optly_push_error(errs, OPTLY_ERR_MISSING_VALUE, flag->fullname); return; } @@ -863,7 +868,7 @@ static void optly__flag_set_value(OptlyFlag *flag, char *value, OptlyErrors *err if (!valid) { OPTLY_LOG(ERROR, "Invalid enum value '%s' for --%s", value, flag->fullname); - optly__push_error(errs, OPTLY_ERR_INVALID_VALUE, value); + optly_push_error(errs, OPTLY_ERR_INVALID_VALUE, value); return; } @@ -874,14 +879,14 @@ static void optly__flag_set_value(OptlyFlag *flag, char *value, OptlyErrors *err if (*end != '\0') { OPTLY_LOG(ERROR, "Argument '%s' is not a number (%s)", flag->fullname, value); - optly__push_error(errs, OPTLY_ERR_INVALID_VALUE, value); + optly_push_error(errs, OPTLY_ERR_INVALID_VALUE, value); return; } flag->present = true; } -inline static bool optly__is_help_flag(char *arg) { +inline static bool optly_is_help_flag(char *arg) { return strcmp(arg, "--help") == 0 || strcmp(arg, OPTLY_HELP_SHORT_FLAG) == 0 || (strlen(arg) > 2 && @@ -890,7 +895,7 @@ inline static bool optly__is_help_flag(char *arg) { strchr(arg, OPTLY_HELP_SHORT_FLAG[1]) != NULL); } -inline static bool optly__is_version_flag(char *arg) { +inline static bool optly_is_version_flag(char *arg) { return strcmp(arg, "--version") == 0 || strcmp(arg, OPTLY_VERSION_SHORT_FLAG) == 0 || (strlen(arg) > 2 && @@ -899,7 +904,7 @@ inline static bool optly__is_version_flag(char *arg) { strchr(arg, OPTLY_VERSION_SHORT_FLAG[1]) != NULL); } -static void optly__parse_batch_flags(char *arg, OptlyFlag *flags, OptlyErrors *errs) { +static void optly_parse_batch_flags(char *arg, OptlyFlag *flags, OptlyErrors *errs) { if (strchr(arg, '=') != NULL) { return; } @@ -908,18 +913,18 @@ static void optly__parse_batch_flags(char *arg, OptlyFlag *flags, OptlyErrors *e char sarg[3]; snprintf(sarg, sizeof(sarg), "-%c", *c); - OptlyFlag *flag = optly__find_flag(sarg, flags); + OptlyFlag *flag = optly_find_flag(sarg, flags); if (!flag) { OPTLY_LOG(WARN, "Unknown short flag: %s", sarg); - optly__push_error(errs, OPTLY_ERR_UNKNOWN_FLAG, sarg); + optly_push_error(errs, OPTLY_ERR_UNKNOWN_FLAG, sarg); continue; } if (flag->type != OPTLY_TYPE_BOOL) { OPTLY_LOG(WARN, "cannot batch non-boolean flags (invalid flag in %s)", sarg); - optly__push_error(errs, OPTLY_ERR_BATCH_NON_BOOL, &flag->shortname); + optly_push_error(errs, OPTLY_ERR_BATCH_NON_BOOL, &flag->shortname); continue; } @@ -930,7 +935,7 @@ static void optly__parse_batch_flags(char *arg, OptlyFlag *flags, OptlyErrors *e return; } -static void optly__parse_long_flags(char ***argv_ptr, int *argc_ptr, OptlyFlag *flags, OptlyErrors *errs) { +static void optly_parse_long_flags(char ***argv_ptr, int *argc_ptr, OptlyFlag *flags, OptlyErrors *errs) { char **argv = *argv_ptr; int argc = *argc_ptr; @@ -961,12 +966,12 @@ static void optly__parse_long_flags(char ***argv_ptr, int *argc_ptr, OptlyFlag * value = eq + 1; } - OptlyFlag *flag = optly__find_flag(arg, flags); + OptlyFlag *flag = optly_find_flag(arg, flags); if (!flag) { OPTLY_LOG(WARN, "Unknown flag: %s", arg); // NOTE: We can't save arg for later because it can point to local tmp (if arg was in form --flag=value) - optly__push_error(errs, OPTLY_ERR_UNKNOWN_FLAG, *argv); + optly_push_error(errs, OPTLY_ERR_UNKNOWN_FLAG, *argv); return; } @@ -977,7 +982,7 @@ static void optly__parse_long_flags(char ***argv_ptr, int *argc_ptr, OptlyFlag * SHIFT_ARG(argv, argc); } - optly__flag_set_value(flag, value, errs); + optly_flag_set_value(flag, value, errs); *argv_ptr = argv; *argc_ptr = argc; @@ -986,7 +991,7 @@ static void optly__parse_long_flags(char ***argv_ptr, int *argc_ptr, OptlyFlag * /** * Parse flags from argv. */ -static void optly__parse_flags(char ***argv_ptr, int *argc_ptr, OptlyFlag *flags, OptlyErrors *errs) { +static void optly_parse_flags(char ***argv_ptr, int *argc_ptr, OptlyFlag *flags, OptlyErrors *errs) { char **argv = *argv_ptr; int argc = *argc_ptr; @@ -999,16 +1004,16 @@ static void optly__parse_flags(char ***argv_ptr, int *argc_ptr, OptlyFlag *flags bool is_batch_short = (arg[0] == '-' && arg[1] != '-' && strlen(arg) > 2) && arg[2] != '='; if (is_batch_short) { - optly__parse_batch_flags(arg, flags, errs); + optly_parse_batch_flags(arg, flags, errs); } else { - optly__parse_long_flags(argv_ptr, argc_ptr, flags, errs); + optly_parse_long_flags(argv_ptr, argc_ptr, flags, errs); } } /** * Parse a command from argv. */ -static OptlyCommand *optly__parse_command(const char *arg, OptlyCommand *commands) { +static OptlyCommand *optly_parse_command(const char *arg, OptlyCommand *commands) { for (OptlyCommand *cmd = commands; !optly_is_command_null(cmd); cmd++) { if (strcmp(arg, cmd->name) == 0) { return cmd; @@ -1018,7 +1023,7 @@ static OptlyCommand *optly__parse_command(const char *arg, OptlyCommand *command return NULL; } -static void optly__push_positional(OptlyCommand *cmd, char *value) { +static void optly_push_positional(OptlyCommand *cmd, char *value) { if (!cmd->positionals) return; size_t pos_count = 0; @@ -1053,16 +1058,16 @@ static void optly__push_positional(OptlyCommand *cmd, char *value) { } } -static void optly__validate_flags(OptlyCommand *cmd, OptlyErrors *errs) { +static void optly_validate_flags(OptlyCommand *cmd, OptlyErrors *errs) { for (OptlyFlag *flag = cmd->flags; !optly_is_flag_null(flag); flag++) { if (flag->required && !flag->present) { OPTLY_LOG(ERROR, "Required flag '--%s' is not present", flag->fullname); - optly__push_error(errs, OPTLY_ERR_MISSING_REQUIRED, flag->fullname); + optly_push_error(errs, OPTLY_ERR_MISSING_REQUIRED, flag->fullname); } } } -static void optly__validate_positionals(OptlyCommand *cmd, OptlyErrors *errs) { +static void optly_validate_positionals(OptlyCommand *cmd, OptlyErrors *errs) { if (!cmd->positionals) { return; } @@ -1073,7 +1078,7 @@ static void optly__validate_positionals(OptlyCommand *cmd, OptlyErrors *errs) { if (pos->max == 0) { if (infinite_found) { OPTLY_LOG(FATAL, "Positional '%s' allows infinite values, but another variadic positional already exists", pos->name); - optly__push_error(errs, OPTLY_ERR_DUPLICATE_VARIADIC, pos->name); + optly_push_error(errs, OPTLY_ERR_DUPLICATE_VARIADIC, pos->name); OPTLY_EXIT(&errs, OPTLY_ERR_DUPLICATE_VARIADIC); } @@ -1082,12 +1087,12 @@ static void optly__validate_positionals(OptlyCommand *cmd, OptlyErrors *errs) { if (pos->count < pos->min) { OPTLY_LOG(ERROR, "Not enough values for positional '%s'", pos->name); - optly__push_error(errs, OPTLY_ERR_POSITIONAL_TOO_FEW, pos->name); + optly_push_error(errs, OPTLY_ERR_POSITIONAL_TOO_FEW, pos->name); } if (pos->max != 0 && pos->count > pos->max) { OPTLY_LOG(ERROR, "Too many values for positional '%s'", pos->name); - optly__push_error(errs, OPTLY_ERR_POSITIONAL_TOO_MANY, pos->name); + optly_push_error(errs, OPTLY_ERR_POSITIONAL_TOO_MANY, pos->name); } } } @@ -1211,21 +1216,21 @@ OPTLYDEF OptlyErrors optly_parse_args(int argc, char *argv[], OptlyCommand *main } #ifdef OPTLY_GEN_HELP_FLAG - if (optly__is_help_flag(arg)) { + if (optly_is_help_flag(arg)) { optly_usage(current_cmd); exit(0); } #endif #ifdef OPTLY_GEN_VERSION_FLAG - if (optly__is_version_flag(arg)) { + if (optly_is_version_flag(arg)) { fprintf(stderr, "%s: %s\n", main_cmd->name, version); exit(0); } #endif if (positional_only) { - optly__push_positional(current_cmd, arg); + optly_push_positional(current_cmd, arg); SHIFT_ARG(argv, argc); continue; } @@ -1238,17 +1243,17 @@ OPTLYDEF OptlyErrors optly_parse_args(int argc, char *argv[], OptlyCommand *main if (arg[0] == '-') { if (current_cmd->flags) { - optly__parse_flags(&argv, &argc, current_cmd->flags, &errs); + optly_parse_flags(&argv, &argc, current_cmd->flags, &errs); } else { // '--flag' argument is positional if no flags defined - optly__push_positional(current_cmd, arg); + optly_push_positional(current_cmd, arg); } SHIFT_ARG(argv, argc); continue; } - OptlyCommand *cmd = optly__parse_command(arg, current_cmd->commands); + OptlyCommand *cmd = optly_parse_command(arg, current_cmd->commands); #ifdef OPTLY_GEN_HELP_COMMAND if (strcmp(arg, "help") == 0) { @@ -1257,10 +1262,10 @@ OPTLYDEF OptlyErrors optly_parse_args(int argc, char *argv[], OptlyCommand *main OptlyCommand *target = current_cmd; if (argc > 0) { - OptlyCommand *tmp = optly__parse_command(*argv, current_cmd->commands); + OptlyCommand *tmp = optly_parse_command(*argv, current_cmd->commands); if (!tmp) { OPTLY_LOG(ERROR, "Unknown command: %s", *argv); - optly__push_error(&errs, OPTLY_ERR_UNKNOWN_COMMAND, *argv); + optly_push_error(&errs, OPTLY_ERR_UNKNOWN_COMMAND, *argv); OPTLY_EXIT(&errs, OPTLY_ERR_UNKNOWN_COMMAND); } @@ -1284,10 +1289,10 @@ OPTLYDEF OptlyErrors optly_parse_args(int argc, char *argv[], OptlyCommand *main current_cmd = current_cmd->next_command; } else { if (current_cmd->positionals) { - optly__push_positional(current_cmd, arg); + optly_push_positional(current_cmd, arg); } else { OPTLY_LOG(ERROR, "Unknown command %s", arg); - optly__push_error(&errs, OPTLY_ERR_UNKNOWN_COMMAND, arg); + optly_push_error(&errs, OPTLY_ERR_UNKNOWN_COMMAND, arg); } } @@ -1295,8 +1300,8 @@ OPTLYDEF OptlyErrors optly_parse_args(int argc, char *argv[], OptlyCommand *main } for (OptlyCommand *cmd = main_cmd; cmd; cmd = cmd->next_command) { - optly__validate_flags(cmd, &errs); - optly__validate_positionals(cmd, &errs); + optly_validate_flags(cmd, &errs); + optly_validate_positionals(cmd, &errs); } current_cmd->next_command = NULL;