]> jfr.im git - yt-dlp.git/commitdiff
[options] Better ambiguous option resolution
authorpukkandan <redacted>
Thu, 10 Feb 2022 15:37:08 +0000 (21:07 +0530)
committerpukkandan <redacted>
Fri, 11 Feb 2022 06:37:03 +0000 (12:07 +0530)
Eg: `--write-auto` no longer results in
> ambiguous option: --write-auto (--write-auto-subs, --write-automatic-subs?)

yt_dlp/options.py

index a6d7c17eb7d0b9c44f145dc5e3f33bdb68312291..d89f74ac5c3f287570c4d3074c451e8ee85be4a7 100644 (file)
@@ -117,6 +117,19 @@ def load_configs():
     return parser, opts, args
 
 
+class _YoutubeDLOptionParser(optparse.OptionParser):
+    # optparse is deprecated since python 3.2. So assume a stable interface even for private methods
+
+    def _match_long_opt(self, opt):
+        """Improve ambigious argument resolution by comparing option objects instead of argument strings"""
+        try:
+            return super()._match_long_opt(opt)
+        except optparse.AmbiguousOptionError as e:
+            if len(set(self._long_opt[p] for p in e.possibilities)) == 1:
+                return e.possibilities[0]
+            raise
+
+
 def create_parser():
     def _format_option_string(option):
         ''' ('-o', '--option') -> -o, --format METAVAR'''
@@ -215,7 +228,7 @@ def _dict_from_options_callback(
         'conflict_handler': 'resolve',
     }
 
-    parser = optparse.OptionParser(**compat_kwargs(kw))
+    parser = _YoutubeDLOptionParser(**compat_kwargs(kw))
 
     general = optparse.OptionGroup(parser, 'General Options')
     general.add_option(