Skip to content
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

Merged
merged 5 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-click/README.md
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/).
13 changes: 13 additions & 0 deletions python-click/add.py
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)

Copy link
Contributor

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?

click.echo(f"{a} + {b} = {a + b}")


if __name__ == "__main__":
cli()
38 changes: 38 additions & 0 deletions python-click/calc_v1.py
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()
16 changes: 16 additions & 0 deletions python-click/calc_v2/calc.py
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)

Copy link
Contributor

Choose a reason for hiding this comment

The 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()
29 changes: 29 additions & 0 deletions python-click/calc_v2/commands.py
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)
16 changes: 16 additions & 0 deletions python-click/calc_v3/calc.py
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)

Copy link
Contributor

Choose a reason for hiding this comment

The 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()
29 changes: 29 additions & 0 deletions python-click/calc_v3/commands.py
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)
52 changes: 52 additions & 0 deletions python-click/calc_v4/README.md
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.
Empty file.
14 changes: 14 additions & 0 deletions python-click/calc_v4/calc/__main__.py
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)
29 changes: 29 additions & 0 deletions python-click/calc_v4/calc/commands.py
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)
16 changes: 16 additions & 0 deletions python-click/calc_v4/pyproject.toml
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"
16 changes: 16 additions & 0 deletions python-click/cat.py
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()
24 changes: 24 additions & 0 deletions python-click/days.py
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()
15 changes: 15 additions & 0 deletions python-click/greet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import click


@click.command()
@click.option(
"--name",
multiple=True,
)
Copy link
Contributor

Choose a reason for hiding this comment

The 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()
11 changes: 11 additions & 0 deletions python-click/hello.py
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()
21 changes: 21 additions & 0 deletions python-click/ls_v1.py
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()
24 changes: 24 additions & 0 deletions python-click/ls_v2.py
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()
Loading
Loading