-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sample code for the Click article #415
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Click and Python: Build Extensible and Composable CLI Apps | ||
|
||
This folder provides the code examples for the Real Python tutorial [Click and Python: Build Extensible and Composable CLI Apps](https://realpython.com/python-click/). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import click | ||
|
||
|
||
@click.command() | ||
def cli(): | ||
a = click.prompt("Enter an integer", type=click.INT, default=0) | ||
b = click.prompt("Enter another integer", type=click.INT, default=0) | ||
|
||
click.echo(f"{a} + {b} = {a + b}") | ||
|
||
|
||
if __name__ == "__main__": | ||
cli() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import click | ||
|
||
|
||
@click.group() | ||
def cli(): | ||
pass | ||
|
||
|
||
@cli.command() | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def add(a, b): | ||
click.echo(a + b) | ||
|
||
|
||
@cli.command() | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def sub(a, b): | ||
click.echo(a - b) | ||
|
||
|
||
@cli.command() | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def mul(a, b): | ||
click.echo(a * b) | ||
|
||
|
||
@cli.command() | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def div(a, b): | ||
click.echo(a / b) | ||
|
||
|
||
if __name__ == "__main__": | ||
cli() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import click | ||
import commands | ||
|
||
|
||
@click.group() | ||
def cli(): | ||
pass | ||
|
||
|
||
cli.add_command(commands.add) | ||
cli.add_command(commands.sub) | ||
cli.add_command(commands.mul) | ||
cli.add_command(commands.div) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be two blank lines here for consistency. |
||
if __name__ == "__main__": | ||
cli() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import click | ||
|
||
|
||
@click.command() | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def add(a, b): | ||
click.echo(a + b) | ||
|
||
|
||
@click.command() | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def sub(a, b): | ||
click.echo(a - b) | ||
|
||
|
||
@click.command() | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def mul(a, b): | ||
click.echo(a * b) | ||
|
||
|
||
@click.command() | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def div(a, b): | ||
click.echo(a / b) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import click | ||
import commands | ||
|
||
|
||
@click.group() | ||
def cli(): | ||
pass | ||
|
||
|
||
cli.add_command(commands.add) | ||
cli.add_command(commands.sub) | ||
cli.add_command(commands.mul) | ||
cli.add_command(commands.div) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should there be two blank lines here as well? |
||
if __name__ == "__main__": | ||
cli() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import click | ||
|
||
|
||
@click.command(help="Add two numbers.") | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def add(a, b): | ||
click.echo(a + b) | ||
|
||
|
||
@click.command(help="Subtract two numbers.") | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def sub(a, b): | ||
click.echo(a - b) | ||
|
||
|
||
@click.command(help="Multiply two numbers.") | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def mul(a, b): | ||
click.echo(a * b) | ||
|
||
|
||
@click.command(help="Divide two numbers.") | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def div(a, b): | ||
click.echo(a / b) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# calc | ||
|
||
Calc is a CLI application that performs arithmetic operations. It takes a subcommand representing the target operation and two numeric arguments. It returns the result of performing the target operations between the two input values. | ||
|
||
## Installation | ||
|
||
1. Create a Python virtual environment | ||
|
||
```sh | ||
$ python -m venv ./venv | ||
$ source venv/bin/activate | ||
(venv) $ | ||
``` | ||
|
||
2. Install calc in editable mode | ||
|
||
```sh | ||
(venv) $ python -m pip install -e . | ||
``` | ||
|
||
## Run the Project | ||
|
||
```sh | ||
(venv) $ calc add 3 4 | ||
7.0 | ||
|
||
(venv) $ calc sub 3 4 | ||
-1.0 | ||
|
||
(venv) $ calc mul 3 4 | ||
12.0 | ||
|
||
(venv) $ calc div 3 4 | ||
0.75 | ||
``` | ||
|
||
## Features | ||
|
||
Calc currently provides the following options: | ||
|
||
- `add` takes two numbers and adds them together. | ||
- `sub` takes two numbers and subtracts them. | ||
- `mul` takes two numbers and multiplies them. | ||
- `div` takes two numbers and divides them. | ||
|
||
## About the Author | ||
|
||
Leodanis Pozo Ramos - Email: [email protected] | ||
|
||
## License | ||
|
||
Distributed under the MIT license. See `LICENSE` in the root directory of this `materials` repo for more information. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import click | ||
|
||
from . import commands | ||
|
||
|
||
@click.group() | ||
def cli(): | ||
pass | ||
|
||
|
||
cli.add_command(commands.add) | ||
cli.add_command(commands.sub) | ||
cli.add_command(commands.mul) | ||
cli.add_command(commands.div) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import click | ||
|
||
|
||
@click.command(help="Add two numbers.") | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def add(a, b): | ||
click.echo(a + b) | ||
|
||
|
||
@click.command(help="Subtract two numbers.") | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def sub(a, b): | ||
click.echo(a - b) | ||
|
||
|
||
@click.command(help="Multiply two numbers.") | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def mul(a, b): | ||
click.echo(a * b) | ||
|
||
|
||
@click.command(help="Divide two numbers.") | ||
@click.argument("a", type=click.FLOAT) | ||
@click.argument("b", type=click.FLOAT) | ||
def div(a, b): | ||
click.echo(a / b) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[build-system] | ||
requires = ["setuptools>=65.5.0", "wheel"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[project] | ||
name = "calc" | ||
version = "0.1.0" | ||
description = "A CLI application that performs arithmetic operations." | ||
readme = "README.md" | ||
authors = [{ name = "Real Python", email = "[email protected]" }] | ||
dependencies = [ | ||
"click >= 8.1.3", | ||
] | ||
|
||
[project.scripts] | ||
calc = "calc.__main__:cli" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import click | ||
|
||
|
||
@click.command() | ||
@click.argument( | ||
"files", | ||
nargs=-1, | ||
type=click.File("r"), | ||
) | ||
def cli(files): | ||
for file in files: | ||
click.echo(file.read().rstrip()) | ||
|
||
|
||
if __name__ == "__main__": | ||
cli() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import click | ||
|
||
|
||
@click.command() | ||
@click.option( | ||
"--weekday", | ||
type=click.Choice( | ||
[ | ||
"Monday", | ||
"Tuesday", | ||
"Wednesday", | ||
"Thursday", | ||
"Friday", | ||
"Saturday", | ||
"Sunday", | ||
] | ||
), | ||
) | ||
def cli(weekday): | ||
click.echo(f"Weekday: {weekday}") | ||
|
||
|
||
if __name__ == "__main__": | ||
cli() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import click | ||
|
||
|
||
@click.command() | ||
@click.option( | ||
"--name", | ||
multiple=True, | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Collapse these into a single line? |
||
def cli(name): | ||
for n in name: | ||
click.echo(f"Hello, {n}!") | ||
|
||
|
||
if __name__ == "__main__": | ||
cli() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import click | ||
|
||
|
||
@click.command("hello") | ||
@click.version_option("0.1.0", prog_name="hello") | ||
def hello(): | ||
click.echo("Hello, World!") | ||
|
||
|
||
if __name__ == "__main__": | ||
hello() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from pathlib import Path | ||
|
||
import click | ||
|
||
|
||
@click.command() | ||
@click.argument("path") | ||
def cli(path): | ||
target_dir = Path(path) | ||
if not target_dir.exists(): | ||
click.echo("The target directory doesn't exist") | ||
raise SystemExit(1) | ||
|
||
for entry in target_dir.iterdir(): | ||
click.echo(f"{entry.name:{len(entry.name) + 5}}", nl=False) | ||
|
||
click.echo() | ||
|
||
|
||
if __name__ == "__main__": | ||
cli() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from pathlib import Path | ||
|
||
import click | ||
|
||
|
||
@click.command() | ||
@click.argument( | ||
"path", | ||
type=click.Path( | ||
exists=True, | ||
file_okay=False, | ||
readable=True, | ||
path_type=Path, | ||
), | ||
) | ||
def cli(path): | ||
for entry in path.iterdir(): | ||
click.echo(f"{entry.name:{len(entry.name) + 5}}", nl=False) | ||
|
||
click.echo() | ||
|
||
|
||
if __name__ == "__main__": | ||
cli() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this extra blank line here?