You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Pretty and colorful help - something rich-argparse also does
Pretty and colorful errors for invalid command lines - something rich-argparse also does
Elegant type based API definition, out of scope here - but sadly it inherits the limitation from click that it can't handle argparse's nargs="+" or nargs="*" functionality https://docs.python.org/dev/library/argparse.html#nargs which some of my old code relies on.
Given I am sticking with argparse to maintain an existing API, I'd like to see how far I can push on points (1) and (2) alone. Using rich-argparse out-of-the-box gives an immediate and very easy improvement by adding color the argparse output - thank you!
Is extending rich-argparse to deviate further from the current formatting something you'd consider in-scope (to mimic typer output or otherwise)?
Example help output (point 1)
So rich-argparse makes the help text much nicer, but doesn't change the layout. Typer goes further with ASCII box art.
Example error in command (point 2)
Again rich-argparse makes the help text nicer, but doesn't change the layout. Typer goes further with ASCII box art.
Code for the above example
Demo using argparse only:
fromargparseimportArgumentParserdefsay_hello(name: str="World"):
print(f"Hello {name}")
parser=ArgumentParser()
parser.add_argument("--name", default="World", help="Who or what to greet?")
parsed_args=parser.parse_args()
say_hello(name=parsed_args.name)
Demo using argparse with rich-argparse only:
fromargparseimportArgumentParserfromrich_argparseimportRichHelpFormatterdefsay_hello(name: str="World"):
print(f"Hello {name}")
parser=ArgumentParser(formatter_class=RichHelpFormatter)
parser.add_argument("--name", default="World", help="Who or what to greet?")
parsed_args=parser.parse_args()
say_hello(name=parsed_args.name)
Demo using typer:
importtyperfromtypingimportAnnotateddefsay_hello(
name: Annotated[
str, typer.Option(help="Who or what to greet?", metavar="NAME")
] ="World",
):
print(f"Hello {name}")
if__name__=="__main__":
typer.run(say_hello)
The text was updated successfully, but these errors were encountered:
fromtypingimportAnnotatedfromrich_argparseimportRichHelpFormatterimportyapxdefsay_hello(
name: Annotated[
str, yapx.arg(help="Who or what to greet?", metavar="NAME")
] ="World",
):
print(f"Hello {name}")
yapx.run(say_hello, formatter_class=RichHelpFormatter)
(Minor bug in yapx which I will report - the horizontal rule from yapx is too wide for the terminal)
This is interesting to me as it combines visual improvements over plain argparse (thanks to rich-argparse) with a type annotation based API definition a bit like typer.
Update: You don't even need to use use RichHelpFormatter explicitly, yapx does this automatically if rich-argparse is installed! See fresh2dev/yapx#2
Personally I dislike the style chosen by typer. It makes the output unreadable when you resize your terminal because of the panel border wrapping. This is just my opinion though, if more people start asking for the typer style I might consider adding it.
Here is your original example with a resized terminal (and a slightly longer help message to demonstrate the point):
I have been very impressed with https://typer.tiangolo.com/ as an argparse alternative for three reasons:
rich-argparse
also doesrich-argparse
also doesclick
that it can't handleargparse
'snargs="+"
ornargs="*"
functionality https://docs.python.org/dev/library/argparse.html#nargs which some of my old code relies on.Given I am sticking with
argparse
to maintain an existing API, I'd like to see how far I can push on points (1) and (2) alone. Usingrich-argparse
out-of-the-box gives an immediate and very easy improvement by adding color the argparse output - thank you!Is extending
rich-argparse
to deviate further from the current formatting something you'd consider in-scope (to mimictyper
output or otherwise)?Example help output (point 1)
So
rich-argparse
makes the help text much nicer, but doesn't change the layout. Typer goes further with ASCII box art.Example error in command (point 2)
Again
rich-argparse
makes the help text nicer, but doesn't change the layout. Typer goes further with ASCII box art.Code for the above example
Demo using argparse only:
Demo using argparse with rich-argparse only:
Demo using typer:
The text was updated successfully, but these errors were encountered: