Closed ipesanz closed 1 year ago
Tests done:
import argparse
from rich.console import Console
from rich import print, box
console = Console()
def custom_error_handler(error):
error_message = f"[bold red]Error:[/bold red] {error}"
console.print(box(error_message, style="bold red"), highlight=False)
parser = argparse.ArgumentParser()
parser.error = custom_error_handler
parser.add_argument("foo", help="foo help")
parser.add_argument("bar", help="bar help")
args = parser.parse_args()
if len(sys.argv) == 1:
raise parser.error('This program needs at least one argument to run. Use -h to check for help')
My initial definition with raise parser.error
work at the beginning, showing the message in plain text and showing the help. I think is a common practice somewhere documented in internet
I didn't came up with a better implementation, as any workaround would imply import directly the class: ArgumentError
or do dirty tricks in the code.
Questions:
argparse
with Rich
, yesdirty
as I'm lacking of general python knowledge
to make a MR
with a feature developed on my own #sorryThat the messages from
raise.error
would be in some red-color using the default RichHelpFormatter
Unfortunately this is not possible in rich-argparse.RichHelpFormatter
does not have access to the parser object that has the .error()
method.
- Is legit the request? Does it make sense? In my opinion, and within the context of enhancing the output of
argparse
withRich
, yes
It is a legit request but unfortunately it is out of the scope of this library. This library only provides help formatter implementation, it does not replace everything in argparse. Until/If argparse changes to make the formatter aware of the parser, this will not be feasible.
- Which should be the appropiate way to achieve this functionality?
The only way possible way is to handle this after argument parsing.
import argparse
import sys
import rich
from rich_argparse import RichHelpFormatter
parser = argparse.ArgumentParser(formatter_class=RichHelpFormatter)
parser.add_argument("-a", "--auto", action="store_true", help="Do something in automatic mode (...)")
# other arguments here
args = parser.parse_args()
if bad_condition_here:
parser.print_usage() # optionally print the short usage (this uses RichHelpFormatter)
rich.print("Other fancy output")
sys.exit(1)
Unrelated to your request but I'll point this out because it might be helpful
required=True
(in parser.add_argument('-a','--auto', required=True, action='store_true', help='Do something in automatic mode (...)')
) and let argparse handle the case of missing arguments.parser.error()
raises SystemExit
internally because it calls parser.exit()
. You are not supposed to raise from it but use it as the final call before your program exits
if len(sys.argv) == 1:
parser.error('This program needs at least one argument to run. Use -h to check for help')
# this line is unreachable
Feature request
I think would be nice if the
parser.error
can be of type:Context:
Desired state: That the messages from
raise.error
would be in some red-color using the default RichHelpFormatter. The traceback exception should be fixed (reason of the request)