[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:
parent
2fde8f69c3
commit
e3d6245aeb
3 changed files with 45 additions and 3 deletions
|
|
@ -7,6 +7,7 @@ from typing import Tuple
|
||||||
|
|
||||||
from mergedeep import mergedeep
|
from mergedeep import mergedeep
|
||||||
|
|
||||||
|
from ytdl_sub.cli.main_args_parser import MainArgs
|
||||||
from ytdl_sub.config.config_file import ConfigOptions
|
from ytdl_sub.config.config_file import ConfigOptions
|
||||||
from ytdl_sub.utils.exceptions import InvalidDlArguments
|
from ytdl_sub.utils.exceptions import InvalidDlArguments
|
||||||
|
|
||||||
|
|
@ -30,10 +31,21 @@ class DownloadArgsParser:
|
||||||
List of extra arguments from argparse
|
List of extra arguments from argparse
|
||||||
config_options
|
config_options
|
||||||
Configuration portion of config.yaml
|
Configuration portion of config.yaml
|
||||||
|
|
||||||
|
Raises
|
||||||
|
------
|
||||||
|
InvalidDlArguments
|
||||||
|
If extra arguments contains a ytdl-sub arg
|
||||||
"""
|
"""
|
||||||
self._unknown_arguments = extra_arguments
|
self._unknown_arguments = extra_arguments
|
||||||
self._config_options = config_options
|
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
|
@property
|
||||||
def _argument_exception(self) -> InvalidDlArguments:
|
def _argument_exception(self) -> InvalidDlArguments:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,25 @@
|
||||||
import argparse
|
import argparse
|
||||||
|
from enum import Enum
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from ytdl_sub.utils.logger import LoggerLevels
|
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
|
# GLOBAL PARSER
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
|
|
@ -9,19 +27,19 @@ parser = argparse.ArgumentParser(
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-c",
|
"-c",
|
||||||
"--config",
|
MainArgs.CONFIG.value,
|
||||||
metavar="CONFIGPATH",
|
metavar="CONFIGPATH",
|
||||||
type=str,
|
type=str,
|
||||||
help="path to the config yaml, uses config.yaml if not provided",
|
help="path to the config yaml, uses config.yaml if not provided",
|
||||||
default="config.yaml",
|
default="config.yaml",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--dry-run",
|
MainArgs.DRY_RUN.value,
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="does not perform any video downloads or writes to output directories",
|
help="does not perform any video downloads or writes to output directories",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--log-level",
|
MainArgs.LOG_LEVEL.value,
|
||||||
metavar="|".join(LoggerLevels.names()),
|
metavar="|".join(LoggerLevels.names()),
|
||||||
type=str,
|
type=str,
|
||||||
help="level of logs to print to console, defaults to info",
|
help="level of logs to print to console, defaults to info",
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ from typing import Optional
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from ytdl_sub.cli.download_args_parser import DownloadArgsParser
|
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.cli.main_args_parser import parser
|
||||||
from ytdl_sub.config.config_file import ConfigOptions
|
from ytdl_sub.config.config_file import ConfigOptions
|
||||||
from ytdl_sub.utils.exceptions import InvalidDlArguments
|
from ytdl_sub.utils.exceptions import InvalidDlArguments
|
||||||
|
|
@ -149,3 +150,14 @@ class TestDownloadArgsParser:
|
||||||
DownloadArgsParser(
|
DownloadArgsParser(
|
||||||
extra_arguments=extra_args, config_options=config_options
|
extra_arguments=extra_args, config_options=config_options
|
||||||
).to_subscription_dict()
|
).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)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue