options: minor cleanup to --no-... handling

Most options starting with --no-<name> are automatically translated to
--<name>=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.
This commit is contained in:
wm4 2018-02-13 03:16:29 +01:00 committed by Kevin Mitchell
parent d7c38a0b23
commit 223821d91c
3 changed files with 14 additions and 7 deletions

View File

@ -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;
}

View File

@ -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,

View File

@ -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