[BUG] Prevent ytdl-sub args being specified as dl args (#139)

* [BUG] Prevent ytdl-sub args being specified as dl args

* refactor fail
This commit is contained in:
Jesse Bannon 2022-08-01 15:40:52 -07:00 committed by GitHub
parent 2fde8f69c3
commit e3d6245aeb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 3 deletions

View file

@ -7,6 +7,7 @@ from typing import Tuple
from mergedeep import mergedeep
from ytdl_sub.cli.main_args_parser import MainArgs
from ytdl_sub.config.config_file import ConfigOptions
from ytdl_sub.utils.exceptions import InvalidDlArguments
@ -30,10 +31,21 @@ class DownloadArgsParser:
List of extra arguments from argparse
config_options
Configuration portion of config.yaml
Raises
------
InvalidDlArguments
If extra arguments contains a ytdl-sub arg
"""
self._unknown_arguments = extra_arguments
self._config_options = config_options
for arg in extra_arguments:
if arg in MainArgs.all():
raise InvalidDlArguments(
f"'{arg}' is a ytdl-sub argument and must placed behind 'dl'"
)
@property
def _argument_exception(self) -> InvalidDlArguments:
"""

View file

@ -1,7 +1,25 @@
import argparse
from enum import Enum
from typing import List
from ytdl_sub.utils.logger import LoggerLevels
class MainArgs(Enum):
CONFIG = "--config"
DRY_RUN = "--dry-run"
LOG_LEVEL = "--log-level"
@classmethod
def all(cls) -> List[str]:
"""
Returns
-------
List of all args used in main CLI
"""
return list(map(lambda arg: arg.value, cls))
###################################################################################################
# GLOBAL PARSER
parser = argparse.ArgumentParser(
@ -9,19 +27,19 @@ parser = argparse.ArgumentParser(
)
parser.add_argument(
"-c",
"--config",
MainArgs.CONFIG.value,
metavar="CONFIGPATH",
type=str,
help="path to the config yaml, uses config.yaml if not provided",
default="config.yaml",
)
parser.add_argument(
"--dry-run",
MainArgs.DRY_RUN.value,
action="store_true",
help="does not perform any video downloads or writes to output directories",
)
parser.add_argument(
"--log-level",
MainArgs.LOG_LEVEL.value,
metavar="|".join(LoggerLevels.names()),
type=str,
help="level of logs to print to console, defaults to info",

View file

@ -7,6 +7,7 @@ from typing import Optional
import pytest
from ytdl_sub.cli.download_args_parser import DownloadArgsParser
from ytdl_sub.cli.main_args_parser import MainArgs
from ytdl_sub.cli.main_args_parser import parser
from ytdl_sub.config.config_file import ConfigOptions
from ytdl_sub.utils.exceptions import InvalidDlArguments
@ -149,3 +150,14 @@ class TestDownloadArgsParser:
DownloadArgsParser(
extra_arguments=extra_args, config_options=config_options
).to_subscription_dict()
@pytest.mark.parametrize("main_argument", MainArgs.all())
def test_error_uses_main_args(self, main_argument, config_options_generator):
config_options = config_options_generator()
extra_args = _get_extra_arguments(cmd_string=f"dl {main_argument}")
with pytest.raises(
InvalidDlArguments,
match=f"'{main_argument}' is a ytdl-sub argument and must placed behind 'dl'",
):
DownloadArgsParser(extra_arguments=extra_args, config_options=config_options)