aboutsummaryrefslogtreecommitdiffstats
path: root/yt_dlp/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'yt_dlp/__init__.py')
-rw-r--r--yt_dlp/__init__.py40
1 files changed, 24 insertions, 16 deletions
diff --git a/yt_dlp/__init__.py b/yt_dlp/__init__.py
index 8f890b34a..81b1716df 100644
--- a/yt_dlp/__init__.py
+++ b/yt_dlp/__init__.py
@@ -4,6 +4,7 @@ f'You are using an unsupported version of Python. Only Python versions 3.6 and a
__license__ = 'Public Domain'
import itertools
+import optparse
import os
import re
import sys
@@ -45,11 +46,18 @@ from .utils import (
setproctitle,
std_headers,
traverse_obj,
+ variadic,
write_string,
)
from .YoutubeDL import YoutubeDL
+def _exit(status=0, *args):
+ for msg in args:
+ sys.stderr.write(msg)
+ raise SystemExit(status)
+
+
def get_urls(urls, batchfile, verbose):
# Batch file verification
batch_urls = []
@@ -66,7 +74,7 @@ def get_urls(urls, batchfile, verbose):
if verbose:
write_string('[debug] Batch file urls: ' + repr(batch_urls) + '\n')
except OSError:
- sys.exit('ERROR: batch file %s could not be read' % batchfile)
+ _exit(f'ERROR: batch file {batchfile} could not be read')
_enc = preferredencoding()
return [
url.strip().decode(_enc, 'ignore') if isinstance(url, bytes) else url.strip()
@@ -810,10 +818,10 @@ def _real_main(argv=None):
if opts.dump_user_agent:
ua = traverse_obj(opts.headers, 'User-Agent', casesense=False, default=std_headers['User-Agent'])
write_string(f'{ua}\n', out=sys.stdout)
- sys.exit(0)
+ return
if print_extractor_information(opts, all_urls):
- sys.exit(0)
+ return
with YoutubeDL(ydl_opts) as ydl:
actual_use = all_urls or opts.load_info_filename
@@ -827,13 +835,13 @@ def _real_main(argv=None):
# If updater returns True, exit. Required for windows
if run_update(ydl):
if actual_use:
- sys.exit('ERROR: The program must exit for the update to complete')
- sys.exit()
+ return 100, 'ERROR: The program must exit for the update to complete'
+ return
# Maybe do nothing
if not actual_use:
if opts.update_self or opts.rm_cachedir:
- sys.exit()
+ return
ydl.warn_if_short_id(sys.argv[1:] if argv is None else argv)
parser.error(
@@ -842,30 +850,30 @@ def _real_main(argv=None):
try:
if opts.load_info_filename is not None:
- retcode = ydl.download_with_info_file(expand_path(opts.load_info_filename))
+ return ydl.download_with_info_file(expand_path(opts.load_info_filename))
else:
- retcode = ydl.download(all_urls)
+ return ydl.download(all_urls)
except DownloadCancelled:
ydl.to_screen('Aborting remaining downloads')
- retcode = 101
-
- sys.exit(retcode)
+ return 101
def main(argv=None):
try:
- _real_main(argv)
+ _exit(*variadic(_real_main(argv)))
except DownloadError:
- sys.exit(1)
+ _exit(1)
except SameFileError as e:
- sys.exit(f'ERROR: {e}')
+ _exit(f'ERROR: {e}')
except KeyboardInterrupt:
- sys.exit('\nERROR: Interrupted by user')
+ _exit('\nERROR: Interrupted by user')
except BrokenPipeError as e:
# https://docs.python.org/3/library/signal.html#note-on-sigpipe
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
- sys.exit(f'\nERROR: {e}')
+ _exit(f'\nERROR: {e}')
+ except optparse.OptParseError as e:
+ _exit(2, f'\n{e}')
from .extractor import gen_extractors, list_extractors