@@ -40,33 +40,25 @@ named after the main module:
4040
4141
4242 def greet (
43- name : Annotated[str , typer.Argument(help = " The (last, if --gender is given) name of the person to greet" )] = " " ,
44- gender : Annotated[str , typer.Option(help = " The gender of the person to greet" )] = " " ,
43+ name : Annotated[str , typer.Argument(help = " The (last, if --title is given) name of the person to greet" )] = " " ,
44+ title : Annotated[str , typer.Option(help = " The preferred title of the person to greet" )] = " " ,
4545 knight : Annotated[bool , typer.Option(help = " Whether the person is a knight" )] = False ,
4646 count : Annotated[int , typer.Option(help = " Number of times to greet the person" )] = 1
4747 ):
48- greeting = " Greetings, dear "
49- masculine = gender == " masculine"
50- feminine = gender == " feminine"
51- if gender or knight:
48+ greeting = " Greetings, "
49+ if not name:
50+ if title:
51+ name = title.lower().rstrip(" ." )
52+ else :
53+ name = " friend"
54+ if title or knight:
5255 salutation = " "
53- if knight:
56+ if title:
57+ salutation = title
58+ elif knight:
5459 salutation = " Sir "
55- elif masculine:
56- salutation = " Mr. "
57- elif feminine:
58- salutation = " Ms. "
5960 greeting += salutation
60- if name:
61- greeting += f " { name} ! "
62- else :
63- pronoun = " her" if feminine else " his" if masculine or knight else " its"
64- greeting += f " what's- { pronoun} -name "
65- else :
66- if name:
67- greeting += f " { name} ! "
68- elif not gender:
69- greeting += " friend!"
61+ greeting += f " { name} ! "
7062 for i in range (0 , count):
7163 print (greeting)
7264
@@ -145,12 +137,14 @@ Let's test it:
145137
146138.. code-block :: console
147139
140+ $ greet
141+ Greetings, friend!
148142 $ greet --knight Lancelot
149- Greetings, dear Sir Lancelot!
150- $ greet --gender feminine Parks
151- Greetings, dear Ms. Parks!
152- $ greet --gender masculine
153- Greetings, dear Mr. what's-his-name !
143+ Greetings, Sir Lancelot!
144+ $ greet --title Ms. Parks
145+ Greetings, Ms. Parks!
146+ $ greet --title Mr.
147+ Greetings, Mr. mr !
154148
155149 Since this example uses ``typer ``, you could now also get an overview of the program's usage by calling it with
156150the ``--help `` option, or configure completions via the ``--install-completion `` option.
0 commit comments