Module 02 / Project 01 -- Click Basics¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | Walkthrough | — | Flashcards | — | — |
Focus¶
@click.command()decorator to define a CLI entry point@click.option()for optional flags like--shout@click.argument()for required positional values- Automatic
--helptext generation
Why this project exists¶
Most Python scripts start with if __name__ == "__main__" and a handful of input() calls. That works for throwaway code, but real tools need named options, help text, and predictable argument parsing. Click handles all of that with decorators you stack on top of a plain function. This project shows you how.
Run¶
cd projects/modules/02-cli-tools/01-click-basics
# Basic greeting
python project.py World
# Greeting with the --shout flag
python project.py World --shout
# View auto-generated help
python project.py --help
# Greeting with a custom greeting word
python project.py World --greeting Howdy
Expected output¶
$ python project.py World
Hello, World!
$ python project.py World --shout
HELLO, WORLD!
$ python project.py World --greeting Howdy
Howdy, World!
$ python project.py --help
Usage: project.py [OPTIONS] NAME
Greet someone by name. A small first step into Click.
Options:
--greeting TEXT Word to use for the greeting. [default: Hello]
--shout Uppercase the entire output.
--help Show this message and exit.
Alter it¶
- Add a
--repeatoption (integer, default 1) that prints the greeting N times. - Add a
--farewellflag that prints a goodbye message after the greeting. - Change the default greeting word from "Hello" to something else and confirm
--helpreflects the change.
Break it¶
- Remove the
@click.argument("name")decorator and run the script. What error do you get? - Change
is_flag=Trueon--shouttotype=int. Try running with--shout. What happens? - Pass two positional arguments instead of one. How does Click respond?
Fix it¶
- Restore the missing decorator and confirm the greeting works again.
- Revert
--shoutback to a boolean flag and verify--shouttoggles uppercasing. - Read the Click docs on
nargsand decide whether your tool should accept multiple names.
Explain it¶
- What does
@click.command()do that a bare function does not? - How does Click generate the
--helptext -- where does it pull the description from? - What is the difference between
@click.option()and@click.argument()in terms of required vs optional? - Why does Click use decorators instead of requiring you to subclass something?
Mastery check¶
You can move on when you can:
- write a Click command from scratch without copying this file,
- explain what is_flag=True does vs a typed option,
- add a new option, re-run --help, and confirm it appears,
- break and recover in one session.
Related Concepts¶
- Decorators Explained
- Errors and Debugging
- Functions Explained
- How Imports Work
- Quiz: Decorators Explained
Next¶
Continue to 02 - Multi-Command CLI.