:memo: Add docs for interactive prompts.
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 GitHub · parents 63db894, 7673cab · Browse files · 9 files·+211 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
9 files+211 added−0 removed
- tests:+99 added−0 removed
- other:+112 added−0 removed
Provenance
- Commit
92a7a01- Tree
a19d02a- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
63db894- Parent 2
7673cab
9 files changed+211 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>added:tests/test_tutorial/test_prompt/__init__.py
added:tests/test_tutorial/test_prompt/test_tutorial001.py+28 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from prompt import tutorial001 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_cli():15+ result = runner.invoke(app, input="Camila\n")16+ assert result.exit_code == 017+ assert "What's your name?:" in result.output18+ assert "Hello Camila" in result.output19+20+21+def test_script():22+ result = subprocess.run(23+ ["coverage", "run", mod.__file__, "--help"],24+ stdout=subprocess.PIPE,25+ stderr=subprocess.PIPE,26+ encoding="utf-8",27+ )28+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_prompt/test_tutorial002.py+36 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from prompt import tutorial002 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_cli():15+ result = runner.invoke(app, input="y\n")16+ assert result.exit_code == 017+ assert "Are you sure you want to delete it? [y/N]:" in result.output18+ assert "Deleting it!" in result.output19+20+21+def test_no_confirm():22+ result = runner.invoke(app, input="n\n")23+ assert result.exit_code == 124+ assert "Are you sure you want to delete it? [y/N]:" in result.output25+ assert "Not deleting" in result.output26+ assert "Aborted!" in result.output27+28+29+def test_script():30+ result = subprocess.run(31+ ["coverage", "run", mod.__file__, "--help"],32+ stdout=subprocess.PIPE,33+ stderr=subprocess.PIPE,34+ encoding="utf-8",35+ )36+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_prompt/test_tutorial003.py+35 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from prompt import tutorial003 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_cli():15+ result = runner.invoke(app, input="y\n")16+ assert result.exit_code == 017+ assert "Are you sure you want to delete it? [y/N]:" in result.output18+ assert "Deleting it!" in result.output19+20+21+def test_no_confirm():22+ result = runner.invoke(app, input="n\n")23+ assert result.exit_code == 124+ assert "Are you sure you want to delete it? [y/N]:" in result.output25+ assert "Aborted!" in result.output26+27+28+def test_script():29+ result = subprocess.run(30+ ["coverage", "run", mod.__file__, "--help"],31+ stdout=subprocess.PIPE,32+ stderr=subprocess.PIPE,33+ encoding="utf-8",34+ )35+ assert "Usage" in result.stdoutmodified:mkdocs.yml+1 added
45 unmodified lines46 - DateTime: 'tutorial/parameter-types/datetime.md'47 - Enum - Choices: 'tutorial/parameter-types/enum.md'48 - Path: 'tutorial/parameter-types/path.md'49 - File: 'tutorial/parameter-types/file.md'50+ - Ask with Prompt: 'tutorial/prompt.md'51 - Alternatives, Inspiration and Comparisons: 'alternatives.md'52 - Help Typer - Get Help: 'help-typer.md'53 - Development - Contributing: 'contributing.md'54 - Release Notes: release-notes.md34 unmodified lines