Introduction
Click is a composable command-line interface creation library with simple syntax.
Basic Command
import click
@click.command()
@click.argument("name")
def hello(name):
click.echo(f"Hello, {name}!")
if __name__ == "__main__":
hello()
Options and Flags
import click
@click.command()
@click.option("--count", "-c", default=1, help="Number of greetings")
@click.option("--shout", is_flag=True, help="Shout")
@click.argument("name")
def hello(count, shout, name):
for _ in range(count):
msg = f"Hello, {name}!"
if shout:
msg = msg.upper()
click.echo(msg)
Prompts
import click
name = click.prompt("Enter your name", default="Anonymous")
if click.confirm("Continue?"):
click.echo("Continuing...")
Practice Problems
- Create CLI with multiple commands
- Add option flags
- Use prompts for user input
- Build menu-based CLI
- Colorize output