Skip to content

Commit d88a5c6

Browse files
Create chapter22.html
1 parent 5b015f4 commit d88a5c6

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

ita/chapter22.html

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<!DOCTYPE html>
2+
<html lang="it">
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
6+
<meta name="description"
7+
content="Capitolo 22 di Python Zero to Hero: costruisci strumenti da riga di comando con argparse, Click e Typer."/>
8+
<meta name="keywords"
9+
content="Python, CLI, linea di comando, argparse, click, typer, console script, strumenti CLI"/>
10+
<meta name="author" content="Luca Bocaletto"/>
11+
<title>Capitolo 22 – Strumenti CLI con argparse, Click &amp; Typer | Python Zero to Hero</title>
12+
13+
<!-- Bootstrap 5 CSS -->
14+
<link
15+
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
16+
rel="stylesheet"
17+
crossorigin="anonymous"
18+
/>
19+
20+
<!-- Highlight.js CSS -->
21+
<link
22+
rel="stylesheet"
23+
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css"
24+
crossorigin="anonymous"
25+
referrerpolicy="no-referrer"
26+
/>
27+
28+
<style>
29+
body { padding-top: 4.5rem; }
30+
pre code { font-size: .9rem; }
31+
.btn-py { font-family: monospace; }
32+
</style>
33+
</head>
34+
<body>
35+
<!-- Navbar -->
36+
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
37+
<div class="container">
38+
<a class="navbar-brand" href="index.html">Python Zero to Hero</a>
39+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
40+
data-bs-target="#navContent" aria-controls="navContent"
41+
aria-expanded="false" aria-label="Toggle navigation">
42+
<span class="navbar-toggler-icon"></span>
43+
</button>
44+
<div class="collapse navbar-collapse" id="navContent">
45+
<ul class="navbar-nav ms-auto">
46+
<li class="nav-item"><a class="nav-link" href="chapter21.html">Capitolo 21</a></li>
47+
<li class="nav-item"><a class="nav-link" href="index.html">Indice</a></li>
48+
<li class="nav-item"><a class="nav-link" href="chapter23.html">Capitolo 23</a></li>
49+
<li class="nav-item">
50+
<a class="nav-link"
51+
href="https://github.com/bocaletto-luca/python-zero-to-hero"
52+
target="_blank">Repo GitHub</a>
53+
</li>
54+
</ul>
55+
</div>
56+
</div>
57+
</nav>
58+
59+
<!-- Contenuto principale -->
60+
<main class="container">
61+
<header class="my-4">
62+
<h1 class="display-6">Capitolo 22: Strumenti CLI con argparse, Click &amp; Typer</h1>
63+
<p class="text-muted">
64+
Impara a creare potenti interfacce da riga di comando usando la libreria standard e framework popolari.
65+
</p>
66+
<a href="src/chapter22.py" download class="btn btn-outline-primary btn-sm btn-py">
67+
Scarica <code>chapter22.py</code>
68+
</a>
69+
<hr>
70+
</header>
71+
72+
<section id="objectives" class="mb-5">
73+
<h2>Obiettivi</h2>
74+
<ul>
75+
<li>Usare <code>argparse</code> per analizzare argomenti e opzioni.</li>
76+
<li>Creare comandi e sottocomandi con <code>Click</code>.</li>
77+
<li>Sfruttare <code>Typer</code> e type hint per costruire CLI.</li>
78+
<li>Pacchettizzare lo strumento come script console.</li>
79+
<li>Gestire validazione, messaggi di aiuto ed errori.</li>
80+
</ul>
81+
</section>
82+
83+
<section id="argparse" class="mb-5">
84+
<h2>1. Nozioni di base su argparse</h2>
85+
<pre><code class="python">import argparse
86+
87+
def main():
88+
parser = argparse.ArgumentParser(description="Saluta un utente.")
89+
parser.add_argument("name", help="Nome dell'utente")
90+
parser.add_argument("-t", "--times", type=int, default=1,
91+
help="Numero di saluti")
92+
args = parser.parse_args()
93+
94+
for _ in range(args.times):
95+
print(f"Hello, {args.name}!")
96+
97+
if __name__ == "__main__":
98+
main()</code></pre>
99+
<p>Esegui: <code>python chapter22.py Alice -t 3</code></p>
100+
</section>
101+
102+
<section id="click" class="mb-5">
103+
<h2>2. Libreria Click</h2>
104+
<pre><code class="python">import click
105+
106+
@click.command()
107+
@click.argument("name")
108+
@click.option("--times", default=1, help="Numero di saluti")
109+
def cli(name, times):
110+
"""Saluta un utente più volte."""
111+
for _ in range(times):
112+
click.echo(f"Hello, {name}!")
113+
114+
if __name__ == "__main__":
115+
cli()</code></pre>
116+
<p>Installa: <code>pip install click</code>. Esegui: <code>python chapter22.py Bob --times 2</code></p>
117+
</section>
118+
119+
<section id="typer" class="mb-5">
120+
<h2>3. Typer & Type Hints</h2>
121+
<pre><code class="python">import typer
122+
123+
app = typer.Typer()
124+
125+
@app.command()
126+
def greet(name: str, times: int = 1):
127+
"""Saluta un utente più volte."""
128+
for _ in range(times):
129+
typer.echo(f"Hello, {name}!")
130+
131+
if __name__ == "__main__":
132+
app()</code></pre>
133+
<p>Installa: <code>pip install typer[all]</code>. Esegui: <code>python chapter22.py greet Carol --times 3</code></p>
134+
</section>
135+
136+
<section id="packaging" class="mb-5">
137+
<h2>4. Pacchettizzare come Script Console</h2>
138+
<p>Aggiungi in <code>setup.py</code> o <code>pyproject.toml</code>:</p>
139+
<pre><code class="toml">[project.scripts]
140+
mycli = "mypackage.cli:app"</code></pre>
141+
<p>Dopo l’installazione:<code>mycli --help</code></p>
142+
</section>
143+
144+
<section id="exercises" class="mb-5">
145+
<h2>Esercizi</h2>
146+
<ol>
147+
<li>Estendi l’esempio con <code>argparse</code> aggiungendo il flag <code>--uppercase</code>.</li>
148+
<li>Crea un gruppo di comandi Click con due sottocomandi: <code>greet</code> e <code>farewell</code>.</li>
149+
<li>Costruisci un’app Typer con comandi per aggiungere/listare/rimuovere elementi da una to-do list in memoria.</li>
150+
<li>Pacchettizza l’app Typer e installala localmente come <code>todo-cli</code>.</li>
151+
</ol>
152+
</section>
153+
154+
<nav class="d-flex justify-content-between mb-5">
155+
<a href="chapter21.html" class="btn btn-outline-secondary">&larr; Capitolo 21</a>
156+
<a href="chapter23.html" class="btn btn-primary">Capitolo 23 &rarr;</a>
157+
</nav>
158+
</main>
159+
160+
<footer class="text-center py-4 border-top">
161+
<small>
162+
© 2025 Python Zero to Hero •
163+
<a href="https://github.com/bocaletto-luca/python-zero-to-hero" target="_blank">
164+
bocaletto-luca/python-zero-to-hero
165+
</a>
166+
</small>
167+
</footer>
168+
169+
<!-- Bootstrap JS Bundle -->
170+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
171+
<!-- Highlight.js JS -->
172+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
173+
<script>hljs.highlightAll();</script>
174+
</body>
175+
</html>

0 commit comments

Comments
 (0)