summaryrefslogtreecommitdiffstats
path: root/m_option.c
diff options
context:
space:
mode:
authorwm4 <wm4@mplayer2.org>2012-01-06 22:26:21 +0100
committerwm4 <wm4@mplayer2.org>2012-01-18 04:33:30 +0100
commit6340b54d5c39d1ef5647c79a98d219cc5d8228d2 (patch)
treec5dd0845a5da1e0ea84105334a92048a84c9b2d5 /m_option.c
parent55560d62ee85f6336c2c38b1b112a4395e22bc14 (diff)
downloadmpv-6340b54d5c39d1ef5647c79a98d219cc5d8228d2.tar.bz2
mpv-6340b54d5c39d1ef5647c79a98d219cc5d8228d2.tar.xz
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.
Diffstat (limited to 'm_option.c')
-rw-r--r--m_option.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/m_option.c b/m_option.c
index d5113bb33a..9e99ab8755 100644
--- a/m_option.c
+++ b/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;
-
- struct m_opt_choice_alternatives *alt;
- for (alt = opt->priv; alt->name; alt++)
- if (!bstrcasecmp0(param, alt->name))
- break;
+ bool allow_empty = opt->flags & M_OPT_IMPLICIT_DEFAULT;
+ int ret;
+
+ 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)