Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gmolveau
GitHub Repository: gmolveau/python_full_course
Path: blob/master/examples/cli/click_group.py
305 views
1
import click
2
3
4
def positive_int(ctx, param, value):
5
if value < 0:
6
raise click.BadParameter("Should be a positive integer")
7
return value
8
9
10
@click.group()
11
def root():
12
pass
13
14
15
@root.group()
16
def cmd():
17
pass
18
19
20
@cmd.command()
21
@click.option("--count", default=1, help="Number of greetings.")
22
@click.option("--name", required=True, help="The person to greet.")
23
@click.argument("n", type=int, default=1, callback=positive_int)
24
def hello(count, name, n):
25
"""Simple program that greets NAME for a total of COUNT times."""
26
print(f"{n=}")
27
for x in range(count):
28
click.echo(f"Hello {name}!")
29
30
31
if __name__ == "__main__":
32
root()
33
34