summaryrefslogtreecommitdiffstats
path: root/options/m_option.c
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2015-02-27 18:31:24 +0100
committerwm4 <wm4@nowhere>2015-02-27 18:31:24 +0100
commit1eb533afb379ac9addb9205bf25e32cdc9442e93 (patch)
treee828478551748b858560965838bde4e9162d5b7e /options/m_option.c
parentb143102f6d0ffe383cca346336bcb745ed029a49 (diff)
downloadmpv-1eb533afb379ac9addb9205bf25e32cdc9442e93.tar.bz2
mpv-1eb533afb379ac9addb9205bf25e32cdc9442e93.tar.xz
options: handle choice -> flag fallback automatically
In the past it happened quite often that flag options (yes/no) were changed to choice options (yes/no/some more). The problem with this was that while flag options don't need a parameter, this wasn't the case with choice options. A hack was introduced to compensate for this: setting M_OPT_OPTIONAL_PARAM on the option, and an empty string ("") was added as choice, so that the choice could be used like a flag. So, for example, "--mute" would set the choice "". Fix this by 1. not requiring a parameter if there's a "yes" choice, and 2. redirect an empty parameter to "yes". The effect is that a choice option with the choices ["yes", "no"] is pretty much equivalent to a flag option.
Diffstat (limited to 'options/m_option.c')
-rw-r--r--options/m_option.c20
1 files changed, 17 insertions, 3 deletions
diff --git a/options/m_option.c b/options/m_option.c
index 699ecb4539..b5f9930068 100644
--- a/options/m_option.c
+++ b/options/m_option.c
@@ -74,9 +74,15 @@ char *m_option_strerror(int code)
int m_option_required_params(const m_option_t *opt)
{
- if (((opt->flags & M_OPT_OPTIONAL_PARAM) ||
- (opt->type->flags & M_OPT_TYPE_OPTIONAL_PARAM)))
+ if (opt->type->flags & M_OPT_TYPE_OPTIONAL_PARAM)
return 0;
+ if (opt->type == &m_option_type_choice) {
+ struct m_opt_choice_alternatives *alt;
+ for (alt = opt->priv; alt->name; alt++) {
+ if (strcmp(alt->name, "yes") == 0)
+ return 0;
+ }
+ }
return 1;
}
@@ -535,9 +541,17 @@ static int parse_choice(struct mp_log *log, const struct m_option *opt,
struct bstr name, struct bstr param, void *dst)
{
struct m_opt_choice_alternatives *alt = opt->priv;
- for ( ; alt->name; alt++)
+ for ( ; alt->name; alt++) {
if (!bstrcmp0(param, alt->name))
break;
+ }
+ if (!alt->name && param.len == 0) {
+ // allow flag-style options, e.g. "--mute" implies "--mute=yes"
+ for (alt = opt->priv; alt->name; alt++) {
+ if (!strcmp("yes", alt->name))
+ break;
+ }
+ }
if (!alt->name) {
if (param.len == 0)
return M_OPT_MISSING_PARAM;