Skip to content

Commit

Permalink
WIP warnings+errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jan 27, 2025
1 parent af0dae8 commit 83086aa
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
1 change: 1 addition & 0 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ANSIColors:
BOLD_GREEN = "\x1b[1;32m"
BOLD_MAGENTA = "\x1b[1;35m"
BOLD_RED = "\x1b[1;31m"
BOLD_YELLOW = "\x1b[1;33m"
BLACK = "\x1b[30m"
GREEN = "\x1b[32m"
GREY = "\x1b[90m"
Expand Down
86 changes: 76 additions & 10 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1270,8 +1270,19 @@ def __call__(self, parser, namespace, values, option_string=None):
raise ArgumentError(self, msg)

if parser_name in self._deprecated:
parser._warning(_("command '%(parser_name)s' is deprecated") %
{'parser_name': parser_name})
from _colorize import get_colors

ansi = get_colors()
bold = ansi.BOLD_MAGENTA
magenta = ansi.MAGENTA
reset = ansi.RESET

parser._warning(
magenta
+ _("command '%(parser_name)s' is deprecated")
% {'parser_name': bold + parser_name + reset + magenta}
+ reset
)

# parse all the remaining options into the namespace
# store any unrecognized options on the object, so that the top
Expand Down Expand Up @@ -2164,8 +2175,19 @@ def consume_optional(start_index):
assert action_tuples
for action, args, option_string in action_tuples:
if action.deprecated and option_string not in warned:
self._warning(_("option '%(option)s' is deprecated") %
{'option': option_string})
from _colorize import get_colors

ansi = get_colors()
bold = ansi.BOLD_MAGENTA
magenta = ansi.MAGENTA
reset = ansi.RESET

self._warning(
magenta
+ _("option '%(option)s' is deprecated")
% {'option': bold + option_string + reset + magenta}
+ reset
)
warned.add(option_string)
take_action(action, args, option_string)
return stop
Expand Down Expand Up @@ -2196,8 +2218,24 @@ def consume_positionals(start_index):
args.remove('--')
start_index += arg_count
if args and action.deprecated and action.dest not in warned:
self._warning(_("argument '%(argument_name)s' is deprecated") %
{'argument_name': action.dest})
from _colorize import get_colors

ansi = get_colors()
bold = ansi.BOLD_MAGENTA
magenta = ansi.MAGENTA
reset = ansi.RESET

self._warning(
magenta
+ _("argument '%(argument_name)s' is deprecated")
% {
'argument_name': bold
+ action.dest
+ reset
+ magenta
}
+ reset
)
warned.add(action.dest)
take_action(action, args)

Expand Down Expand Up @@ -2720,10 +2758,38 @@ def error(self, message):
If you override this in a subclass, it should not return -- it
should either exit or raise an exception.
"""
from _colorize import get_colors

ansi = get_colors()
bold_blue = ansi.BOLD_BLUE
bold_magenta = ansi.BOLD_MAGENTA
bold_red = ansi.BOLD_RED
magenta = ansi.MAGENTA
red = ansi.RED
reset = ansi.RESET

self.print_usage(_sys.stderr)
args = {'prog': self.prog, 'message': message}
self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
prog = f"{bold_magenta}{self.prog}{reset}{bold_red}"
message = f"{reset}{message}{reset}"
args = {'prog': prog, 'message': message}
self.exit(2, _(f'%(prog)s: error: %(message)s\n') % args)

def _warning(self, message):
args = {'prog': self.prog, 'message': message}
self._print_message(_('%(prog)s: warning: %(message)s\n') % args, _sys.stderr)
from _colorize import get_colors

ansi = get_colors()
bold_blue = ansi.BOLD_BLUE
bold_red = ansi.BOLD_RED
bold_yellow = ansi.BOLD_YELLOW
bold_magenta = ansi.BOLD_MAGENTA
magenta = ansi.MAGENTA
red = ansi.RED
yellow = ansi.YELLOW
reset = ansi.RESET

prog = f"{bold_magenta}{self.prog}{reset}{yellow}"
message = f"{reset}{message}"
args = {'prog': prog, 'message': message}
self._print_message(
_(f'%(prog)s: warning: %(message)s\n') % args, _sys.stderr
)

0 comments on commit 83086aa

Please sign in to comment.