@@ -40,33 +40,22 @@ named after the main module:
40
40
41
41
42
42
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 ,
46
46
count : Annotated[int , typer.Option(help = " Number of times to greet the person" )] = 1
47
47
):
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(" ." )
62
54
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} ! "
70
59
for i in range (0 , count):
71
60
print (greeting)
72
61
@@ -145,12 +134,14 @@ Let's test it:
145
134
146
135
.. code-block :: console
147
136
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!
154
145
155
146
Since this example uses ``typer ``, you could now also get an overview of the program's usage by calling it with
156
147
the ``--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
160
151
161
152
.. code-block :: console
162
153
163
- $ pipx run --spec . greet --knight
154
+ $ pipx run --spec . greet --doctor
164
155
165
156
This syntax is a bit impractical, however; as the name of the entry point we defined above does not match the package name,
166
157
we 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:
179
170
180
171
.. code-block :: console
181
172
182
- $ pipx run . --knight
173
+ $ pipx run . --doctor
183
174
184
175
Conclusion
185
176
==========
0 commit comments