Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion docs/src/parameter_types/path/tutorial001.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import typer


def main(config: Path = typer.Option(...)):
def main(config: Path = typer.Option(None)):
if config is None:
typer.echo("No config file")
raise typer.Abort()
if config.is_file():
text = config.read_text()
typer.echo(f"Config file contents: {text}")
Expand Down
12 changes: 11 additions & 1 deletion docs/tutorial/parameter-types/path.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ Check it:
<div class="termy">

```console
// No config
$ python main.py

No config file
Aborted!

// Pass a config that doesn't exist
$ python main.py --config config.txt

The config doesn't exist
Expand Down Expand Up @@ -42,7 +49,10 @@ You can perform several validations for `Path` *CLI parameters*:
* `dir_okay`: controls if a directory is a possible value.
* `writable`: if true, a writable check is performed.
* `readable`: if true, a readable check is performed.
* `resolve_path`: if this is true, then the path is fully resolved before the value is passed onwards. This means that it’s absolute and symlinks are resolved. It will not expand a tilde-prefix, as this is supposed to be done by the shell only.
* `resolve_path`: if this is true, then the path is fully resolved before the value is passed onwards. This means that it’s absolute and <abbr title="symbolic links, also known as shortcuts. Links in a file system that point to other location. For example, some applications when installed create symlinks in the desktop to launch them.">symlinks</abbr> are resolved.

!!! note "Technical Details"
It will not expand a tilde-prefix (something with `~`, like `~/Documents/`), as this is supposed to be done by the shell only.

!!! tip
All these parameters come directly from <a href="https://click.palletsprojects.com/en/7.x/parameters/#parameter-types" target="_blank">Click</a>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
config_file = Path("./config.txt")


def test_no_path():
result = runner.invoke(app)
assert result.exit_code == 1
assert "No config file" in result.output
assert "Aborted!" in result.output


def test_not_exists():
if config_file.exists(): # pragma no cover
config_file.unlink()
Expand Down