Skip to content
Myr

:memo: Update README.md from docs/index.md

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 0842c83 · Browse files · 1 file+38 added−81 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+38 added−81 removed

  • other:+38 added−81 removed

1 file+38 added−81 removed

modified:README.md+38 added-81 removed
42 unmodified lines
43Typer stands on the shoulders of a giant. Its only internal dependency is <a href="https://click.palletsprojects.com/" target="_blank">Click</a>.4445## Installation4647<div class="termy">47```bash48pip install typer48```console49$ pip install typer50---> 100%51Successfully installed typer52```53</div>5455## Example5657### The absolute minimum
15 unmodified lines
73### Run it7475Run your application:7673```bash74python main.py75```77<div class="termy">78```console79// Run your application80$ python main.py8177you will get a response like:7879```82// You get a nice error, you are missing NAME83Usage: main.py [OPTIONS] NAME84Try "main.py --help" for help.8586Error: Missing argument "NAME".84```8786Now pass the `NAME` *argument*:88// You get a --help for free89$ python main.py --help9088```bash89python main.py Camila90```91Usage: main.py [OPTIONS] NAME9292You will get a response like:93Options:94 --help Show this message and exit.9594```96// Now pass the NAME argument97$ python main.py Camila9899Hello Camila96```10098And you automatically get a `--help` command:99100```bash101python main.py --help101// It works! 🎉102```103</div>104104shows:105106```107Usage: main.py [OPTIONS] NAME108109Options:110 --help Show this message and exit.111```112105## Example upgrade106107This was the simplest example possible.108
36 unmodified lines
145* Execute the `app()` itself, as if it was a function (instead of `typer.run`).146147### Run the upgraded example148157Get the main `--help`:149<div class="termy">150```console151// Check the --help152$ python main.py --help153159```bash160python main.py --help161```162163shows:164165```154Usage: main.py [OPTIONS] COMMAND [ARGS]...155156Options:157 --help Show this message and exit.158159Commands:160 goodbye161 hello174```162176You have 2 sub-commands (the 2 functions), `goodbye` and `hello`.163// You have 2 sub-commands (the 2 functions): goodbye and hello164178Now get the help for `hello`:165// Now get the --help for hello166180```bash181python main.py hello --help167$ python main.py hello --help182```168184shows:185186```169Usage: main.py hello [OPTIONS] NAME170171Options:172 --help Show this message and exit.191```173193And now get the help for `goodbye`:174// And now get the --help for goodbye175195```bash196python main.py goodbye --help176$ python main.py goodbye --help197```177199shows:200201```178Usage: main.py goodbye [OPTIONS] NAME179180Options:181 --formal / --no-formal182 --help Show this message and exit.207```183209Notice how it automatically creates a `--formal` and `--no-formal` for your `bool` *option*.184// Automatic --formal and --no-formal for the bool option 🎉185211---186// And if you use it with the hello command187213And of course, if you use it, it does what you expect:188$ python main.py hello Camila189215```bash216python main.py hello Camila217```218219shows:220221```190Hello Camila223```191225Then:192// And with the goodbye command193227```bash228python main.py goodbye Camila194$ python main.py goodbye Camila229```195231shows:232233```196Bye Camila!235```197237And:198// And with --formal199239```bash240python main.py goodbye --formal Camila200$ python main.py goodbye --formal Camila241```201243shows:244245```202Goodbye Ms. Camila. Have a good day.203```204</div>205206### Recap207208In summary, you declare **once** the types of parameters (*arguments* and *options*) as function parameters.
54 unmodified lines