#include "command.h" #include #include #include #include #include enum fx_status fx_arglist_report_missing_option( const fx_arglist *args, unsigned int opt_id) { struct fx_command_option *opt = NULL; if (opt_id != FX_COMMAND_INVALID_ID) { opt = fx_command_get_option_with_id(args->list_command, opt_id); } if (!opt) { return FX_ERR_INVALID_ARGUMENT; } fx_string *opt_string = fx_string_create(); z__fx_get_option_usage_string(opt, 0, opt_string); fx_stringstream *opt_name = fx_stringstream_create(); int opt_names = 0; if (opt->opt_short_name) { opt_names++; } if (opt->opt_long_name) { opt_names++; } if (opt_names == 2) { fx_stream_write_fmt( opt_name, NULL, "-%c / --%s", opt->opt_short_name, opt->opt_long_name); } else if (opt->opt_short_name) { fx_stream_write_fmt(opt_name, NULL, "-%c", opt->opt_short_name); } else if (opt->opt_long_name) { fx_stream_write_fmt(opt_name, NULL, "--%s", opt->opt_long_name); } fx_err("required option `" F_YELLOW "%s" F_RESET "` was not specified.", fx_stringstream_ptr(opt_name)); fx_i("usage: %s", fx_string_ptr(opt_string)); fx_i("for more information, use `" F_YELLOW "--help" F_RESET "`"); fx_string_unref(opt_string); fx_stringstream_unref(opt_name); return FX_SUCCESS; } enum fx_status fx_arglist_report_unexpected_arg( const fx_arglist *args, const char *value) { fx_string *usage = z__fx_command_default_usage_string( args->list_command, NULL, args); fx_err("unexpected argument '" F_YELLOW "%s" F_RESET "' found.", value); fx_i("usage: %s", fx_string_ptr(usage)); fx_i("for more information, use '" F_YELLOW "--help" F_RESET "'"); return FX_SUCCESS; } enum fx_status fx_arglist_report_invalid_arg_value( const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, const char *value) { struct fx_command_option *opt = NULL; struct fx_command_arg *arg = NULL; if (opt_id != FX_COMMAND_INVALID_ID) { opt = fx_command_get_option_with_id(args->list_command, opt_id); } if (arg_id != FX_COMMAND_INVALID_ID) { arg = opt ? fx_command_option_get_arg_with_id(opt, arg_id) : fx_command_get_arg_with_id(args->list_command, arg_id); } fx_string *usage = z__fx_command_default_usage_string( args->list_command, opt, args); fx_string *opt_string = fx_string_create(); if (opt) { z__fx_get_option_usage_string(opt, 0, opt_string); } else { z__fx_get_arg_usage_string(arg, 0, opt_string); } fx_err("invalid value '" F_YELLOW "%s" F_RESET "' for '" F_YELLOW "%s" F_RESET "'.", value, fx_string_ptr(opt_string)); if (opt) { fx_i("'" F_YELLOW "%s" F_RESET "' accepts the following values for '" F_YELLOW "%s" F_RESET "':", fx_string_ptr(opt_string), arg->arg_name); } else { fx_i("'" F_YELLOW "%s" F_RESET "' accepts the following values:", fx_string_ptr(opt_string)); } for (int i = 0; arg->arg_allowed_values[i]; i++) { fx_printf( " * " F_GREEN "%s" F_RESET "\n", arg->arg_allowed_values[i]); } fx_printf("\n"); fx_i("usage: %s", fx_string_ptr(usage)); fx_i("for more information, use '" F_YELLOW "--help" F_RESET); fx_string_unref(usage); fx_string_unref(opt_string); return FX_SUCCESS; } enum fx_status fx_arglist_report_missing_args( const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, size_t args_supplied) { struct fx_command_option *opt = NULL; struct fx_command_arg *arg = NULL; if (opt_id != FX_COMMAND_INVALID_ID) { opt = fx_command_get_option_with_id(args->list_command, opt_id); assert(opt); } if (arg_id != FX_COMMAND_INVALID_ID) { arg = opt ? fx_command_option_get_arg_with_id(opt, arg_id) : fx_command_get_arg_with_id(args->list_command, arg_id); assert(arg); } fx_string *usage = z__fx_command_default_usage_string( args->list_command, opt, args); fx_string *opt_string = fx_string_create(); if (opt) { z__fx_get_option_usage_string(opt, 0, opt_string); } else { z__fx_get_arg_usage_string(arg, 0, opt_string); } char supplied_arg_str[64]; if (args_supplied == 0) { snprintf( supplied_arg_str, sizeof supplied_arg_str, F_RED_BOLD "none" F_RESET " were provided"); } else if (args_supplied == 1) { snprintf( supplied_arg_str, sizeof supplied_arg_str, "only " F_YELLOW_BOLD "%zu" F_RESET " was provided", args_supplied); } else { snprintf( supplied_arg_str, sizeof supplied_arg_str, "only " F_YELLOW_BOLD "%zu" F_RESET " were provided", args_supplied); } char required_arg_count[64]; switch (arg->arg_nr_values) { case FX_ARG_1_OR_MORE_VALUES: snprintf( required_arg_count, sizeof required_arg_count, "one or more"); break; default: snprintf( required_arg_count, sizeof required_arg_count, "%d", arg->arg_nr_values); } fx_err("argument `" F_YELLOW "%s" F_RESET "` requires " F_GREEN_BOLD "%s" F_RESET " `" F_YELLOW "%s" F_RESET "` value%s, but %s.", fx_string_ptr(opt_string), required_arg_count, arg->arg_name, (arg->arg_nr_values == 1) ? "" : "s", supplied_arg_str); fx_i("usage: %s", fx_string_ptr(usage)); fx_i("for more information, use '" F_YELLOW "--help" F_RESET "'"); fx_string_unref(usage); fx_string_unref(opt_string); return FX_SUCCESS; }