Skip to content

Commit

Permalink
♻️ 📝 Refactor docs' structure and add CLI option names
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo authored Jan 2, 2020
2 parents 19a4f6e + d8a5236 commit 1dacbcd
Show file tree
Hide file tree
Showing 61 changed files with 1,512 additions and 844 deletions.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions docs/src/commands/help/tutorial002.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import typer

app = typer.Typer()


@app.command(help="Create a new user with USERNAME.")
def create(username: str):
"""
Some internal utility function to create.
"""
typer.echo(f"Creating user: {username}")


@app.command(help="Delete a user with USERNAME.")
def delete(username: str):
"""
Some internal utility function to delete.
"""
typer.echo(f"Deleting user: {username}")


if __name__ == "__main__":
app()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions docs/src/options/name/tutorial001.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import typer


def main(user_name: str = typer.Option(..., "--name")):
typer.echo(f"Hello {user_name}")


if __name__ == "__main__":
typer.run(main)
9 changes: 9 additions & 0 deletions docs/src/options/name/tutorial002.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import typer


def main(user_name: str = typer.Option(..., "--name", "-n")):
typer.echo(f"Hello {user_name}")


if __name__ == "__main__":
typer.run(main)
9 changes: 9 additions & 0 deletions docs/src/options/name/tutorial003.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import typer


def main(user_name: str = typer.Option(..., "-n")):
typer.echo(f"Hello {user_name}")


if __name__ == "__main__":
typer.run(main)
9 changes: 9 additions & 0 deletions docs/src/options/name/tutorial004.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import typer


def main(user_name: str = typer.Option(..., "--user-name", "-n")):
typer.echo(f"Hello {user_name}")


if __name__ == "__main__":
typer.run(main)
15 changes: 15 additions & 0 deletions docs/src/options/name/tutorial005.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import typer


def main(
name: str = typer.Option(..., "--name", "-n"),
formal: bool = typer.Option(False, "--formal", "-f"),
):
if formal:
typer.echo(f"Good day Ms. {name}.")
else:
typer.echo(f"Hello {name}")


if __name__ == "__main__":
typer.run(main)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 5 additions & 10 deletions docs/tutorial/arguments.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The same way that you have `typer.Option()` to help you define things for *CLI options*, there's also the equivalent `typer.Argument()` for *CLI arguments*.
Let's see how to configure *CLI arguments* with `typer.Argument()`.

## Optional *CLI arguments*

Expand Down Expand Up @@ -63,20 +63,15 @@ And then we changed it to:
name: str = typer.Argument(...)
```

The same as with `typer.Option()`, there is a `typer.Argument()`.
But now as `typer.Argument()` is the "default value" of the function's parameter, it would mean that "it is no longer required" (in Python terms).

And now as `typer.Argument()` is the "default value" of the function's parameter, in Python terms, it would mean that "it is no longer required" (in Python terms).
As we no longer have the Python function default value (or its absence) to tell if something is required or not and what is the default value, the first parameter to `typer.Argument()` serves the same purpose of defining that default value, or making it required.

As we no longer have the Python function default value (or its absence) to tell it if something is required or not and what is the default value, the first parameter to `typer.Argument()` serves the same purpose of defining that default value, or making it required.

To make it *required*, we pass `...` as that first parameter to the function.
To make it *required*, we pass `...` as the first function argument passed to `typer.Argument(...)`.

!!! info
If you hadn't seen that `...` before: it is a a special single value, it is <a href="https://docs.python.org/3/library/constants.html#Ellipsis" target="_blank">part of Python and is called "Ellipsis"</a>.

!!! tip
This works exactly the same way `typer.Option()` does.

All we did there achieves the same thing as before, a **required** *CLI argument*:

<div class="termy">
Expand Down Expand Up @@ -150,7 +145,7 @@ $ python main.py
Hello World!

// With one optional CLI argument
$ python main.py
$ python main.py Camila

Hello Camila
```
Expand Down
Loading

0 comments on commit 1dacbcd

Please sign in to comment.