mirror of
https://github.com/mpv-player/mpv
synced 2025-02-07 07:31:48 +00:00
options: allow choice options without parameter
If an m_option_type_choice option is declared with M_OPT_IMPLICIT_DEFAULT in its flags, it doesn't require a parameter. For example, if --opt is such an option, it can be invoked as "--opt=val", "-opt", or "--opt". The last two will set the option to the first choice the option declares. Note that "-opt val" (using the old option syntax) is not allowed in this case, as it would be ambiguous. Normal option parsing should be unaffected.
This commit is contained in:
parent
55560d62ee
commit
6340b54d5c
21
m_option.c
21
m_option.c
@ -281,13 +281,20 @@ const struct m_option_type m_option_type_intpair = {
|
||||
static int parse_choice(const struct m_option *opt, struct bstr name,
|
||||
struct bstr param, bool ambiguous_param, void *dst)
|
||||
{
|
||||
if (param.len == 0)
|
||||
return M_OPT_MISSING_PARAM;
|
||||
bool allow_empty = opt->flags & M_OPT_IMPLICIT_DEFAULT;
|
||||
int ret;
|
||||
|
||||
struct m_opt_choice_alternatives *alt;
|
||||
for (alt = opt->priv; alt->name; alt++)
|
||||
if (!bstrcasecmp0(param, alt->name))
|
||||
break;
|
||||
struct m_opt_choice_alternatives *alt = opt->priv;
|
||||
if (param.len == 0 || (ambiguous_param && allow_empty)) {
|
||||
if (!allow_empty)
|
||||
return M_OPT_MISSING_PARAM;
|
||||
ret = 0;
|
||||
} else {
|
||||
for ( ; alt->name; alt++)
|
||||
if (!bstrcasecmp0(param, alt->name))
|
||||
break;
|
||||
ret = 1;
|
||||
}
|
||||
if (!alt->name) {
|
||||
mp_msg(MSGT_CFGPARSER, MSGL_ERR,
|
||||
"Invalid value for option %.*s: %.*s\n",
|
||||
@ -301,7 +308,7 @@ static int parse_choice(const struct m_option *opt, struct bstr name,
|
||||
if (dst)
|
||||
*(int *)dst = alt->value;
|
||||
|
||||
return 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static char *print_choice(const m_option_t *opt, const void *val)
|
||||
|
@ -338,6 +338,11 @@ struct m_option {
|
||||
// The option should be set during command line pre-parsing
|
||||
#define M_OPT_PRE_PARSE (1 << 6)
|
||||
|
||||
// Accept an option without parameter, even if the option type normally requires
|
||||
// a parameter. The option value will be set to a default value.
|
||||
// For m_option_type_choice, the first listed choice will be used.
|
||||
#define M_OPT_IMPLICIT_DEFAULT (1 << 7)
|
||||
|
||||
// These are kept for compatibility with older code.
|
||||
#define CONF_MIN M_OPT_MIN
|
||||
#define CONF_MAX M_OPT_MAX
|
||||
|
Loading…
Reference in New Issue
Block a user