From 223821d91c7ace8a4f7f726f1868bf2ae76b90ba Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 13 Feb 2018 03:16:29 +0100 Subject: options: minor cleanup to --no-... handling Most options starting with --no- are automatically translated to --=no. Make the code slightly nicer by using a flag instead of explicitly comparing option types. Also fix an issue that made the option parser print nonsense error messages for if --no-... was used for options which don't support it. --- options/m_config.c | 10 +++++----- options/m_option.c | 6 ++++-- options/m_option.h | 5 +++++ 3 files changed, 14 insertions(+), 7 deletions(-) (limited to 'options') diff --git a/options/m_config.c b/options/m_config.c index dc0fcf9c54..09f14eb2e4 100644 --- a/options/m_config.c +++ b/options/m_config.c @@ -819,16 +819,16 @@ static struct m_config_option *m_config_mogrify_cli_opt(struct m_config *config, return co; // Turn "--no-foo" into "foo" + set *out_negate. - if (!co && bstr_eatstart0(name, "no-")) { - co = m_config_get_co(config, *name); + bstr no_name = *name; + if (!co && bstr_eatstart0(&no_name, "no-")) { + co = m_config_get_co(config, no_name); // Not all choice types have this value - if they don't, then parsing // them will simply result in an error. Good enough. - if (co && co->opt->type != CONF_TYPE_FLAG && - co->opt->type != CONF_TYPE_CHOICE && - co->opt->type != &m_option_type_aspect) + if (!co || !(co->opt->type->flags & M_OPT_TYPE_CHOICE)) return NULL; + *name = no_name; *out_negate = true; return co; } diff --git a/options/m_option.c b/options/m_option.c index e777c5a19e..4a32582873 100644 --- a/options/m_option.c +++ b/options/m_option.c @@ -183,11 +183,11 @@ const m_option_type_t m_option_type_flag = { // need yes or no in config files .name = "Flag", .size = sizeof(int), - .flags = M_OPT_TYPE_OPTIONAL_PARAM, + .flags = M_OPT_TYPE_OPTIONAL_PARAM | M_OPT_TYPE_CHOICE, .parse = parse_flag, .print = print_flag, .copy = copy_opt, - .add = add_flag, + .add = add_flag, .set = flag_set, .get = flag_get, }; @@ -747,6 +747,7 @@ static char *print_choice(const m_option_t *opt, const void *val) const struct m_option_type m_option_type_choice = { .name = "Choice", .size = sizeof(int), + .flags = M_OPT_TYPE_CHOICE, .parse = parse_choice, .print = print_choice, .copy = copy_opt, @@ -1087,6 +1088,7 @@ static int parse_float_aspect(struct mp_log *log, const m_option_t *opt, const m_option_type_t m_option_type_aspect = { .name = "Aspect", .size = sizeof(float), + .flags = M_OPT_TYPE_CHOICE, .parse = parse_float_aspect, .print = print_float, .pretty_print = print_float_f3, diff --git a/options/m_option.h b/options/m_option.h index af082a893d..726c080fa9 100644 --- a/options/m_option.h +++ b/options/m_option.h @@ -443,6 +443,11 @@ char *format_file_size(int64_t size); // options can be used without "=" and value. #define M_OPT_TYPE_OPTIONAL_PARAM (1 << 0) +// Behaves fundamentally like a choice or a superset of it (all allowed string +// values are from a fixed set, although other types of values like numbers +// might be allowed too). E.g. m_option_type_choice and m_option_type_flag. +#define M_OPT_TYPE_CHOICE (1 << 1) + ///////////////////////////// Parser flags ///////////////////////////////// // OptionParserReturn -- cgit v1.2.3