@@ -40,33 +40,22 @@ 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" )] = " " ,
45- knight : Annotated[bool , typer.Option(help = " Whether the person is a knight " )] = False ,
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" )] = " " ,
45+ doctor : Annotated[bool , typer.Option(help = " Whether the person is a doctor (MD or PhD) " )] = 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:
52- salutation = " "
53- if knight:
54- salutation = " Sir "
55- elif masculine:
56- salutation = " Mr. "
57- elif feminine:
58- salutation = " Ms. "
59- greeting += salutation
60- if name:
61- greeting += f " { name} ! "
48+ greeting = " Greetings, "
49+ if doctor and not title:
50+ title = " Dr."
51+ if not name:
52+ if title:
53+ name = title.lower().rstrip(" ." )
6254 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!"
55+ name = " friend"
56+ if title:
57+ greeting += f " { title} "
58+ greeting += f " { name} ! "
7059 for i in range (0 , count):
7160 print (greeting)
7261
@@ -145,12 +134,14 @@ Let's test it:
145134
146135.. code-block :: console
147136
148- $ 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!
137+ $ greet
138+ Greetings, friend!
139+ $ greet --doctor Brennan
140+ Greetings, Dr. Brennan!
141+ $ greet --title Ms. Parks
142+ Greetings, Ms. Parks!
143+ $ greet --title Mr.
144+ Greetings, Mr. mr!
154145
155146 Since this example uses ``typer ``, you could now also get an overview of the program's usage by calling it with
156147the ``--help `` option, or configure completions via the ``--install-completion `` option.
@@ -160,7 +151,7 @@ To just run the program without installing it permanently, use ``pipx run``, whi
160151
161152.. code-block :: console
162153
163- $ pipx run --spec . greet --knight
154+ $ pipx run --spec . greet --doctor
164155
165156 This syntax is a bit impractical, however; as the name of the entry point we defined above does not match the package name,
166157we need to state explicitly which executable script to run (even though there is only on in existence).
@@ -179,7 +170,7 @@ default one and run it, which makes this command possible:
179170
180171.. code-block :: console
181172
182- $ pipx run . --knight
173+ $ pipx run . --doctor
183174
184175 Conclusion
185176==========
0 commit comments