-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter22.html
More file actions
194 lines (173 loc) · 6.96 KB
/
chapter22.html
File metadata and controls
194 lines (173 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="description"
content="Chapter 22 of Python Zero to Hero: build robust command-line interfaces with argparse, Click and Typer."/>
<meta name="keywords"
content="Python, CLI, command line, argparse, click, typer, console scripts, CLI tools"/>
<meta name="author" content="Luca Bocaletto"/>
<title>Chapter 22 – Building CLI Tools with argparse, Click & Typer | Python Zero to Hero</title>
<!-- Bootstrap 5 CSS -->
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-YSa3P1QY0VSei3nHevwltF1Jxv2pT3z5z0R6x35o1XQdYzdgQc8sYC+bS9v+g3Tc"
crossorigin="anonymous"
/>
<!-- Highlight.js CSS -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css"
integrity="sha512-94jnYVzi0AuOmT1JrLJiqxGWM1ZwVz4i1oF6XD2fXZj2lq+OJ9GSeUWLeFN5svoe0WyWkDi/gAhFI8QeYQj1Rg=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<style>
body { padding-top: 4.5rem; }
pre code { font-size: .9rem; }
.btn-py { font-family: monospace; }
</style>
</head>
<body>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="index.html">Python Zero to Hero</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navContent" aria-controls="navContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navContent">
<ul class="navbar-nav ms-auto">
<li class="nav-item"><a class="nav-link" href="chapter21.html">Chapter 21</a></li>
<li class="nav-item"><a class="nav-link" href="index.html">Index</a></li>
<li class="nav-item"><a class="nav-link" href="chapter23.html">Chapter 23</a></li>
<li class="nav-item">
<a class="nav-link"
href="https://github.com/bocaletto-luca/python-zero-to-hero"
target="_blank">GitHub Repo</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Main Content -->
<main class="container">
<header class="my-4">
<h1 class="display-6">Chapter 22: Building CLI Tools with argparse, Click & Typer</h1>
<p class="text-muted">
Learn to create powerful command-line interfaces using Python’s standard library and popular frameworks.
</p>
<a href="src/chapter22.py" download class="btn btn-outline-primary btn-sm btn-py">
Download <code>chapter22.py</code>
</a>
<hr>
</header>
<!-- Objectives -->
<section id="objectives" class="mb-5">
<h2>Objectives</h2>
<ul>
<li>Use <code>argparse</code> to parse arguments and options.</li>
<li>Create commands and subcommands with <code>Click</code>.</li>
<li>Leverage <code>Typer</code> and type hints to build CLIs.</li>
<li>Package your tool as a console script.</li>
<li>Handle input validation, help text and error messages.</li>
</ul>
</section>
<!-- 1. argparse Basics -->
<section id="argparse" class="mb-5">
<h2>1. argparse Basics</h2>
<pre><code class="python">import argparse
def main():
parser = argparse.ArgumentParser(description="Greet a user.")
parser.add_argument("name", help="Name of the user")
parser.add_argument("-t", "--times", type=int, default=1,
help="Number of greetings")
args = parser.parse_args()
for _ in range(args.times):
print(f"Hello, {args.name}!")
if __name__ == "__main__":
main()</code></pre>
<p>Run: <code>python chapter22.py Alice -t 3</code></p>
</section>
<!-- 2. Click Library -->
<section id="click" class="mb-5">
<h2>2. Click</h2>
<pre><code class="python">import click
@click.command()
@click.argument("name")
@click.option("--times", default=1, help="Number of greetings")
def cli(name, times):
"""Greet a user multiple times."""
for _ in range(times):
click.echo(f"Hello, {name}!")
if __name__ == "__main__":
cli()</code></pre>
<p>Install: <code>pip install click</code>. Run: <code>python chapter22.py Bob --times 2</code></p>
</section>
<!-- 3. Typer Framework -->
<section id="typer" class="mb-5">
<h2>3. Typer & Type Hints</h2>
<pre><code class="python">import typer
app = typer.Typer()
@app.command()
def greet(name: str, times: int = 1):
\"\"\"Greet a user multiple times.\"\"\"
for _ in range(times):
typer.echo(f"Hello, {name}!")
if __name__ == "__main__":
app()</code></pre>
<p>Install: <code>pip install typer[all]</code>. Run: <code>python chapter22.py greet Carol --times 3</code></p>
</section>
<!-- 4. Packaging as Console Script -->
<section id="packaging" class="mb-5">
<h2>4. Packaging CLI Tools</h2>
<p>Add to <code>setup.py</code> or <code>pyproject.toml</code>:</p>
<pre><code class="toml">[project.scripts]
mycli = "mypackage.cli:app"</code></pre>
<p>After installation: <code>mycli --help</code></p>
</section>
<!-- Exercises -->
<section id="exercises" class="mb-5">
<h2>Exercises</h2>
<ol>
<li>Extend the <code>argparse</code> example with a <code>--uppercase</code> flag.</li>
<li>Create a Click group with two subcommands: <code>greet</code> and <code>farewell</code>.</li>
<li>Build a Typer app with commands to add/list/remove items from an in-memory todo list.</li>
<li>Package the Typer app and install it locally as <code>todo-cli</code>.</li>
</ol>
</section>
<!-- Navigation -->
<nav class="d-flex justify-content-between mb-5">
<a href="chapter21.html" class="btn btn-outline-secondary">← Chapter 21</a>
<a href="chapter23.html" class="btn btn-primary">Chapter 23 →</a>
</nav>
</main>
<!-- Footer -->
<footer class="text-center py-4 border-top">
<small>
© 2025 Python Zero to Hero •
<a href="https://github.com/bocaletto-luca/python-zero-to-hero" target="_blank">
bocaletto-luca/python-zero-to-hero
</a>
</small>
</footer>
<!-- Bootstrap JS Bundle -->
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-pQjTSprQoSWb8PQmxHkFvHUuI+13Z2VBxXc5xykxDc8/8aMbkwg/Sf5FCvbyT1Kg"
crossorigin="anonymous"
></script>
<!-- Highlight.js JS -->
<script
src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"
integrity="sha512-v+HDTvKi4ym72wvsLmDdWKH1Z9r7qmOZEk11Kd/PO5vQRO3KHSEt+XeajpxdsBUW5Fve4BJR+wHrHBW2ApwBMQ=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
></script>
<script>hljs.highlightAll();</script>
</body>
</html>