Skip to content

Commit

Permalink
feat: deprecate verbose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Zane committed Oct 8, 2024
1 parent 7f44227 commit b6a80a1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
5 changes: 0 additions & 5 deletions schemachange/config/get_merged_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ def get_yaml_config_kwargs(config_file_path: Optional[Path]) -> dict:

def get_merged_config() -> Union[DeployConfig, RenderConfig]:
cli_kwargs = parse_cli_args(sys.argv[1:])

if "verbose" in cli_kwargs and cli_kwargs["verbose"]:
cli_kwargs["log_level"] = logging.DEBUG
cli_kwargs.pop("verbose")

cli_config_vars = cli_kwargs.pop("config_vars", None)
if cli_config_vars is None:
cli_config_vars = {}
Expand Down
41 changes: 40 additions & 1 deletion schemachange/config/parse_cli_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import argparse
import json
import logging
import warnings
from enum import Enum

import structlog
Expand Down Expand Up @@ -41,6 +43,34 @@ def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, value)


class LogLevel(Enum):
CRITICAL = logging.CRITICAL
ERROR = logging.ERROR
WARNING = logging.WARNING
INFO = logging.INFO
DEBUG = logging.DEBUG


def deprecate_verbose(
args: list[str], verbose: argparse.Action, parsed_args: argparse.Namespace
):
# If --verbose or -v were supplied, warn the user and interpret it as a
for option_string in verbose.option_strings:
if option_string not in args:
continue

warnings.warn(
"Argument %s is deprecated and will be interpreted as a DEBUG log level."
% verbose.option_strings
)

parsed_args.log_level = logging.DEBUG

break

del parsed_args.verbose


def parse_cli_args(args) -> dict:
parser = argparse.ArgumentParser(
prog="schemachange",
Expand Down Expand Up @@ -80,12 +110,19 @@ def parse_cli_args(args) -> dict:
required=False,
)
parent_parser.add_argument(
"--log-level",
type=LogLevel,
action=EnumAction,
help="Set the log level. Defaults to INFO.",
)
verbose = parent_parser.add_argument(
"-v",
"--verbose",
action="store_const",
const=True,
default=None,
help="Display verbose debugging details during execution (the default is False)",
help="DEPRECATED: Use --log-level instead. Display verbose debugging details "
"during execution (the default is False)",
required=False,
)

Expand Down Expand Up @@ -199,6 +236,8 @@ def parse_cli_args(args) -> dict:

parsed_args = parser.parse_args(args)

deprecate_verbose(args=args, verbose=verbose, parsed_args=parsed_args)

parsed_kwargs = parsed_args.__dict__

if "log_level" in parsed_kwargs and isinstance(parsed_kwargs["log_level"], Enum):
Expand Down
15 changes: 15 additions & 0 deletions tests/config/test_parse_cli_args.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

import json
import logging

import pytest

from schemachange.config.parse_cli_args import parse_cli_args

Expand Down Expand Up @@ -121,3 +124,15 @@ def test_parse_args_deploy_flags():
assert parsed_args["subcommand"] == "deploy"
for expected_arg, expected_value in expected.items():
assert parsed_args[expected_arg] == expected_value


def test_parse_args_verbose_deprecation():
args: list[str] = ["--verbose"]
with pytest.warns(UserWarning) as warning:
parsed_args = parse_cli_args(args)
assert parsed_args.get("verbose", None) is None
assert parsed_args["log_level"] is logging.DEBUG
assert (
str(warning[0].message)
== "Argument ['-v', '--verbose'] is deprecated and will be interpreted as a DEBUG log level."
)
2 changes: 2 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@
"schemachange",
"deploy",
*required_args,
"--log-level",
"INFO",
],
{
**default_deploy_config,
Expand Down

0 comments on commit b6a80a1

Please sign in to comment.