Standardized function for creating dict from repeated options

pull/31/head
pukkandan 4 years ago
parent 430c2757ea
commit 45016689fa

@ -435,7 +435,7 @@ Then simply type this
--referer URL Specify a custom referer, use if the video --referer URL Specify a custom referer, use if the video
access is restricted to one domain access is restricted to one domain
--add-header FIELD:VALUE Specify a custom HTTP header and its value, --add-header FIELD:VALUE Specify a custom HTTP header and its value,
separated by a colon ':'. You can use this separated by a colon ":". You can use this
option multiple times option multiple times
--bidi-workaround Work around terminals that lack --bidi-workaround Work around terminals that lack
bidirectional text support. Requires bidiv bidirectional text support. Requires bidiv
@ -554,8 +554,8 @@ Then simply type this
supported: mp4|flv|ogg|webm|mkv|avi) supported: mp4|flv|ogg|webm|mkv|avi)
--postprocessor-args NAME:ARGS Give these arguments to the postprocessors. --postprocessor-args NAME:ARGS Give these arguments to the postprocessors.
Specify the postprocessor/executable name Specify the postprocessor/executable name
and the arguments separated by a colon ':' and the arguments separated by a colon ":"
to give the argument to only the specified to give the argument to the specified
postprocessor/executable. Supported postprocessor/executable. Supported
postprocessors are: SponSkrub, postprocessors are: SponSkrub,
ExtractAudio, VideoRemuxer, VideoConvertor, ExtractAudio, VideoRemuxer, VideoConvertor,
@ -569,7 +569,8 @@ Then simply type this
to different postprocessors. You can also to different postprocessors. You can also
specify "PP+EXE:ARGS" to give the arguments specify "PP+EXE:ARGS" to give the arguments
to the specified executable only when being to the specified executable only when being
used by the specified postprocessor (Alias: used by the specified postprocessor. You
can use this option multiple times (Alias:
--ppa) --ppa)
-k, --keep-video Keep the intermediate video file on disk -k, --keep-video Keep the intermediate video file on disk
after post-processing after post-processing

@ -70,14 +70,7 @@ def _real_main(argv=None):
std_headers['Referer'] = opts.referer std_headers['Referer'] = opts.referer
# Custom HTTP headers # Custom HTTP headers
if opts.headers is not None: std_headers.update(opts.headers)
for h in opts.headers:
if ':' not in h:
parser.error('wrong header formatting, it should be key:value, not "%s"' % h)
key, value = h.split(':', 1)
if opts.verbose:
write_string('[debug] Adding header from command line option %s:%s\n' % (key, value))
std_headers[key] = value
# Dump user agent # Dump user agent
if opts.dump_user_agent: if opts.dump_user_agent:
@ -337,21 +330,9 @@ def _real_main(argv=None):
if opts.external_downloader_args: if opts.external_downloader_args:
external_downloader_args = compat_shlex_split(opts.external_downloader_args) external_downloader_args = compat_shlex_split(opts.external_downloader_args)
postprocessor_args = {} if 'default-compat' in opts.postprocessor_args and 'default' not in opts.postprocessor_args:
if opts.postprocessor_args is not None: opts.postprocessor_args.setdefault('sponskrub', [])
for string in opts.postprocessor_args: opts.postprocessor_args['default'] = opts.postprocessor_args['default-compat']
mobj = re.match(r'(?P<pp>\w+(?:\+\w+)?):(?P<args>.*)$', string)
if mobj is None:
if 'sponskrub' not in postprocessor_args: # for backward compatibility
postprocessor_args['sponskrub'] = []
if opts.verbose:
write_string('[debug] Adding postprocessor args from command line option sponskrub: \n')
pp_key, pp_args = 'default', string
else:
pp_key, pp_args = mobj.group('pp').lower(), mobj.group('args')
if opts.verbose:
write_string('[debug] Adding postprocessor args from command line option %s: %s\n' % (pp_key, pp_args))
postprocessor_args[pp_key] = compat_shlex_split(pp_args)
match_filter = ( match_filter = (
None if opts.match_filter is None None if opts.match_filter is None
@ -486,7 +467,7 @@ def _real_main(argv=None):
'hls_prefer_native': opts.hls_prefer_native, 'hls_prefer_native': opts.hls_prefer_native,
'hls_use_mpegts': opts.hls_use_mpegts, 'hls_use_mpegts': opts.hls_use_mpegts,
'external_downloader_args': external_downloader_args, 'external_downloader_args': external_downloader_args,
'postprocessor_args': postprocessor_args, 'postprocessor_args': opts.postprocessor_args,
'cn_verification_proxy': opts.cn_verification_proxy, 'cn_verification_proxy': opts.cn_verification_proxy,
'geo_verification_proxy': opts.geo_verification_proxy, 'geo_verification_proxy': opts.geo_verification_proxy,
'config_location': opts.config_location, 'config_location': opts.config_location,

@ -104,6 +104,20 @@ def parseOpts(overrideArguments=None):
def _comma_separated_values_options_callback(option, opt_str, value, parser): def _comma_separated_values_options_callback(option, opt_str, value, parser):
setattr(parser.values, option.dest, value.split(',')) setattr(parser.values, option.dest, value.split(','))
def _dict_from_multiple_values_options_callback(
option, opt_str, value, parser, allowed_keys=r'[\w-]+', delimiter=':', default_key=None, process=None):
out_dict = getattr(parser.values, option.dest)
mobj = re.match(r'(?i)(?P<key>%s)%s(?P<val>.*)$' % (allowed_keys, delimiter), value)
if mobj is not None:
key, val = mobj.group('key').lower(), mobj.group('val')
elif default_key is not None:
key, val = default_key, value
else:
raise optparse.OptionValueError(
'wrong %s formatting; it should be %s, not "%s"' % (opt_str, option.metavar, value))
out_dict[key] = process(val) if callable(process) else val
# No need to wrap help messages if we're on a wide console # No need to wrap help messages if we're on a wide console
columns = compat_get_terminal_size().columns columns = compat_get_terminal_size().columns
max_width = columns if columns else 80 max_width = columns if columns else 80
@ -651,8 +665,9 @@ def parseOpts(overrideArguments=None):
) )
workarounds.add_option( workarounds.add_option(
'--add-header', '--add-header',
metavar='FIELD:VALUE', dest='headers', action='append', metavar='FIELD:VALUE', dest='headers', default={}, type='str',
help='Specify a custom HTTP header and its value, separated by a colon \':\'. You can use this option multiple times', action='callback', callback=_dict_from_multiple_values_options_callback,
help='Specify a custom HTTP header and its value, separated by a colon ":". You can use this option multiple times',
) )
workarounds.add_option( workarounds.add_option(
'--bidi-workaround', '--bidi-workaround',
@ -975,18 +990,21 @@ def parseOpts(overrideArguments=None):
metavar='FORMAT', dest='recodevideo', default=None, metavar='FORMAT', dest='recodevideo', default=None,
help='Re-encode the video into another format if re-encoding is necessary (currently supported: mp4|flv|ogg|webm|mkv|avi)') help='Re-encode the video into another format if re-encoding is necessary (currently supported: mp4|flv|ogg|webm|mkv|avi)')
postproc.add_option( postproc.add_option(
'--postprocessor-args', '--ppa', metavar='NAME:ARGS', '--postprocessor-args', '--ppa',
dest='postprocessor_args', action='append', metavar='NAME:ARGS', dest='postprocessor_args', default={}, type='str',
action='callback', callback=_dict_from_multiple_values_options_callback,
callback_kwargs={'default_key': 'default-compat', 'allowed_keys': r'\w+(?:\+\w+)?', 'process': compat_shlex_split},
help=( help=(
'Give these arguments to the postprocessors. ' 'Give these arguments to the postprocessors. '
'Specify the postprocessor/executable name and the arguments separated by a colon ":" ' 'Specify the postprocessor/executable name and the arguments separated by a colon ":" '
'to give the argument to only the specified postprocessor/executable. Supported postprocessors are: ' 'to give the argument to the specified postprocessor/executable. Supported postprocessors are: '
'SponSkrub, ExtractAudio, VideoRemuxer, VideoConvertor, EmbedSubtitle, Metadata, Merger, ' 'SponSkrub, ExtractAudio, VideoRemuxer, VideoConvertor, EmbedSubtitle, Metadata, Merger, '
'FixupStretched, FixupM4a, FixupM3u8, SubtitlesConvertor and EmbedThumbnail. ' 'FixupStretched, FixupM4a, FixupM3u8, SubtitlesConvertor and EmbedThumbnail. '
'The supported executables are: SponSkrub, FFmpeg, FFprobe, avconf, avprobe and AtomicParsley. ' 'The supported executables are: SponSkrub, FFmpeg, FFprobe, avconf, avprobe and AtomicParsley. '
'You can use this option multiple times to give different arguments to different postprocessors. ' 'You can use this option multiple times to give different arguments to different postprocessors. '
'You can also specify "PP+EXE:ARGS" to give the arguments to the specified executable ' 'You can also specify "PP+EXE:ARGS" to give the arguments to the specified executable '
'only when being used by the specified postprocessor (Alias: --ppa)')) 'only when being used by the specified postprocessor. '
'You can use this option multiple times (Alias: --ppa)'))
postproc.add_option( postproc.add_option(
'-k', '--keep-video', '-k', '--keep-video',
action='store_true', dest='keepvideo', default=False, action='store_true', dest='keepvideo', default=False,

Loading…
Cancel
Save