:memo: Add docs for asking with a prompt
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sebastián Ramírez · committed by Sebastián Ramírez · parent 63db894 · Browse files · 5 files·+112 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+112 added−0 removed
- other:+112 added−0 removed
Provenance
- Commit
541ef46- Tree
99c042f- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
Sebastián Ramírez <tiangolo@gmail.com>- Parent
63db894
5 files changed+112 added−0 removed
added:docs/src/prompt/tutorial001.py+10 added
1+import typer2+3+4+def main():5+ person_name = typer.prompt("What's your name?")6+ typer.echo(f"Hello {person_name}")7+8+9+if __name__ == "__main__":10+ typer.run(main)added:docs/src/prompt/tutorial002.py+13 added
1+import typer2+3+4+def main():5+ delete = typer.confirm("Are you sure you want to delete it?")6+ if not delete:7+ typer.echo("Not deleting")8+ raise typer.Abort()9+ typer.echo("Deleting it!")10+11+12+if __name__ == "__main__":13+ typer.run(main)added:docs/src/prompt/tutorial003.py+10 added
1+import typer2+3+4+def main():5+ delete = typer.confirm("Are you sure you want to delete it?", abort=True)6+ typer.echo("Deleting it!")7+8+9+if __name__ == "__main__":10+ typer.run(main)added:docs/tutorial/prompt.md+78 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778When you need to ask the user for info interactively you should normally use <a href="https://typer.tiangolo.com/tutorial/options/prompt/" target="_blank">*CLI Option*s with Prompt</a>, because they allow using the CLI program in a non-interactive way (for example, a Bash script could use it).
But if you absolutely need to ask for interactive information without using a *CLI option*, you can use `typer.prompt()`:
```Python hl_lines="5"{!./src/prompt/tutorial001.py!}```
Check it:
<div class="termy">
```console$ python main.py
# What's your name?:$ Camila
Hello Camila```
</div>
## Confirm
There's also an alternative to ask for confirmation. Again, if possible, you should use a <a href="https://typer.tiangolo.com/tutorial/options/prompt/" target="_blank">*CLI Option* with a confirmation prompt</a>:
```Python hl_lines="5"{!./src/prompt/tutorial002.py!}```
Check it:
<div class="termy">
```console$ python main.py
# Are you sure you want to delete it? [y/N]:$ y
Deleting it!
// This time cancel it$ python main.py
# Are you sure you want to delete it? [y/N]:$ n
Not deletingAborted!```
</div>
## Confirm or abort
As it's very common to abort if the user doesn't confirm, there's an integrated parameter `abort` that does it automatically:
```Python hl_lines="5"{!./src/prompt/tutorial003.py!}```
<div class="termy">
```console$ python main.py
# Are you sure you want to delete it? [y/N]:$ y
Deleting it!
// This time cancel it$ python main.py
# Are you sure you want to delete it? [y/N]:$ n
Aborted!```
</div>modified:mkdocs.yml+1 added
45 unmodified lines46474849505152535434 unmodified lines45 unmodified lines - DateTime: 'tutorial/parameter-types/datetime.md' - Enum - Choices: 'tutorial/parameter-types/enum.md' - Path: 'tutorial/parameter-types/path.md' - File: 'tutorial/parameter-types/file.md' - Ask with Prompt: 'tutorial/prompt.md' - Alternatives, Inspiration and Comparisons: 'alternatives.md' - Help Typer - Get Help: 'help-typer.md' - Development - Contributing: 'contributing.md' - Release Notes: release-notes.md34 unmodified lines