:recycle: :memo: Refactor docs' structure and add CLI option names
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 19a4f6e, d8a5236 · Browse files · 85 files·+2316 added−1648 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
85 files+2316 added−1648 removed
- tests:+839 added−584 removed
- other:+1477 added−1064 removed
Provenance
- Commit
1dacbcd- Tree
d0f254e- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
19a4f6e- Parent 2
d8a5236
85 files changed+2316 added−1648 removed
added:docs/src/commands/arguments/tutorial001.py+17 added
1+import typer2+3+app = typer.Typer()4+5+6+@app.command()7+def create(username: str):8+ typer.echo(f"Creating user: {username}")9+10+11+@app.command()12+def delete(username: str):13+ typer.echo(f"Deleting user: {username}")14+15+16+if __name__ == "__main__":17+ app()added:docs/src/commands/help/tutorial001.py+62 added
1+import typer2+3+app = typer.Typer(help="Awesome CLI user manager.")4+5+6+@app.command()7+def create(username: str):8+ """9+ Create a new user with USERNAME.10+ """11+ typer.echo(f"Creating user: {username}")12+13+14+@app.command()15+def delete(16+ username: str,17+ force: bool = typer.Option(18+ ...,19+ prompt="Are you sure you want to delete the user?",20+ help="Force deletion without confirmation.",21+ ),22+):23+ """24+ Delete a user with USERNAME.25+26+ If --force is not used, will ask for confirmation.27+ """28+ if force:29+ typer.echo(f"Deleting user: {username}")30+ else:31+ typer.echo("Operation cancelled")32+33+34+@app.command()35+def delete_all(36+ force: bool = typer.Option(37+ ...,38+ prompt="Are you sure you want to delete ALL users?",39+ help="Force deletion without confirmation.",40+ )41+):42+ """43+ Delete ALL users in the database.44+45+ If --force is not used, will ask for confirmation.46+ """47+ if force:48+ typer.echo("Deleting all users")49+ else:50+ typer.echo("Operation cancelled")51+52+53+@app.command()54+def init():55+ """56+ Initialize the users database.57+ """58+ typer.echo("Initializing user database")59+60+61+if __name__ == "__main__":62+ app()added:docs/src/commands/help/tutorial002.py+23 added
1+import typer2+3+app = typer.Typer()4+5+6+@app.command(help="Create a new user with USERNAME.")7+def create(username: str):8+ """9+ Some internal utility function to create.10+ """11+ typer.echo(f"Creating user: {username}")12+13+14+@app.command(help="Delete a user with USERNAME.")15+def delete(username: str):16+ """17+ Some internal utility function to delete.18+ """19+ typer.echo(f"Deleting user: {username}")20+21+22+if __name__ == "__main__":23+ app()added:docs/src/commands/index/tutorial001.py+12 added
1+import typer2+3+app = typer.Typer()4+5+6+@app.command()7+def main(name: str):8+ typer.echo(f"Hello {name}")9+10+11+if __name__ == "__main__":12+ app()added:docs/src/commands/index/tutorial002.py+17 added
1+import typer2+3+app = typer.Typer()4+5+6+@app.command()7+def create():8+ typer.echo("Creating user: Hiro Hamada")9+10+11+@app.command()12+def delete():13+ typer.echo("Deleting user: Hiro Hamada")14+15+16+if __name__ == "__main__":17+ app()added:docs/src/commands/name/tutorial001.py+17 added
1+import typer2+3+app = typer.Typer()4+5+6+@app.command("create")7+def cli_create_user(username: str):8+ typer.echo(f"Creating user: {username}")9+10+11+@app.command("delete")12+def cli_delete_user(username: str):13+ typer.echo(f"Deleting user: {username}")14+15+16+if __name__ == "__main__":17+ app()added:docs/src/commands/options/tutorial001.py+38 added
1+import typer2+3+app = typer.Typer()4+5+6+@app.command()7+def create(username: str):8+ typer.echo(f"Creating user: {username}")9+10+11+@app.command()12+def delete(13+ username: str,14+ force: bool = typer.Option(..., prompt="Are you sure you want to delete the user?"),15+):16+ if force:17+ typer.echo(f"Deleting user: {username}")18+ else:19+ typer.echo("Operation cancelled")20+21+22+@app.command()23+def delete_all(24+ force: bool = typer.Option(..., prompt="Are you sure you want to delete ALL users?")25+):26+ if force:27+ typer.echo("Deleting all users")28+ else:29+ typer.echo("Operation cancelled")30+31+32+@app.command()33+def init():34+ typer.echo("Initializing user database")35+36+37+if __name__ == "__main__":38+ app()deleted:docs/src/commands/tutorial001.py-12 removed
1-import typer2-3-app = typer.Typer()4-5-6-@app.command()7-def main(name: str):8- typer.echo(f"Hello {name}")9-10-11-if __name__ == "__main__":12- app()deleted:docs/src/commands/tutorial002.py-17 removed
1-import typer2-3-app = typer.Typer()4-5-6-@app.command()7-def create():8- typer.echo("Creating user: Hiro Hamada")9-10-11-@app.command()12-def delete():13- typer.echo("Deleting user: Hiro Hamada")14-15-16-if __name__ == "__main__":17- app()deleted:docs/src/commands/tutorial003.py-17 removed
1-import typer2-3-app = typer.Typer()4-5-6-@app.command()7-def create(username: str):8- typer.echo(f"Creating user: {username}")9-10-11-@app.command()12-def delete(username: str):13- typer.echo(f"Deleting user: {username}")14-15-16-if __name__ == "__main__":17- app()deleted:docs/src/commands/tutorial004.py-38 removed
1-import typer2-3-app = typer.Typer()4-5-6-@app.command()7-def create(username: str):8- typer.echo(f"Creating user: {username}")9-10-11-@app.command()12-def delete(13- username: str,14- force: bool = typer.Option(..., prompt="Are you sure you want to delete the user?"),15-):16- if force:17- typer.echo(f"Deleting user: {username}")18- else:19- typer.echo("Operation cancelled")20-21-22-@app.command()23-def delete_all(24- force: bool = typer.Option(..., prompt="Are you sure you want to delete ALL users?")25-):26- if force:27- typer.echo("Deleting all users")28- else:29- typer.echo("Operation cancelled")30-31-32-@app.command()33-def init():34- typer.echo("Initializing user database")35-36-37-if __name__ == "__main__":38- app()deleted:docs/src/commands/tutorial005.py-62 removed
1-import typer2-3-app = typer.Typer(help="Awesome CLI user manager.")4-5-6-@app.command()7-def create(username: str):8- """9- Create a new user with USERNAME.10- """11- typer.echo(f"Creating user: {username}")12-13-14-@app.command()15-def delete(16- username: str,17- force: bool = typer.Option(18- ...,19- prompt="Are you sure you want to delete the user?",20- help="Force deletion without confirmation.",21- ),22-):23- """24- Delete a user with USERNAME.25-26- If --force is not used, will ask for confirmation.27- """28- if force:29- typer.echo(f"Deleting user: {username}")30- else:31- typer.echo("Operation cancelled")32-33-34-@app.command()35-def delete_all(36- force: bool = typer.Option(37- ...,38- prompt="Are you sure you want to delete ALL users?",39- help="Force deletion without confirmation.",40- )41-):42- """43- Delete ALL users in the database.44-45- If --force is not used, will ask for confirmation.46- """47- if force:48- typer.echo("Deleting all users")49- else:50- typer.echo("Operation cancelled")51-52-53-@app.command()54-def init():55- """56- Initialize the users database.57- """58- typer.echo("Initializing user database")59-60-61-if __name__ == "__main__":62- app()deleted:docs/src/commands/tutorial006.py-17 removed
1-import typer2-3-app = typer.Typer()4-5-6-@app.command("create")7-def cli_create_user(username: str):8- typer.echo(f"Creating user: {username}")9-10-11-@app.command("delete")12-def cli_delete_user(username: str):13- typer.echo(f"Deleting user: {username}")14-15-16-if __name__ == "__main__":17- app()added:docs/src/options/help/tutorial001.py+21 added
1+import typer2+3+4+def main(5+ name: str,6+ lastname: str = typer.Option("", help="Last name of person to greet."),7+ formal: bool = typer.Option(False, help="Say hi formally."),8+):9+ """10+ Say hi to NAME, optionally with a --lastname.11+12+ If --formal is used, say hi very formally.13+ """14+ if formal:15+ typer.echo(f"Good day Ms. {name} {lastname}.")16+ else:17+ typer.echo(f"Hello {name} {lastname}")18+19+20+if __name__ == "__main__":21+ typer.run(main)added:docs/src/options/help/tutorial002.py+9 added
1+import typer2+3+4+def main(fullname: str = typer.Option("Wade Wilson", show_default=True)):5+ typer.echo(f"Hello {fullname}")6+7+8+if __name__ == "__main__":9+ typer.run(main)added:docs/src/options/name/tutorial001.py+9 added
1+import typer2+3+4+def main(user_name: str = typer.Option(..., "--name")):5+ typer.echo(f"Hello {user_name}")6+7+8+if __name__ == "__main__":9+ typer.run(main)added:docs/src/options/name/tutorial002.py+9 added
1+import typer2+3+4+def main(user_name: str = typer.Option(..., "--name", "-n")):5+ typer.echo(f"Hello {user_name}")6+7+8+if __name__ == "__main__":9+ typer.run(main)added:docs/src/options/name/tutorial003.py+9 added
1+import typer2+3+4+def main(user_name: str = typer.Option(..., "-n")):5+ typer.echo(f"Hello {user_name}")6+7+8+if __name__ == "__main__":9+ typer.run(main)added:docs/src/options/name/tutorial004.py+9 added
1+import typer2+3+4+def main(user_name: str = typer.Option(..., "--user-name", "-n")):5+ typer.echo(f"Hello {user_name}")6+7+8+if __name__ == "__main__":9+ typer.run(main)added:docs/src/options/name/tutorial005.py+15 added
1+import typer2+3+4+def main(5+ name: str = typer.Option(..., "--name", "-n"),6+ formal: bool = typer.Option(False, "--formal", "-f"),7+):8+ if formal:9+ typer.echo(f"Good day Ms. {name}.")10+ else:11+ typer.echo(f"Hello {name}")12+13+14+if __name__ == "__main__":15+ typer.run(main)added:docs/src/options/prompt/tutorial001.py+9 added
1+import typer2+3+4+def main(name: str, lastname: str = typer.Option(..., prompt=True)):5+ typer.echo(f"Hello {name} {lastname}")6+7+8+if __name__ == "__main__":9+ typer.run(main)added:docs/src/options/prompt/tutorial002.py+12 added
1+import typer2+3+4+def main(5+ name: str,6+ lastname: str = typer.Option(..., prompt="Please tell me your last name"),7+):8+ typer.echo(f"Hello {name} {lastname}")9+10+11+if __name__ == "__main__":12+ typer.run(main)added:docs/src/options/prompt/tutorial003.py+9 added
1+import typer2+3+4+def main(project_name: str = typer.Option(..., prompt=True, confirmation_prompt=True)):5+ typer.echo(f"Deleting project {project_name}")6+7+8+if __name__ == "__main__":9+ typer.run(main)added:docs/src/options/required/tutorial001.py+9 added
1+import typer2+3+4+def main(name: str, lastname: str = typer.Option(...)):5+ typer.echo(f"Hello {name} {lastname}")6+7+8+if __name__ == "__main__":9+ typer.run(main)deleted:docs/src/options/tutorial001.py-21 removed
1-import typer2-3-4-def main(5- name: str,6- lastname: str = typer.Option("", help="Last name of person to greet."),7- formal: bool = typer.Option(False, help="Say hi formally."),8-):9- """10- Say hi to NAME, optionally with a --lastname.11-12- If --formal is used, say hi very formally.13- """14- if formal:15- typer.echo(f"Good day Ms. {name} {lastname}.")16- else:17- typer.echo(f"Hello {name} {lastname}")18-19-20-if __name__ == "__main__":21- typer.run(main)deleted:docs/src/options/tutorial002.py-9 removed
1-import typer2-3-4-def main(name: str, lastname: str = typer.Option(...)):5- typer.echo(f"Hello {name} {lastname}")6-7-8-if __name__ == "__main__":9- typer.run(main)deleted:docs/src/options/tutorial003.py-9 removed
1-import typer2-3-4-def main(name: str, lastname: str = typer.Option(..., prompt=True)):5- typer.echo(f"Hello {name} {lastname}")6-7-8-if __name__ == "__main__":9- typer.run(main)deleted:docs/src/options/tutorial004.py-12 removed
1-import typer2-3-4-def main(5- name: str,6- lastname: str = typer.Option(..., prompt="Please tell me your last name"),7-):8- typer.echo(f"Hello {name} {lastname}")9-10-11-if __name__ == "__main__":12- typer.run(main)deleted:docs/src/options/tutorial005.py-9 removed
1-import typer2-3-4-def main(project_name: str = typer.Option(..., prompt=True, confirmation_prompt=True)):5- typer.echo(f"Deleting project {project_name}")6-7-8-if __name__ == "__main__":9- typer.run(main)deleted:docs/src/options/tutorial006.py-9 removed
1-import typer2-3-4-def main(fullname: str = typer.Option("Wade Wilson", show_default=True)):5- typer.echo(f"Hello {fullname}")6-7-8-if __name__ == "__main__":9- typer.run(main)added:docs/tutorial/commands/arguments.md+39 added
1+The same way as with a CLI application with a single command, subcommands (or just "commands") can also have their own *CLI arguments*:2+3+```Python hl_lines="7 12"4+{!./src/commands/arguments/tutorial001.py!}5+```6+7+<div class="termy">8+9+```console10+// Check the help for create11+$ python main.py create --help12+13+Usage: main.py create [OPTIONS] USERNAME14+15+Options:16+ --help Show this message and exit.17+18+// Call it with a CLI argument19+$ python main.py create Camila20+21+Creating user: Camila22+23+// The same for delete24+$ python main.py delete Camila25+26+Deleting user: Camila27+```28+29+</div>30+31+!!! tip32+ Everything to the *right* of the *command* are *CLI parameters* (*CLI arguments* and *CLI options*) for that command.33+34+!!! note "Technical Details"35+ Actually, it's everything to the right of that command, *before any subcommand*.36+37+ It's possible to have groups of *subcommands*, it's like if one *command* also had *subcommands*. And then those *subcommands* could have their own *CLI parameters*, taking their own *CLI parameters*.38+39+ You will see about them later in another section.added:docs/tutorial/commands/help.md+120 added
1+The same as before, you can add help for the commands in the docstrings and the *CLI options*.2+3+And the `typer.Typer()` application receives a parameter `help` that you can pass with the main help text for your CLI program:4+5+```Python hl_lines="3 8 9 10 20 23 24 25 26 27 39 42 43 44 45 46 55 56 57"6+{!./src/commands/help/tutorial001.py!}7+```8+9+Check it:10+11+<div class="termy">12+13+```console14+// Check the new help15+$ python main.py --help16+17+Usage: main.py [OPTIONS] COMMAND [ARGS]...18+19+ Awesome CLI user manager.20+21+Options:22+ --install-completion Install completion for the current shell.23+ --show-completion Show completion for the current shell, to copy it or customize the installation.24+ --help Show this message and exit.25+26+Commands:27+ create Create a new user with USERNAME.28+ delete Delete a user with USERNAME.29+ delete-all Delete ALL users in the database.30+ init Initialize the users database.31+32+// Now the commands have inline help 🎉33+34+// Check the help for create35+$ python main.py create --help36+37+Usage: main.py create [OPTIONS] USERNAME38+39+ Create a new user with USERNAME.40+41+Options:42+ --help Show this message and exit.43+44+// Check the help for delete45+$ python main.py delete --help46+47+Usage: main.py delete [OPTIONS] USERNAME48+49+ Delete a user with USERNAME.50+51+ If --force is not used, will ask for confirmation.52+53+Options:54+ --force / --no-force Force deletion without confirmation. [required]55+ --help Show this message and exit.56+57+// Check the help for delete-all58+$ python main.py delete-all --help59+60+Usage: main.py delete-all [OPTIONS]61+62+ Delete ALL users in the database.63+64+ If --force is not used, will ask for confirmation.65+66+Options:67+ --force / --no-force Force deletion without confirmation. [required]68+ --help Show this message and exit.69+70+// Check the help for init71+$ python main.py init --help72+73+Usage: main.py init [OPTIONS]74+75+ Initialize the users database.76+77+Options:78+ --help Show this message and exit.79+```80+81+</div>82+83+!!! tip84+ `typer.Typer()` receives several other parameters for other things, we'll see that later.85+86+ You will also see how to use "Callbacks" later, and those include a way to add this same help message in a function docstring.87+88+## Overwrite command help89+90+You will probably be better adding the help text as a docstring to your functions, but if for some reason you wanted to overwrite it, you can use the `help` function argument passed to `@app.command()`:91+92+```Python hl_lines="6 14"93+{!./src/commands/help/tutorial002.py!}94+```95+96+Check it:97+98+<div class="termy">99+100+```console101+// Check the help102+$ python main.py --help103+104+// Notice it uses the help passed to @app.command()105+Usage: main.py [OPTIONS] COMMAND [ARGS]...106+107+Options:108+ --install-completion Install completion for the current shell.109+ --show-completion Show completion for the current shell, to copy110+ it or customize the installation.111+ --help Show this message and exit.112+113+Commands:114+ create Create a new user with USERNAME.115+ delete Delete a user with USERNAME.116+117+// It uses "Create a new user with USERNAME." instead of "Some internal utility function to create."118+```119+120+</div>added:docs/tutorial/commands/index.md+182 added
1+We have seen how to create a CLI program with possibly several *CLI Options* and *CLI Arguments*.2+3+But **Typer** allows you to create CLI programs with several commands (also known as subcommands).4+5+For example, the program `git` has several commands.6+7+One command of `git` is `git push`. And `git push` in turn takes its own *CLI arguments* and *CLI options*.8+9+For example:10+11+<div class="termy">12+13+```console14+// The push command with no parameters15+$ git push16+17+---> 100%18+19+// The push command with one CLI option --set-upstream and 2 CLI arguments20+$ git push --set-upstream origin master21+22+---> 100%23+```24+25+</div>26+27+Another command of `git` is `git pull`, it also has some *CLI parameters*.28+29+It's like if the same big program `git` had several small programs inside.30+31+!!! tip32+ A command looks the same as a *CLI argument*, it's just some name without a preceding `--`. But commands have predefined name, and are used to group different sets of functionalities into the same CLI application.33+34+## Command or subcommand35+36+It's common to call a CLI program a "command".37+38+But when one of these programs have subcommands, those subcommands are also frequently called just "commands".39+40+Have that in mind so you don't get confused.41+42+Here I'll use **CLI application** or **program** to refer to the program you are building in Python with Typer, and **command** to refer to one of these "subcommands" of your program.43+44+## Explicit application45+46+Before creating CLI applications with multiple commands/subcommands we need to understand how to create an explicit `typer.Typer()` application.47+48+In the *CLI Options* and *CLI Argument* tutorials you have seen how to create a single function and then pass that function to `typer.run()`.49+50+For example:51+52+```Python hl_lines="9"53+{!./src/first_steps/tutorial002.py!}54+```55+56+But that is actually a shortcut. Under the hood, **Typer** converts that to a CLI application with `typer.Typer()` and executes it. All that inside of `typer.run()`.57+58+There's also a more explicit way to achieve the same:59+60+```Python hl_lines="3 6 12"61+{!./src/commands/index/tutorial001.py!}62+```63+64+When you use `typer.run()`, **Typer** is doing more or less the same as above, it will:65+66+* Create a new `typer.Typer()` "application".67+* Create a new "`command`" with your function.68+* Call the same "application" as if it was a function with "`app()`".69+70+!!! info "`@decorator` Info"71+ That `@something` syntax in Python is called a "decorator".72+73+ You put it on top of a function. Like a pretty decorative hat (I guess that's where the term came from).74+75+ A "decorator" takes the function below and does something with it.76+77+ In our case, this decorator tells **Typer** that the function below is a "`command`".78+79+Both ways, with `typer.run()` and creating the explicit application, achieve the same.80+81+!!! tip82+ If your use case is solved with just `typer.run()`, that's fine, you don't have to create the explicit `app` and use `@app.command()`, etc.83+84+ You might want to do that later when your app needs the extra features, but if it doesn't need them yet, that's fine.85+86+If you run the second example, with the explicit `app`, it works exactly the same:87+88+<div class="termy">89+90+```console91+// Without a CLI argument92+$ python main.py93+94+Usage: main.py [OPTIONS] NAME95+Try "main.py --help" for help.96+97+Error: Missing argument "NAME".98+99+// With the NAME CLI argument100+$ python main.py Camila101+102+Hello Camila103+104+// Asking for help105+$ python main.py --help106+107+Usage: main.py [OPTIONS] NAME108+109+Options:110+ --install-completion Install completion for the current shell.111+ --show-completion Show completion for the current shell, to copy it or customize the installation.112+ --help Show this message and exit.113+```114+115+</div>116+117+## A CLI application with multiple commands118+119+Coming back to the CLI applications with multiple commands/subcommands, **Typer** allows creating CLI applications with multiple of them.120+121+Now that you know how to create an explicit `typer.Typer()` application and add one command, let's see how to add multiple commands.122+123+Let's say that we have a CLI application to manage users.124+125+We'll have a command to `create` users and another command to `delete` them.126+127+To begin, let's say it can only create and delete one single predefined user:128+129+```Python hl_lines="6 11"130+{!./src/commands/index/tutorial002.py!}131+```132+133+Now we have a CLI application with 2 commands, `create` and `delete`:134+135+<div class="termy">136+137+```console138+// Check the help139+$ python main.py --help140+141+Usage: main.py [OPTIONS] COMMAND [ARGS]...142+143+Options:144+ --install-completion Install completion for the current shell.145+ --show-completion Show completion for the current shell, to copy it or customize the installation.146+ --help Show this message and exit.147+148+Commands:149+ create150+ delete151+152+// Test them153+$ python main.py create154+155+Creating user: Hiro Hamada156+157+$ python main.py delete158+159+Deleting user: Hiro Hamada160+161+// Now we have 2 commands! 🎉162+```163+164+</div>165+166+Notice that the help text now shows the 2 commands: `create` and `delete`.167+168+!!! tip169+ By default, the names of the commands are generated from the function name.170+171+## Decorator Technical Details172+173+When you use `@app.command()` the function under the decorator is registered in the **Typer** application and is then used later by the application.174+175+But Typer doesn't modify that function itself, the function is left as is.176+177+That means that if your function is simple enough that you could create it without using `typer.Option()` or `typer.Argument()`, you could use the same function for a **Typer** application and a **FastAPI** application putting both decorators on top, or similar tricks.178+179+!!! note "Click Technical Details"180+ This behavior is a design difference with Click.181+182+ In Click, when you add a `@click.command()` decorator it actually modifies the function underneath and replaces it with an object.added:docs/tutorial/commands/name.md+46 added
1+By default, the command names are generated from the function name.2+3+So, if your function is something like:4+5+```Python6+def create(username: str):7+ ...8+```9+10+Then the command name will be `create`.11+12+But if you already had a function called `create()` somewhere in your code, you would have to name your CLI function differently.13+14+And what if you wanted the command to still be named `create`?15+16+For this, you can set the name of the command in the first parameter for the `@app.command()` decorator:17+18+```Python hl_lines="6 11"19+{!./src/commands/name/tutorial001.py!}20+```21+22+Now, even though the functions are named `cli_create_user()` and `cli_delete_user()`, the commands will still be named `create` and `delete`:23+24+<div class="termy">25+26+```console27+$ python main.py --help28+29+Usage: main.py [OPTIONS] COMMAND [ARGS]...30+31+Options:32+ --install-completion Install completion for the current shell.33+ --show-completion Show completion for the current shell, to copy it or customize the installation.34+ --help Show this message and exit.35+36+Commands:37+ create38+ delete39+40+// Test it41+$ python main.py create Camila42+43+Creating user: Camila44+```45+46+</div>added:docs/tutorial/commands/one-or-multiple.md+91 added
1+You might have noticed that if you create a single command, as in the first example:2+3+```Python hl_lines="3 6 12"4+{!./src/commands/tutorial001.py!}5+```6+7+**Typer** is smart enough to create a CLI application with that single function as the main CLI application, not as a command/subcommand:8+9+<div class="termy">10+11+```console12+// Without a CLI argument13+$ python main.py14+15+Usage: main.py [OPTIONS] NAME16+Try "main.py --help" for help.17+18+Error: Missing argument "NAME".19+20+// With the NAME CLI argument21+$ python main.py Camila22+23+Hello Camila24+25+// Asking for help26+$ python main.py27+28+Usage: main.py [OPTIONS] NAME29+30+Options:31+ --install-completion Install completion for the current shell.32+ --show-completion Show completion for the current shell, to copy it or customize the installation.33+ --help Show this message and exit.34+```35+36+</div>37+38+!!! tip39+ Notice that it doesn't show a command `main`, even though the function name is `main`.40+41+But if you add multiple commands, **Typer** will create one *CLI command* for each one of them:42+43+```Python hl_lines="6 11"44+{!./src/commands/tutorial002.py!}45+```46+47+Here we have 2 commands `create` and `delete`:48+49+<div class="termy">50+51+```console52+// Check the help53+$ python main.py --help54+55+Usage: main.py [OPTIONS] COMMAND [ARGS]...56+57+Options:58+ --install-completion Install completion for the current shell.59+ --show-completion Show completion for the current shell, to copy it or customize the installation.60+ --help Show this message and exit.61+62+Commands:63+ create64+ delete65+66+// Test the commands67+$ python main.py create68+69+Creating user: Hiro Hamada70+71+$ python main.py delete72+73+Deleting user: Hiro Hamada74+```75+76+</div>77+78+!!! tip79+ This is **Typer**'s behavior by default, but you could still create a CLI application with one single command, like:80+81+ ```82+ $ python main.py main83+ ```84+85+ instead of just:86+87+ ```88+ $ python main.py89+ ```90+91+ You will learn more about this in the section about application Callbacks.added:docs/tutorial/commands/options.md+96 added
1+Commands can also have their own *CLI options*.2+3+In fact, each command can have different *CLI arguments* and *CLI options*:4+5+```Python hl_lines="7 13 14 24 33"6+{!./src/commands/options/tutorial001.py!}7+```8+9+Here we have multiple commands, with different *CLI parameters*:10+11+* `create`:12+ * `username`: a *CLI argument*.13+* `delete`:14+ * `username`: a *CLI argument*.15+ * `--force`: a *CLI option*, if not provided, it's prompted.16+* `delete-all`:17+ * `--force`: a *CLI option*, if not provided, it's prompted.18+* `init`:19+ * Doesn't take any *CLI parameters*.20+21+<div class="termy">22+23+```console24+// Check the help25+python main.py --help26+27+Usage: main.py [OPTIONS] COMMAND [ARGS]...28+29+Options:30+ --install-completion Install completion for the current shell.31+ --show-completion Show completion for the current shell, to copy it or customize the installation.32+ --help Show this message and exit.33+34+Commands:35+ create36+ delete37+ delete-all38+ info39+```40+41+</div>42+43+!!! tip44+ Check the command `delete-all`, by default command names are generated from the function name, replacing `_` with `-`.45+46+Test it:47+48+<div class="termy">49+50+```console51+// Check the command create52+$ python main.py create Camila53+54+Creating user: Camila55+56+// Now test the command delete57+$ python main.py delete Camila58+59+# Are you sure you want to delete the user? [y/N]: $ y60+61+Deleting user: Camila62+63+$ python main.py delete Wade64+65+# Are you sure you want to delete the user? [y/N]: $ n66+67+Operation cancelled68+69+// And finally, the command delete-all70+// Notice it doesn't have CLI arguments, only a CLI option71+72+$ python main.py delete-all73+74+# Are you sure you want to delete ALL users? [y/N]: $ y75+76+Deleting all users77+78+$ python main.py delete-all79+80+# Are you sure you want to delete ALL users? [y/N]: $ n81+82+Operation cancelled83+84+// And if you pass the --force CLI option, it doesn't need to confirm85+86+$ python main.py delete-all --force87+88+Deleting all users89+90+// And init that doesn't take any CLI parameter91+$ python main.py init92+93+Initializing user database94+```95+96+</div>added:docs/tutorial/options/help.md+100 added
1+In the *First Steps* section you saw how to add help for a CLI app/command by adding it to a function's <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr>.2+3+Here's how that last example looked like:4+5+```Python6+{!./src/first_steps/tutorial006.py!}7+```8+9+Now we'll add a *help* section to the *CLI options*:10+11+```Python hl_lines="6 7"12+{!./src/options/help/tutorial001.py!}13+```14+15+We are replacing the default values we had before with `typer.Option()`.16+17+As we no longer have a default value there, the first parameter to `typer.Option()` serves the same purpose of defining that default value.18+19+So, if we had:20+21+```Python22+lastname: str = ""23+```24+25+now we write:26+27+```Python28+lastname: str = typer.Option("")29+```30+31+And both forms achieve the same: a *CLI option* with a default value of an empty string (`""`).32+33+And then we can pass the `help` keyword parameter:34+35+```Python36+lastname: str = typer.Option("", help="this option does this and that")37+```38+39+to create the help for that *CLI option*.40+41+Copy that example from above to a file `main.py`.42+43+Test it:44+45+<div class="termy">46+47+```console48+$ python main.py --help49+50+Usage: main.py [OPTIONS] NAME51+52+ Say hi to NAME, optionally with a --lastname.53+54+ If --formal is used, say hi very formally.55+56+Options:57+ --lastname TEXT Last name of person to greet.58+ --formal / --no-formal Say hi formally.59+ --install-completion Install completion for the current shell.60+ --show-completion Show completion for the current shell, to copy it or customize the installation.61+ --help Show this message and exit.62+63+// Now you have a help text for the --lastname and --formal CLI options 🎉64+```65+66+</div>67+68+## Show default in help69+70+You can tell Typer to show the default value in the help text with `show_default=True`:71+72+```Python hl_lines="4"73+{!./src/options/help/tutorial002.py!}74+```75+76+And it will show up in the help text:77+78+<div class="termy">79+80+```console81+$ python main.py82+83+Hello Wade Wilson84+85+// Show the help86+$ python main.py --help87+88+Usage: main.py [OPTIONS]89+90+Options:91+ --fullname TEXT [default: Wade Wilson]92+ --install-completion Install completion for the current shell.93+ --show-completion Show completion for the current shell, to copy it or customize the installation.94+ --help Show this message and exit.95+```96+97+</div>98+99+!!! tip100+ Notice the `[default: Wade Wilson]` in the help text.added:docs/tutorial/options/index.md+3 added
1+In the next short sections we will see how to modify *CLI options* using `typer.Option()`.2+3+`typer.Option()` works very similarly to `typer.Argument()`, but has some extra features that we'll see next.added:docs/tutorial/options/name.md+328 added
1+By default **Typer** will create a *CLI option* name from the function parameter.2+3+So, if you have a function with:4+5+```Python6+def main(user_name: str = None):7+ pass8+```9+10+or11+12+```Python13+def main(user_name: str = typer.Option(None)):14+ pass15+```16+17+**Typer** will create a *CLI option*:18+19+```20+--user-name21+```22+23+But you can customize it if you want to.24+25+Let's say the function parameter name is `user_name` as above, but you want the *CLI option* to be just `--name`.26+27+You can pass the *CLI option* name that you want to have in the next positional argument passed to `typer.Option()`:28+29+```Python hl_lines="4"30+{!./src/options/name/tutorial001.py!}31+```32+33+Here you are passing the string `"--name"` as the second positional argument to `typer.Option()`.34+35+!!! info36+ "<a href="https://docs.python.org/3.8/glossary.html#term-argument" target="_blank">Positional</a>" means that it's not a function argument with a keyword name.37+ 38+ For example `show_default=True` is a keyword argument. "`show_default`" is the keyword.39+40+ But in `"--name"` there's no `option_name="--name"` or something similar, it's just the string value `"--name"` that goes in `typer.Option()` after the `...` value passed in the first position.41+42+ That's a "positional argument" in a function.43+44+Check it:45+46+<div class="termy">47+48+```console49+$ python main.py --help50+51+// Notice the --name instead of --user-name52+Usage: main.py [OPTIONS]53+54+Options:55+ --name TEXT [required]56+ --install-completion Install completion for the current shell.57+ --show-completion Show completion for the current shell, to copy it or customize the installation.58+ --help Show this message and exit.59+60+// Try it61+$ python --name Camila62+63+Hello Camila64+```65+66+</div>67+68+## *CLI option* short names69+70+A short name is a *CLI option* name with a single dash (`-`) instead of 2 (`--`) and a single letter, like `-n` instead of `--name`.71+72+For example, the `ls` program has a *CLI option* named `--size`, and the same *CLI option* also has a short name `-s`:73+74+<div class="termy">75+76+```console77+// With the long name --size78+$ ls ./myproject --size79+80+12 first-steps.md 4 intro.md81+82+// With the short name -s83+$ ls ./myproject -s84+85+12 first-steps.md 4 intro.md86+87+// Both CLI option names do the same88+```89+90+</div>91+92+### *CLI option* short names together93+94+Short names have another feature, when they have a single letter, as in `-s`, you can put several of these *CLI options* together, with a single dash.95+96+For example, the `ls` program has these 2 *CLI options* (among others):97+98+* `--size`: show the sizes of the listed files.99+* `--human`: show a human-readable format, like `1MB` instead of just `1024`.100+101+And these 2 *CLI options* have short versions too:102+103+* `--size`: short version `-s`.104+* `--human`: short version `-h`.105+106+So, you can put them together with `-sh` or `-hs`:107+108+<div class="termy">109+110+```console111+// Call ls with long CLI options112+$ ls --size --human113+114+12K first-steps.md 4.0K intro.md115+116+// Now with short versions117+$ ls -s -h118+119+12K first-steps.md 4.0K intro.md120+121+// And with short versions together122+$ ls -sh123+124+12K first-steps.md 4.0K intro.md125+126+// Order in short versions doesn't matter127+$ ls -hs128+129+12K first-steps.md 4.0K intro.md130+131+// They all work the same 🎉132+```133+134+</div>135+136+### *CLI option* short names with values137+138+When you use *CLI options* with short names, you can put them together if they are just boolean flags, like `--size` or `--human`.139+140+But if you have a *CLI option* `--file` with a short name `-f` that takes a value, if you put it with other short names for *CLI options*, you have to put it as the last letter, so that it can receive the value that comes right after.141+142+For example, let's say you are decompressing/extracting a file `myproject.tar.gz` with the program `tar`.143+144+You can pass these *CLI option* short names to `tar`:145+146+* `-x`: means "e`X`tract", to decompress and extract the contents.147+* `-v`: means "`V`erbose", to print on the screen what it is doing, so you can know that it's decompressing each file and can entertain yourself while you wait.148+* `-f`: means "`F`ile", this one requires a value, the compressed file to extract (in our example, this is `myproject.tar.gz`).149+ * So if you use all the short names together, this `-f` has to come last, to receive the value that comes next to it.150+151+For example:152+153+<div class="termy">154+155+```console156+$ tar -xvf myproject.tar.gz157+158+myproject/159+myproject/first-steps.md160+myproject/intro.md161+162+// But if you put the -f before163+$ tar -fxv myproject.tar.gz164+165+// You get an ugly error166+tar: You must specify one of the blah, blah, error, error167+```168+169+</div>170+171+### Defining *CLI option* short names172+173+In **Typer** you can also define *CLI option* short names the same way you can customize the long names.174+175+`typer.Option()` receives as a first function argument the default value, e.g. `None`, and all the next *positional* values are to define the *CLI option* name(s).176+177+!!! tip178+ Remember the *positional* function arguments are those that don't have a keyword.179+180+ All the other function arguments/parameters you pass to `typer.Option()` like `prompt=True` and `help="This option blah, blah"` require the keyword.181+182+You can overwrite the *CLI option* name to use as in the previous example, but you can also declare extra alternatives, including short names.183+184+For example, extending the previous example, let's add a *CLI option* short name `-n`:185+186+```Python hl_lines="4"187+{!./src/options/name/tutorial002.py!}188+```189+190+Here we are overwriting the *CLI option* name that by default would be `--user-name`, and we are defining it to be `--name`. And we are also declaring a *CLI option* short name of `-n`.191+192+Check it:193+194+<div class="termy">195+196+```console197+// Check the help198+$ python main.py --help199+200+// Notice the two CLI option names -n and --name201+Usage: main.py [OPTIONS]202+203+Options:204+ -n, --name TEXT [required]205+ --install-completion Install completion for the current shell.206+ --show-completion Show completion for the current shell, to copy it or customize the installation.207+ --help Show this message and exit.208+209+// Try the short version210+$ python main.py -n Camila211+212+Hello Camila213+```214+215+</div>216+217+### *CLI option* only short name218+219+If you only declare a short name like `-n` then that will be the only *CLI option* name. And neither `--name` nor `--user-name` will be available.220+221+```Python hl_lines="4"222+{!./src/options/name/tutorial003.py!}223+```224+225+Check it:226+227+<div class="termy">228+229+```console230+$ python main.py --help231+232+// Notice there's no --name nor --user-name, only -n233+Usage: main.py [OPTIONS]234+235+Options:236+ -n TEXT [required]237+ --install-completion Install completion for the current shell.238+ --show-completion Show completion for the current shell, to copy it or customize the installation.239+ --help Show this message and exit.240+241+// Try it242+$ python main.py -n Camila243+244+Hello Camila245+```246+247+</div>248+249+### *CLI option* short name and default250+251+Continuing with the example above, as **Typer** allows you to declare a *CLI option* as having only a short name, if you want to have the default long name plus a short name, you have to declare both explicitly:252+253+```Python hl_lines="4"254+{!./src/options/name/tutorial004.py!}255+```256+257+Check it:258+259+<div class="termy">260+261+```console262+$ python main.py --help263+264+// Notice that we have the long version --user-name back265+// and we also have the short version -n266+Usage: main.py [OPTIONS]267+268+Options:269+ -n, --user-name TEXT [required]270+ --install-completion Install completion for the current shell.271+ --show-completion Show completion for the current shell, to copy it or customize the installation.272+ --help Show this message and exit.273+274+// Try it275+$ python main.py --user-name Camila276+277+Hello Camila278+279+// And try the short version280+$ python main.py -n Camila281+```282+283+</div>284+285+### *CLI option* short names together286+287+You can create multiple short names and use them together.288+289+You don't have to do anything special for it to work (apart from declaring those short versions):290+291+```Python hl_lines="5 6"292+{!./src/options/name/tutorial005.py!}293+```294+295+!!! tip296+ Notice that, again, we are declaring the long and short version of the *CLI option* names.297+298+Check it:299+300+<div class="termy">301+302+```console303+$ python main.py --help304+305+// We now have short versions -n and -f306+// And also long versions --name and --formal307+Usage: main.py [OPTIONS]308+309+Options:310+ -n, --name TEXT [required]311+ -f, --formal312+ --install-completion Install completion for the current shell.313+ --show-completion Show completion for the current shell, to copy it or customize the installation.314+ --help Show this message and exit.315+316+// Try the short versions317+$ python main.py -n Camila -f318+319+Good day Ms. Camila.320+321+// And try the 2 short versions together322+// See how -n has to go last, to be able to get the value323+$ python main.py -fn Camila324+325+Good day Ms. Camila.326+```327+328+</div>added:docs/tutorial/options/prompt.md+88 added
1+It's also possible to, instead of just showing an error, ask for the missing value with `prompt=True`:2+3+```Python hl_lines="4"4+{!./src/options/prompt/tutorial001.py!}5+```6+7+And then your program will ask the user for it in the terminal:8+9+<div class="termy">10+11+```console12+// Call it with the NAME CLI argument13+$ python main.py Camila14+15+// It asks for the missing CLI option --lastname16+# Lastname: $ Gutiérrez17+18+Hello Camila Gutiérrez19+```20+21+</div>22+23+## Customize the prompt24+25+You can also set a custom prompt, passing the string that you want to use instead of just `True`:26+27+```Python hl_lines="6"28+{!./src/options/prompt/tutorial002.py!}29+```30+31+And then your program will ask for it using with your custom prompt:32+33+<div class="termy">34+35+```console36+// Call it with the NAME CLI argument37+$ python main.py Camila38+39+// It uses the custom prompt40+# Please tell me your last name: $ Gutiérrez41+42+Hello Camila Gutiérrez43+```44+45+</div>46+47+## Confirmation prompt48+49+In some cases you could want to prompt for something and then ask the user to confirm it by typing it twice.50+51+You can do it passing the parameter `confirmation_prompt=True`.52+53+Let's say it's a CLI app to delete a project:54+55+```Python hl_lines="4"56+{!./src/options/prompt/tutorial003.py!}57+```58+59+And it will prompt the user for a value and then for the confirmation:60+61+<div class="termy">62+63+```console64+$ python main.py65+66+// Your app will first prompt for the project name, and then for the confirmation67+# Project name: $ Old Project68+# Repeat for confirmation: $ Old Project69+70+Deleting project Old Project71+72+// If the user doesn't type the same, receives an error and a new prompt73+$ python main.py74+75+# Project name: $ Old Project76+# Repeat for confirmation: $ New Spice77+78+Error: the two entered values do not match79+80+# Project name: $ Old Project81+# Repeat for confirmation: $ Old Project82+83+Deleting project Old Project84+85+// Now it works 🎉86+```87+88+</div>added:docs/tutorial/options/required.md+56 added
1+We said before that *by default*:2+3+* *CLI options* are **optional**4+* *CLI arguments* are **required**5+6+Well, that's how they work *by default*, and that's the convention in many CLI programs and systems.7+8+But if you really want, you can change that.9+10+To make a *CLI option* required, pass `...` to `typer.Option()`.11+12+!!! info13+ 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>.14+15+That will tell **Typer** that it's still a *CLI option*, but it doesn't have a default value, and it's required.16+17+Let's make `--lastname` a required *CLI option*:18+19+```Python hl_lines="4"20+{!./src/options/required/tutorial001.py!}21+```22+23+And test it:24+25+<div class="termy">26+27+```console28+// Pass the NAME CLI argument29+$ python main.py Camila30+31+// We didn't pass the now required --lastname CLI option32+Usage: main.py [OPTIONS] NAME33+Try "main.py --help" for help.34+35+Error: Missing option "--lastname".36+37+// Now update it to pass the required --lastname CLI option38+$ python main.py Camila --lastname Gutiérrez39+40+Hello Camila Gutiérrez41+42+// And if you check the help43+$ python main.py --help44+45+Usage: main.py [OPTIONS] NAME46+47+Options:48+ --lastname TEXT [required]49+ --install-completion Install completion for the current shell.50+ --show-completion Show completion for the current shell, to copy it or customize the installation.51+ --help Show this message and exit.52+53+// It now tells you that --lastname is required 🎉54+```55+56+</div>modified:docs/tutorial/arguments.md+5 added-10 removed
1-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*.1+Let's see how to configure *CLI arguments* with `typer.Argument()`.2 3 ## Optional *CLI arguments*4 5 We said before that *by default*:56 unmodified lines62 ```Python63 name: str = typer.Argument(...)64 ```65 66-The same as with `typer.Option()`, there is a `typer.Argument()`.66+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).67 68-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).68+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.69 70-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.70+To make it *required*, we pass `...` as the first function argument passed to `typer.Argument(...)`.71 72-To make it *required*, we pass `...` as that first parameter to the function.73-72 !!! info73 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>.74 77-!!! tip78- This works exactly the same way `typer.Option()` does.79-75 All we did there achieves the same thing as before, a **required** *CLI argument*:76 77 <div class="termy">78 65 unmodified lines144 145 Hello World!146 147 // With one optional CLI argument153-$ python main.py148+$ python main.py Camila149 150 Hello Camila151 ```152 56 unmodified linesdeleted:docs/tutorial/commands.md-555 removed
1-We have seen how to create a CLI program with possibly several *CLI Options* and *CLI Arguments*.2-3-But **Typer** allows you to create CLI programs with several commands (also known as subcommands).4-5-For example, the program `git` has several commands.6-7-One command of `git` is `git push`. And `git push` in turn takes its own *CLI arguments* and *CLI options*.8-9-For example:10-11-<div class="termy">12-13-```console14-// The push command with no parameters15-$ git push16-17----> 100%18-19-// The push command with one CLI option --set-upstream and 2 CLI arguments20-$ git push --set-upstream origin master21-22----> 100%23-```24-25-</div>26-27-Another command of `git` is `git pull`, it also has some *CLI parameters*.28-29-It's like if the same big program `git` had several small programs inside.30-31-!!! tip32- A command looks the same as a *CLI argument*, it's just some name without a preceding `--`. But commands have predefined name, and are used to group different sets of functionalities into the same CLI application.33-34-## Command or subcommand35-36-It's common to call a CLI program a "command".37-38-But when one of these programs have subcommands, those subcommands are also frequently called just "commands".39-40-Have that in mind so you don't get confused.41-42-Here I'll use **CLI application** or **program** to refer to the program you are building in Python with Typer, and **command** to refer to one of these "subcommands" of your program.43-44-## Explicit application45-46-Before creating CLI applications with multiple commands/subcommands we need to understand how to create an explicit `typer.Typer()` application.47-48-In the *CLI Options* and *CLI Argument* tutorials you have seen how to create a single function and then pass that function to `typer.run()`.49-50-For example:51-52-```Python hl_lines="9"53-{!./src/first_steps/tutorial002.py!}54-```55-56-But that is actually a shortcut. Under the hood, **Typer** converts that to a CLI application with `typer.Typer()` and executes it. All that inside of `typer.run()`.57-58-There's also a more explicit way to achieve the same:59-60-```Python hl_lines="3 6 12"61-{!./src/commands/tutorial001.py!}62-```63-64-When you use `typer.run()`, **Typer** is doing more or less the same as above, it will:65-66-* Create a new `typer.Typer()` "application".67-* Create a new "`command`" with your function.68-* Call the same "application" as if it was a function with "`app()`".69-70-!!! info "`@decorator` Info"71- That `@something` syntax in Python is called a "decorator".72-73- You put it on top of a function. Like a pretty decorative hat (I guess that's where the term came from).74-75- A "decorator" takes the function below and does something with it.76-77- In our case, this decorator tells **Typer** that the function below is a "`command`".78-79-Both ways, with `typer.run()` and creating the explicit application, achieve the same.80-81-!!! tip82- If your use case is solved with just `typer.run()`, that's fine, you don't have to create the explicit `app` and use `@app.command()`, etc.83-84- You might want to do that later when your app needs the extra features, but if it doesn't need them yet, that's fine.85-86-If you run the second example, with the explicit `app`, it works exactly the same:87-88-<div class="termy">89-90-```console91-// Without a CLI argument92-$ python main.py93-94-Usage: main.py [OPTIONS] NAME95-Try "main.py --help" for help.96-97-Error: Missing argument "NAME".98-99-// With the NAME CLI argument100-$ python main.py Camila101-102-Hello Camila103-104-// Asking for help105-$ python main.py --help106-107-Usage: main.py [OPTIONS] NAME108-109-Options:110- --install-completion Install completion for the current shell.111- --show-completion Show completion for the current shell, to copy it or customize the installation.112- --help Show this message and exit.113-```114-115-</div>116-117-## A CLI application with multiple commands118-119-Coming back to the CLI applications with multiple commands/subcommands, **Typer** allows creating CLI applications with multiple of them.120-121-Now that you know how to create an explicit `typer.Typer()` application and add one command, let's see how to add multiple commands.122-123-Let's say that we have a CLI application to manage users.124-125-We'll have a command to `create` users and another command to `delete` them.126-127-To begin, let's say it can only create and delete one single predefined user:128-129-```Python hl_lines="6 11"130-{!./src/commands/tutorial002.py!}131-```132-133-Now we have a CLI application with 2 commands, `create` and `delete`:134-135-<div class="termy">136-137-```console138-// Check the help139-$ python main.py --help140-141-Usage: main.py [OPTIONS] COMMAND [ARGS]...142-143-Options:144- --install-completion Install completion for the current shell.145- --show-completion Show completion for the current shell, to copy it or customize the installation.146- --help Show this message and exit.147-148-Commands:149- create150- delete151-152-// Test them153-$ python main.py create154-155-Creating user: Hiro Hamada156-157-$ python main.py delete158-159-Deleting user: Hiro Hamada160-161-// Now we have 2 commands! 🎉162-```163-164-</div>165-166-Notice that the help text now shows the 2 commands: `create` and `delete`.167-168-!!! tip169- By default, the names of the commands are generated from the function name.170-171-## Command *CLI arguments*172-173-The same way as with a CLI application with a single command, subcommands (or just "commands") can also have their own *CLI arguments*:174-175-```Python hl_lines="7 12"176-{!./src/commands/tutorial003.py!}177-```178-179-<div class="termy">180-181-```console182-// Check the help for create183-$ python main.py create --help184-185-Usage: main.py create [OPTIONS] USERNAME186-187-Options:188- --help Show this message and exit.189-190-// Call it with a CLI argument191-$ python main.py create Camila192-193-Creating user: Camila194-195-// The same for delete196-$ python main.py delete Camila197-198-Deleting user: Camila199-```200-201-</div>202-203-!!! tip204- Everything to the *right* of the *command* are *CLI parameters* (*CLI arguments* and *CLI options*) for that command.205-206-!!! note "Technical Details"207- Actually, it's everything to the right of that command, *before any subcommand*.208-209- It's possible to have groups of *subcommands*, it's like if one *command* also had *subcommands*. And then those *subcommands* could have their own *CLI parameters*, taking their own *CLI parameters*.210-211- You will see about them later in another section.212-213-## Command *CLI options*214-215-Commands can also have their own *CLI options*.216-217-In fact, each command can have different *CLI arguments* and *CLI options*:218-219-```Python hl_lines="7 13 14 24 33"220-{!./src/commands/tutorial004.py!}221-```222-223-Here we have multiple commands, with different *CLI parameters*:224-225-* `create`:226- * `username`: a *CLI argument*.227-* `delete`:228- * `username`: a *CLI argument*.229- * `--force`: a *CLI option*, if not provided, it's prompted.230-* `delete-all`:231- * `--force`: a *CLI option*, if not provided, it's prompted.232-* `init`:233- * Doesn't take any *CLI parameters*.234-235-<div class="termy">236-237-```console238-// Check the help239-python main.py --help240-241-Usage: main.py [OPTIONS] COMMAND [ARGS]...242-243-Options:244- --install-completion Install completion for the current shell.245- --show-completion Show completion for the current shell, to copy it or customize the installation.246- --help Show this message and exit.247-248-Commands:249- create250- delete251- delete-all252- info253-```254-255-</div>256-257-!!! tip258- Check the command `delete-all`, by default command names are generated from the function name, replacing `_` with `-`.259-260-Test it:261-262-<div class="termy">263-264-```console265-// Check the command create266-$ python main.py create Camila267-268-Creating user: Camila269-270-// Now test the command delete271-$ python main.py delete Camila272-273-# Are you sure you want to delete the user? [y/N]: $ y274-275-Deleting user: Camila276-277-$ python main.py delete Wade278-279-# Are you sure you want to delete the user? [y/N]: $ n280-281-Operation cancelled282-283-// And finally, the command delete-all284-// Notice it doesn't have CLI arguments, only a CLI option285-286-$ python main.py delete-all287-288-# Are you sure you want to delete ALL users? [y/N]: $ y289-290-Deleting all users291-292-$ python main.py delete-all293-294-# Are you sure you want to delete ALL users? [y/N]: $ n295-296-Operation cancelled297-298-// And if you pass the --force CLI option, it doesn't need to confirm299-300-$ python main.py delete-all --force301-302-Deleting all users303-304-// And init that doesn't take any CLI parameter305-$ python main.py init306-307-Initializing user database308-```309-310-</div>311-312-## Command help313-314-The same as before, you can add help for the commands in the docstrings and the *CLI options*.315-316-And the `typer.Typer()` application receives a parameter `help` that you can pass with the main help text for your CLI program:317-318-```Python hl_lines="3 8 9 10 20 23 24 25 26 27 39 42 43 44 45 46 55 56 57"319-{!./src/commands/tutorial005.py!}320-```321-322-Check it:323-324-<div class="termy">325-326-```console327-// Check the new help328-$ python main.py --help329-330-Usage: main.py [OPTIONS] COMMAND [ARGS]...331-332- Awesome CLI user manager.333-334-Options:335- --install-completion Install completion for the current shell.336- --show-completion Show completion for the current shell, to copy it or customize the installation.337- --help Show this message and exit.338-339-Commands:340- create Create a new user with USERNAME.341- delete Delete a user with USERNAME.342- delete-all Delete ALL users in the database.343- init Initialize the users database.344-345-// Now the commands have inline help 🎉346-347-// Check the help for create348-$ python main.py create --help349-350-Usage: main.py create [OPTIONS] USERNAME351-352- Create a new user with USERNAME.353-354-Options:355- --help Show this message and exit.356-357-// Check the help for delete358-$ python main.py delete --help359-360-Usage: main.py delete [OPTIONS] USERNAME361-362- Delete a user with USERNAME.363-364- If --force is not used, will ask for confirmation.365-366-Options:367- --force / --no-force Force deletion without confirmation. [required]368- --help Show this message and exit.369-370-// Check the help for delete-all371-$ python main.py delete-all --help372-373-Usage: main.py delete-all [OPTIONS]374-375- Delete ALL users in the database.376-377- If --force is not used, will ask for confirmation.378-379-Options:380- --force / --no-force Force deletion without confirmation. [required]381- --help Show this message and exit.382-383-// Check the help for init384-$ python main.py init --help385-386-Usage: main.py init [OPTIONS]387-388- Initialize the users database.389-390-Options:391- --help Show this message and exit.392-```393-394-</div>395-396-!!! tip397- `typer.Typer()` receives several other parameters for other things, we'll see that later.398-399- You will also see how to use "Callbacks" later, and those include a way to add this same help message in a function docstring.400-401-## Custom command name402-403-By default, the command names are generated from the function name.404-405-So, if your function is something like:406-407-```Python408-def create(username: str):409- ...410-```411-412-Then the command name will be `create`.413-414-But if you already had a function called `create()` somewhere in your code, you would have to name your CLI function differently.415-416-And what if you wanted the command to still be named `create`?417-418-For this, you can set the name of the command in the first parameter for the `@app.command()` decorator:419-420-```Python hl_lines="6 11"421-{!./src/commands/tutorial006.py!}422-```423-424-Now, even though the functions are named `cli_create_user()` and `cli_delete_user()`, the commands will still be named `create` and `delete`:425-426-<div class="termy">427-428-```console429-$ python main.py --help430-431-Usage: main.py [OPTIONS] COMMAND [ARGS]...432-433-Options:434- --install-completion Install completion for the current shell.435- --show-completion Show completion for the current shell, to copy it or customize the installation.436- --help Show this message and exit.437-438-Commands:439- create440- delete441-442-// Test it443-$ python main.py create Camila444-445-Creating user: Camila446-```447-448-</div>449-450-## One command vs multiple commands451-452-You might have noticed that if you create a single command, as in the first example:453-454-```Python hl_lines="3 6 12"455-{!./src/commands/tutorial001.py!}456-```457-458-**Typer** is smart enough to create a CLI application with that single function as the main CLI application, not as a command/subcommand:459-460-<div class="termy">461-462-```console463-// Without a CLI argument464-$ python main.py465-466-Usage: main.py [OPTIONS] NAME467-Try "main.py --help" for help.468-469-Error: Missing argument "NAME".470-471-// With the NAME CLI argument472-$ python main.py Camila473-474-Hello Camila475-476-// Asking for help477-$ python main.py478-479-Usage: main.py [OPTIONS] NAME480-481-Options:482- --install-completion Install completion for the current shell.483- --show-completion Show completion for the current shell, to copy it or customize the installation.484- --help Show this message and exit.485-```486-487-</div>488-489-!!! tip490- Notice that it doesn't show a command `main`, even though the function name is `main`.491-492-But if you add multiple commands, **Typer** will create one *CLI command* for each one of them:493-494-```Python hl_lines="6 11"495-{!./src/commands/tutorial002.py!}496-```497-498-Here we have 2 commands `create` and `delete`:499-500-<div class="termy">501-502-```console503-// Check the help504-$ python main.py --help505-506-Usage: main.py [OPTIONS] COMMAND [ARGS]...507-508-Options:509- --install-completion Install completion for the current shell.510- --show-completion Show completion for the current shell, to copy it or customize the installation.511- --help Show this message and exit.512-513-Commands:514- create515- delete516-517-// Test the commands518-$ python main.py create519-520-Creating user: Hiro Hamada521-522-$ python main.py delete523-524-Deleting user: Hiro Hamada525-```526-527-</div>528-529-!!! tip530- This is **Typer**'s behavior by default, but you could still create a CLI application with one single command, like:531-532- ```533- $ python main.py main534- ```535-536- instead of just:537-538- ```539- $ python main.py540- ```541-542- You will learn more about this in the section about application Callbacks.543-544-## Decorator Technical Details545-546-When you use `@app.command()` the function under the decorator is registered in the **Typer** application and is then used later by the application.547-548-But Typer doesn't modify that function itself, the function is left as is.549-550-That means that if your function is simple enough that you could create it without using `typer.Option()` or `typer.Argument()`, you could use the same function for a **Typer** application and a **FastAPI** application putting both decorators on top, or similar tricks.551-552-!!! note "Click Technical Details"553- This behavior is a design difference with Click.554-555- In Click, when you add a `@click.command()` decorator it actually modifies the function underneath and replaces it with an object.deleted:docs/tutorial/options.md-263 removed
1-## *CLI options* with help2-3-In the *First Steps* section you saw how to add help for a CLI app/command by adding it to a function's <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr>.4-5-Here's how that last example looked like:6-7-```Python8-{!./src/first_steps/tutorial006.py!}9-```10-11-Now we'll add a *help* section to the *CLI options*:12-13-```Python hl_lines="6 7"14-{!./src/options/tutorial001.py!}15-```16-17-We are replacing the default values we had before with `typer.Option()`.18-19-As we no longer have a default value there, the first parameter to `typer.Option()` serves the same purpose of defining that default value.20-21-So, if we had:22-23-```Python24-lastname: str = ""25-```26-27-now we write:28-29-```Python30-lastname: str = typer.Option("")31-```32-33-And both forms achieve the same: a *CLI option* with a default value of an empty string (`""`).34-35-And then we can pass the `help` keyword parameter:36-37-```Python38-lastname: str = typer.Option("", help="this option does this and that")39-```40-41-to create the help for that *CLI option*.42-43-Copy that example from above to a file `main.py`.44-45-Test it:46-47-<div class="termy">48-49-```console50-$ python main.py --help51-52-Usage: main.py [OPTIONS] NAME53-54- Say hi to NAME, optionally with a --lastname.55-56- If --formal is used, say hi very formally.57-58-Options:59- --lastname TEXT Last name of person to greet.60- --formal / --no-formal Say hi formally.61- --install-completion Install completion for the current shell.62- --show-completion Show completion for the current shell, to copy it or customize the installation.63- --help Show this message and exit.64-65-// Now you have a help text for the --lastname and --formal CLI options 🎉66-```67-68-</div>69-70-## Make a *CLI option* required71-72-We said before that *by default*:73-74-* *CLI options* are **optional**75-* *CLI arguments* are **required**76-77-Well, that's how they work *by default*, and that's the convention in many CLI programs and systems.78-79-But if you really want, you can change that.80-81-To make a *CLI option* required, pass `...` to `typer.Option()`.82-83-!!! info84- 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>.85-86-That will tell **Typer** that it's still a *CLI option*, but it doesn't have a default value, and it's required.87-88-Let's make the `--lastname` a required *CLI option*.89-90-We'll also simplify the example to focus on the new parts:91-92-```Python hl_lines="4"93-{!./src/options/tutorial002.py!}94-```95-96-!!! tip97- You could still add `help` to `typer.Option()` as before, but we are omitting it here to simplify the example.98-99-And test it:100-101-<div class="termy">102-103-```console104-// Pass the NAME CLI argument105-$ python main.py Camila106-107-// We didn't pass the now required --lastname CLI option108-Usage: main.py [OPTIONS] NAME109-Try "main.py --help" for help.110-111-Error: Missing option "--lastname".112-113-// Now update it to pass the required --lastname CLI option114-$ python main.py Camila --lastname Gutiérrez115-116-Hello Camila Gutiérrez117-118-// And if you check the help119-$ python main.py --help120-121-Usage: main.py [OPTIONS] NAME122-123-Options:124- --lastname TEXT [required]125- --install-completion Install completion for the current shell.126- --show-completion Show completion for the current shell, to copy it or customize the installation.127- --help Show this message and exit.128-129-// It now tells you that --lastname is required 🎉130-```131-132-</div>133-134-## Prompt for a *CLI option*135-136-It's also possible to, instead of just showing an error, ask for the missing value with `prompt=True`:137-138-```Python hl_lines="4"139-{!./src/options/tutorial003.py!}140-```141-142-And then your program will ask the user for it in the terminal:143-144-<div class="termy">145-146-```console147-// Call it with the NAME CLI argument148-$ python main.py Camila149-150-// It asks for the missing CLI option --lastname151-# Lastname: $ Gutiérrez152-153-Hello Camila Gutiérrez154-```155-156-</div>157-158-### Customize the prompt159-160-You can also set a custom prompt, passing the string that you want to use instead of just `True`:161-162-```Python hl_lines="6"163-{!./src/options/tutorial004.py!}164-```165-166-And then your program will ask for it using with your custom prompt:167-168-<div class="termy">169-170-```console171-// Call it with the NAME CLI argument172-$ python main.py Camila173-174-// It uses the custom prompt175-# Please tell me your last name: $ Gutiérrez176-177-Hello Camila Gutiérrez178-```179-180-</div>181-182-## Confirmation prompt183-184-In some cases you could want to prompt for something and then ask the user to confirm it by typing it twice.185-186-You can do it passing the parameter `confirmation_prompt=True`.187-188-Let's say it's a CLI app to delete a project:189-190-```Python hl_lines="4"191-{!./src/options/tutorial005.py!}192-```193-194-And it will prompt the user for a value and then for the confirmation:195-196-<div class="termy">197-198-```console199-$ python main.py200-201-// Your app will first prompt for the project name, and then for the confirmation202-# Project name: $ Old Project203-# Repeat for confirmation: $ Old Project204-205-Deleting project Old Project206-207-// If the user doesn't type the same, receives an error and a new prompt208-$ python main.py209-210-# Project name: $ Old Project211-# Repeat for confirmation: $ New Spice212-213-Error: the two entered values do not match214-215-# Project name: $ Old Project216-# Repeat for confirmation: $ Old Project217-218-Deleting project Old Project219-220-// Now it works 🎉221-```222-223-</div>224-225-## Show default in help226-227-You can tell Typer to show the default value in the help text with `show_default=True`:228-229-```Python hl_lines="4"230-{!./src/options/tutorial006.py!}231-```232-233-And it will show up in the help text:234-235-<div class="termy">236-237-```console238-$ python main.py239-240-Hello Wade Wilson241-242-// Show the help243-$ python main.py --help244-245-Usage: main.py [OPTIONS]246-247-Options:248- --fullname TEXT [default: Wade Wilson]249- --install-completion Install completion for the current shell.250- --show-completion Show completion for the current shell, to copy it or customize the installation.251- --help Show this message and exit.252-```253-254-</div>255-256-!!! tip257- Notice the `[default: Wade Wilson]` in the help text.258-259-## Other uses260-261-`typer.Option()` has several other users. For data validation, to enable other features, etc.262-263-But you will see about that later in the docs.added:tests/test_tutorial/test_commands/test_arguments/__init__.py
added:tests/test_tutorial/test_commands/test_arguments/test_tutorial001.py+42 added
1+import subprocess2+from commands.arguments import tutorial001 as mod3+4+from typer.testing import CliRunner5+6+app = mod.app7+8+runner = CliRunner()9+10+11+def test_help_create():12+ result = runner.invoke(app, ["create", "--help"])13+ assert result.exit_code == 014+ assert "create [OPTIONS] USERNAME" in result.output15+16+17+def test_help_delete():18+ result = runner.invoke(app, ["delete", "--help"])19+ assert result.exit_code == 020+ assert "delete [OPTIONS] USERNAME" in result.output21+22+23+def test_create():24+ result = runner.invoke(app, ["create", "Camila"])25+ assert result.exit_code == 026+ assert "Creating user: Camila" in result.output27+28+29+def test_delete():30+ result = runner.invoke(app, ["delete", "Camila"])31+ assert result.exit_code == 032+ assert "Deleting user: Camila" in result.output33+34+35+def test_script():36+ result = subprocess.run(37+ ["coverage", "run", mod.__file__, "--help"],38+ stdout=subprocess.PIPE,39+ stderr=subprocess.PIPE,40+ encoding="utf-8",41+ )42+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_commands/test_help/__init__.py
added:tests/test_tutorial/test_commands/test_help/test_tutorial001.py+106 added
1+import subprocess2+from commands.help import tutorial001 as mod3+4+from typer.testing import CliRunner5+6+app = mod.app7+8+runner = CliRunner()9+10+11+def test_help():12+ result = runner.invoke(app, ["--help"])13+ assert result.exit_code == 014+ assert "Awesome CLI user manager." in result.output15+ assert "create" in result.output16+ assert "Create a new user with USERNAME." in result.output17+ assert "delete" in result.output18+ assert "Delete a user with USERNAME." in result.output19+ assert "delete-all" in result.output20+ assert "Delete ALL users in the database." in result.output21+ assert "init" in result.output22+ assert "Initialize the users database." in result.output23+24+25+def test_help_create():26+ result = runner.invoke(app, ["create", "--help"])27+ assert result.exit_code == 028+ assert "create [OPTIONS] USERNAME" in result.output29+ assert "Create a new user with USERNAME." in result.output30+31+32+def test_help_delete():33+ result = runner.invoke(app, ["delete", "--help"])34+ assert result.exit_code == 035+ assert "delete [OPTIONS] USERNAME" in result.output36+ assert "Delete a user with USERNAME." in result.output37+ assert "--force / --no-force" in result.output38+ assert "Force deletion without confirmation." in result.output39+40+41+def test_help_delete_all():42+ result = runner.invoke(app, ["delete-all", "--help"])43+ assert result.exit_code == 044+ assert "delete-all [OPTIONS]" in result.output45+ assert "Delete ALL users in the database." in result.output46+ assert "If --force is not used, will ask for confirmation." in result.output47+ assert "[required]" in result.output48+ assert "--force / --no-force" in result.output49+ assert "Force deletion without confirmation." in result.output50+51+52+def test_help_init():53+ result = runner.invoke(app, ["init", "--help"])54+ assert result.exit_code == 055+ assert "init [OPTIONS]" in result.output56+ assert "Initialize the users database." in result.output57+58+59+def test_create():60+ result = runner.invoke(app, ["create", "Camila"])61+ assert result.exit_code == 062+ assert "Creating user: Camila" in result.output63+64+65+def test_delete():66+ result = runner.invoke(app, ["delete", "Camila"], input="y\n")67+ assert result.exit_code == 068+ assert "Are you sure you want to delete the user? [y/N]:" in result.output69+ assert "Deleting user: Camila" in result.output70+71+72+def test_no_delete():73+ result = runner.invoke(app, ["delete", "Camila"], input="n\n")74+ assert result.exit_code == 075+ assert "Are you sure you want to delete the user? [y/N]:" in result.output76+ assert "Operation cancelled" in result.output77+78+79+def test_delete_all():80+ result = runner.invoke(app, ["delete-all",], input="y\n")81+ assert result.exit_code == 082+ assert "Are you sure you want to delete ALL users? [y/N]:" in result.output83+ assert "Deleting all users" in result.output84+85+86+def test_no_delete_all():87+ result = runner.invoke(app, ["delete-all",], input="n\n")88+ assert result.exit_code == 089+ assert "Are you sure you want to delete ALL users? [y/N]:" in result.output90+ assert "Operation cancelled" in result.output91+92+93+def test_init():94+ result = runner.invoke(app, ["init",])95+ assert result.exit_code == 096+ assert "Initializing user database" in result.output97+98+99+def test_script():100+ result = subprocess.run(101+ ["coverage", "run", mod.__file__, "--help"],102+ stdout=subprocess.PIPE,103+ stderr=subprocess.PIPE,104+ encoding="utf-8",105+ )106+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_commands/test_help/test_tutorial002.py+55 added
1+import subprocess2+from commands.help import tutorial002 as mod3+4+from typer.testing import CliRunner5+6+app = mod.app7+8+runner = CliRunner()9+10+11+def test_help():12+ result = runner.invoke(app, ["--help"])13+ assert result.exit_code == 014+ assert "create" in result.output15+ assert "Create a new user with USERNAME." in result.output16+ assert "delete" in result.output17+ assert "Delete a user with USERNAME." in result.output18+ assert "Some internal utility function to create." not in result.output19+ assert "Some internal utility function to delete." not in result.output20+21+22+def test_help_create():23+ result = runner.invoke(app, ["create", "--help"])24+ assert result.exit_code == 025+ assert "Create a new user with USERNAME." in result.output26+ assert "Some internal utility function to create." not in result.output27+28+29+def test_help_create():30+ result = runner.invoke(app, ["delete", "--help"])31+ assert result.exit_code == 032+ assert "Delete a user with USERNAME." in result.output33+ assert "Some internal utility function to delete." not in result.output34+35+36+def test_create():37+ result = runner.invoke(app, ["create", "Camila"])38+ assert result.exit_code == 039+ assert "Creating user: Camila" in result.output40+41+42+def test_delete():43+ result = runner.invoke(app, ["delete", "Camila"])44+ assert result.exit_code == 045+ assert "Deleting user: Camila" in result.output46+47+48+def test_script():49+ result = subprocess.run(50+ ["coverage", "run", mod.__file__, "--help"],51+ stdout=subprocess.PIPE,52+ stderr=subprocess.PIPE,53+ encoding="utf-8",54+ )55+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_commands/test_index/__init__.py
added:tests/test_tutorial/test_commands/test_index/test_tutorial001.py+30 added
1+import subprocess2+from commands.index import tutorial001 as mod3+4+from typer.testing import CliRunner5+6+app = mod.app7+8+runner = CliRunner()9+10+11+def test_no_arg():12+ result = runner.invoke(app)13+ assert result.exit_code != 014+ assert 'Error: Missing argument "NAME".' in result.output15+16+17+def test_arg():18+ result = runner.invoke(app, ["Camila"])19+ assert result.exit_code == 020+ assert "Hello Camila" in result.output21+22+23+def test_script():24+ result = subprocess.run(25+ ["coverage", "run", mod.__file__, "--help"],26+ stdout=subprocess.PIPE,27+ stderr=subprocess.PIPE,28+ encoding="utf-8",29+ )30+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_commands/test_index/test_tutorial002.py+39 added
1+import subprocess2+from commands.index import tutorial002 as mod3+4+from typer.testing import CliRunner5+6+app = mod.app7+8+runner = CliRunner()9+10+11+def test_help():12+ result = runner.invoke(app, ["--help"])13+ assert result.exit_code == 014+ assert "[OPTIONS] COMMAND [ARGS]..." in result.output15+ assert "Commands:" in result.output16+ assert "create" in result.output17+ assert "delete" in result.output18+19+20+def test_create():21+ result = runner.invoke(app, ["create"])22+ assert result.exit_code == 023+ assert "Creating user: Hiro Hamada" in result.output24+25+26+def test_delete():27+ result = runner.invoke(app, ["delete"])28+ assert result.exit_code == 029+ assert "Deleting user: Hiro Hamada" in result.output30+31+32+def test_script():33+ result = subprocess.run(34+ ["coverage", "run", mod.__file__, "--help"],35+ stdout=subprocess.PIPE,36+ stderr=subprocess.PIPE,37+ encoding="utf-8",38+ )39+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_commands/test_name/__init__.py
added:tests/test_tutorial/test_commands/test_name/test_tutorial001.py+38 added
1+import subprocess2+from commands.name import tutorial001 as mod3+4+from typer.testing import CliRunner5+6+app = mod.app7+8+runner = CliRunner()9+10+11+def test_help():12+ result = runner.invoke(app, ["--help"])13+ assert result.exit_code == 014+ assert "Commands:" in result.output15+ assert "create" in result.output16+ assert "delete" in result.output17+18+19+def test_create():20+ result = runner.invoke(app, ["create", "Camila"])21+ assert result.exit_code == 022+ assert "Creating user: Camila" in result.output23+24+25+def test_delete():26+ result = runner.invoke(app, ["delete", "Camila"])27+ assert result.exit_code == 028+ assert "Deleting user: Camila" in result.output29+30+31+def test_script():32+ result = subprocess.run(33+ ["coverage", "run", mod.__file__, "--help"],34+ stdout=subprocess.PIPE,35+ stderr=subprocess.PIPE,36+ encoding="utf-8",37+ )38+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_commands/test_options/__init__.py
added:tests/test_tutorial/test_commands/test_options/test_tutorial001.py+75 added
1+import subprocess2+from commands.options import tutorial001 as mod3+4+from typer.testing import CliRunner5+6+app = mod.app7+8+runner = CliRunner()9+10+11+def test_help():12+ result = runner.invoke(app, ["--help"])13+ assert result.exit_code == 014+ assert "Commands:" in result.output15+ assert "create" in result.output16+ assert "delete" in result.output17+ assert "delete-all" in result.output18+ assert "init" in result.output19+20+21+def test_create():22+ result = runner.invoke(app, ["create", "Camila"])23+ assert result.exit_code == 024+ assert "Creating user: Camila" in result.output25+26+27+def test_delete():28+ result = runner.invoke(app, ["delete", "Camila"], input="y\n")29+ assert result.exit_code == 030+ assert "Are you sure you want to delete the user? [y/N]:" in result.output31+ assert "Deleting user: Camila" in result.output32+33+34+def test_no_delete():35+ result = runner.invoke(app, ["delete", "Camila"], input="n\n")36+ assert result.exit_code == 037+ assert "Are you sure you want to delete the user? [y/N]:" in result.output38+ assert "Operation cancelled" in result.output39+40+41+def test_delete_all():42+ result = runner.invoke(app, ["delete-all"], input="y\n")43+ assert result.exit_code == 044+ assert "Are you sure you want to delete ALL users? [y/N]:" in result.output45+ assert "Deleting all users" in result.output46+47+48+def test_no_delete_all():49+ result = runner.invoke(app, ["delete-all"], input="n\n")50+ assert result.exit_code == 051+ assert "Are you sure you want to delete ALL users? [y/N]:" in result.output52+ assert "Operation cancelled" in result.output53+54+55+def test_delete_all_force():56+ result = runner.invoke(app, ["delete-all", "--force"])57+ assert result.exit_code == 058+ assert "Are you sure you want to delete ALL users? [y/N]:" not in result.output59+ assert "Deleting all users" in result.output60+61+62+def test_init():63+ result = runner.invoke(app, ["init"])64+ assert result.exit_code == 065+ assert "Initializing user database" in result.output66+67+68+def test_script():69+ result = subprocess.run(70+ ["coverage", "run", mod.__file__, "--help"],71+ stdout=subprocess.PIPE,72+ stderr=subprocess.PIPE,73+ encoding="utf-8",74+ )75+ assert "Usage" in result.stdoutdeleted:tests/test_tutorial/test_commands/test_tutorial001.py-30 removed
1-import subprocess2-from commands import tutorial001 as mod3-4-from typer.testing import CliRunner5-6-app = mod.app7-8-runner = CliRunner()9-10-11-def test_no_arg():12- result = runner.invoke(app)13- assert result.exit_code != 014- assert 'Error: Missing argument "NAME".' in result.output15-16-17-def test_arg():18- result = runner.invoke(app, ["Camila"])19- assert result.exit_code == 020- assert "Hello Camila" in result.output21-22-23-def test_script():24- result = subprocess.run(25- ["coverage", "run", mod.__file__, "--help"],26- stdout=subprocess.PIPE,27- stderr=subprocess.PIPE,28- encoding="utf-8",29- )30- assert "Usage" in result.stdoutdeleted:tests/test_tutorial/test_commands/test_tutorial002.py-39 removed
1-import subprocess2-from commands import tutorial002 as mod3-4-from typer.testing import CliRunner5-6-app = mod.app7-8-runner = CliRunner()9-10-11-def test_help():12- result = runner.invoke(app, ["--help"])13- assert result.exit_code == 014- assert "[OPTIONS] COMMAND [ARGS]..." in result.output15- assert "Commands:" in result.output16- assert "create" in result.output17- assert "delete" in result.output18-19-20-def test_create():21- result = runner.invoke(app, ["create"])22- assert result.exit_code == 023- assert "Creating user: Hiro Hamada" in result.output24-25-26-def test_delete():27- result = runner.invoke(app, ["delete"])28- assert result.exit_code == 029- assert "Deleting user: Hiro Hamada" in result.output30-31-32-def test_script():33- result = subprocess.run(34- ["coverage", "run", mod.__file__, "--help"],35- stdout=subprocess.PIPE,36- stderr=subprocess.PIPE,37- encoding="utf-8",38- )39- assert "Usage" in result.stdoutdeleted:tests/test_tutorial/test_commands/test_tutorial003.py-42 removed
1-import subprocess2-from commands import tutorial003 as mod3-4-from typer.testing import CliRunner5-6-app = mod.app7-8-runner = CliRunner()9-10-11-def test_help_create():12- result = runner.invoke(app, ["create", "--help"])13- assert result.exit_code == 014- assert "create [OPTIONS] USERNAME" in result.output15-16-17-def test_help_delete():18- result = runner.invoke(app, ["delete", "--help"])19- assert result.exit_code == 020- assert "delete [OPTIONS] USERNAME" in result.output21-22-23-def test_create():24- result = runner.invoke(app, ["create", "Camila"])25- assert result.exit_code == 026- assert "Creating user: Camila" in result.output27-28-29-def test_delete():30- result = runner.invoke(app, ["delete", "Camila"])31- assert result.exit_code == 032- assert "Deleting user: Camila" in result.output33-34-35-def test_script():36- result = subprocess.run(37- ["coverage", "run", mod.__file__, "--help"],38- stdout=subprocess.PIPE,39- stderr=subprocess.PIPE,40- encoding="utf-8",41- )42- assert "Usage" in result.stdoutdeleted:tests/test_tutorial/test_commands/test_tutorial004.py-75 removed
1-import subprocess2-from commands import tutorial004 as mod3-4-from typer.testing import CliRunner5-6-app = mod.app7-8-runner = CliRunner()9-10-11-def test_help():12- result = runner.invoke(app, ["--help"])13- assert result.exit_code == 014- assert "Commands:" in result.output15- assert "create" in result.output16- assert "delete" in result.output17- assert "delete-all" in result.output18- assert "init" in result.output19-20-21-def test_create():22- result = runner.invoke(app, ["create", "Camila"])23- assert result.exit_code == 024- assert "Creating user: Camila" in result.output25-26-27-def test_delete():28- result = runner.invoke(app, ["delete", "Camila"], input="y\n")29- assert result.exit_code == 030- assert "Are you sure you want to delete the user? [y/N]:" in result.output31- assert "Deleting user: Camila" in result.output32-33-34-def test_no_delete():35- result = runner.invoke(app, ["delete", "Camila"], input="n\n")36- assert result.exit_code == 037- assert "Are you sure you want to delete the user? [y/N]:" in result.output38- assert "Operation cancelled" in result.output39-40-41-def test_delete_all():42- result = runner.invoke(app, ["delete-all"], input="y\n")43- assert result.exit_code == 044- assert "Are you sure you want to delete ALL users? [y/N]:" in result.output45- assert "Deleting all users" in result.output46-47-48-def test_no_delete_all():49- result = runner.invoke(app, ["delete-all"], input="n\n")50- assert result.exit_code == 051- assert "Are you sure you want to delete ALL users? [y/N]:" in result.output52- assert "Operation cancelled" in result.output53-54-55-def test_delete_all_force():56- result = runner.invoke(app, ["delete-all", "--force"])57- assert result.exit_code == 058- assert "Are you sure you want to delete ALL users? [y/N]:" not in result.output59- assert "Deleting all users" in result.output60-61-62-def test_init():63- result = runner.invoke(app, ["init"])64- assert result.exit_code == 065- assert "Initializing user database" in result.output66-67-68-def test_script():69- result = subprocess.run(70- ["coverage", "run", mod.__file__, "--help"],71- stdout=subprocess.PIPE,72- stderr=subprocess.PIPE,73- encoding="utf-8",74- )75- assert "Usage" in result.stdoutdeleted:tests/test_tutorial/test_commands/test_tutorial005.py-106 removed
1-import subprocess2-from commands import tutorial005 as mod3-4-from typer.testing import CliRunner5-6-app = mod.app7-8-runner = CliRunner()9-10-11-def test_help():12- result = runner.invoke(app, ["--help"])13- assert result.exit_code == 014- assert "Awesome CLI user manager." in result.output15- assert "create" in result.output16- assert "Create a new user with USERNAME." in result.output17- assert "delete" in result.output18- assert "Delete a user with USERNAME." in result.output19- assert "delete-all" in result.output20- assert "Delete ALL users in the database." in result.output21- assert "init" in result.output22- assert "Initialize the users database." in result.output23-24-25-def test_help_create():26- result = runner.invoke(app, ["create", "--help"])27- assert result.exit_code == 028- assert "create [OPTIONS] USERNAME" in result.output29- assert "Create a new user with USERNAME." in result.output30-31-32-def test_help_delete():33- result = runner.invoke(app, ["delete", "--help"])34- assert result.exit_code == 035- assert "delete [OPTIONS] USERNAME" in result.output36- assert "Delete a user with USERNAME." in result.output37- assert "--force / --no-force" in result.output38- assert "Force deletion without confirmation." in result.output39-40-41-def test_help_delete_all():42- result = runner.invoke(app, ["delete-all", "--help"])43- assert result.exit_code == 044- assert "delete-all [OPTIONS]" in result.output45- assert "Delete ALL users in the database." in result.output46- assert "If --force is not used, will ask for confirmation." in result.output47- assert "[required]" in result.output48- assert "--force / --no-force" in result.output49- assert "Force deletion without confirmation." in result.output50-51-52-def test_help_init():53- result = runner.invoke(app, ["init", "--help"])54- assert result.exit_code == 055- assert "init [OPTIONS]" in result.output56- assert "Initialize the users database." in result.output57-58-59-def test_create():60- result = runner.invoke(app, ["create", "Camila"])61- assert result.exit_code == 062- assert "Creating user: Camila" in result.output63-64-65-def test_delete():66- result = runner.invoke(app, ["delete", "Camila"], input="y\n")67- assert result.exit_code == 068- assert "Are you sure you want to delete the user? [y/N]:" in result.output69- assert "Deleting user: Camila" in result.output70-71-72-def test_no_delete():73- result = runner.invoke(app, ["delete", "Camila"], input="n\n")74- assert result.exit_code == 075- assert "Are you sure you want to delete the user? [y/N]:" in result.output76- assert "Operation cancelled" in result.output77-78-79-def test_delete_all():80- result = runner.invoke(app, ["delete-all",], input="y\n")81- assert result.exit_code == 082- assert "Are you sure you want to delete ALL users? [y/N]:" in result.output83- assert "Deleting all users" in result.output84-85-86-def test_no_delete_all():87- result = runner.invoke(app, ["delete-all",], input="n\n")88- assert result.exit_code == 089- assert "Are you sure you want to delete ALL users? [y/N]:" in result.output90- assert "Operation cancelled" in result.output91-92-93-def test_init():94- result = runner.invoke(app, ["init",])95- assert result.exit_code == 096- assert "Initializing user database" in result.output97-98-99-def test_script():100- result = subprocess.run(101- ["coverage", "run", mod.__file__, "--help"],102- stdout=subprocess.PIPE,103- stderr=subprocess.PIPE,104- encoding="utf-8",105- )106- assert "Usage" in result.stdoutdeleted:tests/test_tutorial/test_commands/test_tutorial006.py-38 removed
1-import subprocess2-from commands import tutorial006 as mod3-4-from typer.testing import CliRunner5-6-app = mod.app7-8-runner = CliRunner()9-10-11-def test_help():12- result = runner.invoke(app, ["--help"])13- assert result.exit_code == 014- assert "Commands:" in result.output15- assert "create" in result.output16- assert "delete" in result.output17-18-19-def test_create():20- result = runner.invoke(app, ["create", "Camila"])21- assert result.exit_code == 022- assert "Creating user: Camila" in result.output23-24-25-def test_delete():26- result = runner.invoke(app, ["delete", "Camila"])27- assert result.exit_code == 028- assert "Deleting user: Camila" in result.output29-30-31-def test_script():32- result = subprocess.run(33- ["coverage", "run", mod.__file__, "--help"],34- stdout=subprocess.PIPE,35- stderr=subprocess.PIPE,36- encoding="utf-8",37- )38- assert "Usage" in result.stdoutadded:tests/test_tutorial/test_options/test_help/__init__.py
added:tests/test_tutorial/test_options/test_help/test_tutorial001.py+48 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from options.help import tutorial001 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_help():15+ result = runner.invoke(app, ["--help"])16+ assert result.exit_code == 017+ assert "Say hi to NAME, optionally with a --lastname." in result.output18+ assert "If --formal is used, say hi very formally." in result.output19+ assert "Last name of person to greet." in result.output20+ assert "Say hi formally." in result.output21+22+23+def test_1():24+ result = runner.invoke(app, ["Camila"])25+ assert result.exit_code == 026+ assert "Hello Camila" in result.output27+28+29+def test_option_lastname():30+ result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"])31+ assert result.exit_code == 032+ assert "Hello Camila Gutiérrez" in result.output33+34+35+def test_formal():36+ result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez", "--formal"])37+ assert result.exit_code == 038+ assert "Good day Ms. Camila Gutiérrez." in result.output39+40+41+def test_script():42+ result = subprocess.run(43+ ["coverage", "run", mod.__file__, "--help"],44+ stdout=subprocess.PIPE,45+ stderr=subprocess.PIPE,46+ encoding="utf-8",47+ )48+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_options/test_help/test_tutorial002.py+34 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from options.help import tutorial002 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_call():15+ result = runner.invoke(app)16+ assert result.exit_code == 017+ assert "Hello Wade Wilson" in result.output18+19+20+def test_help():21+ result = runner.invoke(app, ["--help"])22+ assert result.exit_code == 023+ assert "--fullname TEXT" in result.output24+ assert "[default: Wade Wilson]" in result.output25+26+27+def test_script():28+ result = subprocess.run(29+ ["coverage", "run", mod.__file__, "--help"],30+ stdout=subprocess.PIPE,31+ stderr=subprocess.PIPE,32+ encoding="utf-8",33+ )34+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_options/test_name/__init__.py
added:tests/test_tutorial/test_options/test_name/test_tutorial001.py+34 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from options.name import tutorial001 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_option_help():15+ result = runner.invoke(app, ["--help"])16+ assert result.exit_code == 017+ assert "--name TEXT" in result.output18+ assert "--user-name" not in result.output19+20+21+def test_call():22+ result = runner.invoke(app, ["--name", "Camila"])23+ assert result.exit_code == 024+ assert "Hello Camila" in result.output25+26+27+def test_script():28+ result = subprocess.run(29+ ["coverage", "run", mod.__file__, "--help"],30+ stdout=subprocess.PIPE,31+ stderr=subprocess.PIPE,32+ encoding="utf-8",33+ )34+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_options/test_name/test_tutorial002.py+40 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from options.name import tutorial002 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_option_help():15+ result = runner.invoke(app, ["--help"])16+ assert result.exit_code == 017+ assert "-n, --name TEXT" in result.output18+ assert "--user-name" not in result.output19+20+21+def test_call():22+ result = runner.invoke(app, ["-n", "Camila"])23+ assert result.exit_code == 024+ assert "Hello Camila" in result.output25+26+27+def test_call_long():28+ result = runner.invoke(app, ["--name", "Camila"])29+ assert result.exit_code == 030+ assert "Hello Camila" in result.output31+32+33+def test_script():34+ result = subprocess.run(35+ ["coverage", "run", mod.__file__, "--help"],36+ stdout=subprocess.PIPE,37+ stderr=subprocess.PIPE,38+ encoding="utf-8",39+ )40+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_options/test_name/test_tutorial003.py+35 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from options.name import tutorial003 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_option_help():15+ result = runner.invoke(app, ["--help"])16+ assert result.exit_code == 017+ assert "-n TEXT" in result.output18+ assert "--user-name" not in result.output19+ assert "--name" not in result.output20+21+22+def test_call():23+ result = runner.invoke(app, ["-n", "Camila"])24+ assert result.exit_code == 025+ assert "Hello Camila" 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.stdoutadded:tests/test_tutorial/test_options/test_name/test_tutorial004.py+40 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from options.name import tutorial004 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_option_help():15+ result = runner.invoke(app, ["--help"])16+ assert result.exit_code == 017+ assert "-n, --user-name TEXT" in result.output18+ assert "--name" not in result.output19+20+21+def test_call():22+ result = runner.invoke(app, ["-n", "Camila"])23+ assert result.exit_code == 024+ assert "Hello Camila" in result.output25+26+27+def test_call_long():28+ result = runner.invoke(app, ["--user-name", "Camila"])29+ assert result.exit_code == 030+ assert "Hello Camila" in result.output31+32+33+def test_script():34+ result = subprocess.run(35+ ["coverage", "run", mod.__file__, "--help"],36+ stdout=subprocess.PIPE,37+ stderr=subprocess.PIPE,38+ encoding="utf-8",39+ )40+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_options/test_name/test_tutorial005.py+51 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from options.name import tutorial005 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_option_help():15+ result = runner.invoke(app, ["--help"])16+ assert result.exit_code == 017+ assert "-n, --name TEXT" in result.output18+ assert "-f, --formal" in result.output19+20+21+def test_call():22+ result = runner.invoke(app, ["-n", "Camila"])23+ assert result.exit_code == 024+ assert "Hello Camila" in result.output25+26+27+def test_call_formal():28+ result = runner.invoke(app, ["-n", "Camila", "-f"])29+ assert result.exit_code == 030+ assert "Good day Ms. Camila." in result.output31+32+33+def test_call_formal_condensed():34+ result = runner.invoke(app, ["-fn", "Camila"])35+ assert result.exit_code == 036+ assert "Good day Ms. Camila." in result.output37+38+39+def test_call_condensed_wrong_order():40+ result = runner.invoke(app, ["-nf", "Camila"])41+ assert result.exit_code != 042+43+44+def test_script():45+ result = subprocess.run(46+ ["coverage", "run", mod.__file__, "--help"],47+ stdout=subprocess.PIPE,48+ stderr=subprocess.PIPE,49+ encoding="utf-8",50+ )51+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_options/test_prompt/__init__.py
added:tests/test_tutorial/test_options/test_prompt/test_tutorial001.py+41 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from options.prompt import tutorial001 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_option_lastname():15+ result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"])16+ assert result.exit_code == 017+ assert "Hello Camila Gutiérrez" in result.output18+19+20+def test_option_lastname_prompt():21+ result = runner.invoke(app, ["Camila"], input="Gutiérrez")22+ assert result.exit_code == 023+ assert "Lastname: " in result.output24+ assert "Hello Camila Gutiérrez" in result.output25+26+27+def test_help():28+ result = runner.invoke(app, ["--help"])29+ assert result.exit_code == 030+ assert "--lastname TEXT" in result.output31+ assert "[required]" in result.output32+33+34+def test_script():35+ result = subprocess.run(36+ ["coverage", "run", mod.__file__, "--help"],37+ stdout=subprocess.PIPE,38+ stderr=subprocess.PIPE,39+ encoding="utf-8",40+ )41+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_options/test_prompt/test_tutorial002.py+41 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from options.prompt import tutorial002 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_option_lastname():15+ result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"])16+ assert result.exit_code == 017+ assert "Hello Camila Gutiérrez" in result.output18+19+20+def test_option_lastname_prompt():21+ result = runner.invoke(app, ["Camila"], input="Gutiérrez")22+ assert result.exit_code == 023+ assert "Please tell me your last name: " in result.output24+ assert "Hello Camila Gutiérrez" in result.output25+26+27+def test_help():28+ result = runner.invoke(app, ["--help"])29+ assert result.exit_code == 030+ assert "--lastname TEXT" in result.output31+ assert "[required]" in result.output32+33+34+def test_script():35+ result = subprocess.run(36+ ["coverage", "run", mod.__file__, "--help"],37+ stdout=subprocess.PIPE,38+ stderr=subprocess.PIPE,39+ encoding="utf-8",40+ )41+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_options/test_prompt/test_tutorial003.py+50 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from options.prompt import tutorial003 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_prompt():15+ result = runner.invoke(app, input="Old Project\nOld Project\n")16+ assert result.exit_code == 017+ assert "Deleting project Old Project" in result.output18+19+20+def test_prompt_not_equal():21+ result = runner.invoke(22+ app, input="Old Project\nNew Spice\nOld Project\nOld Project\n"23+ )24+ assert result.exit_code == 025+ assert "Error: the two entered values do not match" in result.output26+ assert "Deleting project Old Project" in result.output27+28+29+def test_option():30+ result = runner.invoke(app, ["--project-name", "Old Project"])31+ assert result.exit_code == 032+ assert "Deleting project Old Project" in result.output33+ assert "Project name: " not in result.output34+35+36+def test_help():37+ result = runner.invoke(app, ["--help"])38+ assert result.exit_code == 039+ assert "--project-name TEXT" in result.output40+ assert "[required]" in result.output41+42+43+def test_script():44+ result = subprocess.run(45+ ["coverage", "run", mod.__file__, "--help"],46+ stdout=subprocess.PIPE,47+ stderr=subprocess.PIPE,48+ encoding="utf-8",49+ )50+ assert "Usage" in result.stdoutadded:tests/test_tutorial/test_options/test_required/__init__.py
added:tests/test_tutorial/test_options/test_required/test_tutorial002.py+40 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from options.required import tutorial001 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_1():15+ result = runner.invoke(app, ["Camila"])16+ assert result.exit_code != 017+ assert 'Error: Missing option "--lastname".' in result.output18+19+20+def test_option_lastname():21+ result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"])22+ assert result.exit_code == 023+ assert "Hello Camila Gutiérrez" in result.output24+25+26+def test_help():27+ result = runner.invoke(app, ["--help"])28+ assert result.exit_code == 029+ assert "--lastname TEXT" in result.output30+ assert "[required]" in result.output31+32+33+def test_script():34+ result = subprocess.run(35+ ["coverage", "run", mod.__file__, "--help"],36+ stdout=subprocess.PIPE,37+ stderr=subprocess.PIPE,38+ encoding="utf-8",39+ )40+ assert "Usage" in result.stdoutdeleted:tests/test_tutorial/test_options/test_tutorial001.py-48 removed
1-import subprocess2-3-import typer4-from typer.testing import CliRunner5-6-from options import tutorial001 as mod7-8-runner = CliRunner()9-10-app = typer.Typer()11-app.command()(mod.main)12-13-14-def test_help():15- result = runner.invoke(app, ["--help"])16- assert result.exit_code == 017- assert "Say hi to NAME, optionally with a --lastname." in result.output18- assert "If --formal is used, say hi very formally." in result.output19- assert "Last name of person to greet." in result.output20- assert "Say hi formally." in result.output21-22-23-def test_1():24- result = runner.invoke(app, ["Camila"])25- assert result.exit_code == 026- assert "Hello Camila" in result.output27-28-29-def test_option_lastname():30- result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"])31- assert result.exit_code == 032- assert "Hello Camila Gutiérrez" in result.output33-34-35-def test_formal():36- result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez", "--formal"])37- assert result.exit_code == 038- assert "Good day Ms. Camila Gutiérrez." in result.output39-40-41-def test_script():42- result = subprocess.run(43- ["coverage", "run", mod.__file__, "--help"],44- stdout=subprocess.PIPE,45- stderr=subprocess.PIPE,46- encoding="utf-8",47- )48- assert "Usage" in result.stdoutdeleted:tests/test_tutorial/test_options/test_tutorial002.py-40 removed
1-import subprocess2-3-import typer4-from typer.testing import CliRunner5-6-from options import tutorial002 as mod7-8-runner = CliRunner()9-10-app = typer.Typer()11-app.command()(mod.main)12-13-14-def test_1():15- result = runner.invoke(app, ["Camila"])16- assert result.exit_code != 017- assert 'Error: Missing option "--lastname".' in result.output18-19-20-def test_option_lastname():21- result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"])22- assert result.exit_code == 023- assert "Hello Camila Gutiérrez" in result.output24-25-26-def test_help():27- result = runner.invoke(app, ["--help"])28- assert result.exit_code == 029- assert "--lastname TEXT" in result.output30- assert "[required]" in result.output31-32-33-def test_script():34- result = subprocess.run(35- ["coverage", "run", mod.__file__, "--help"],36- stdout=subprocess.PIPE,37- stderr=subprocess.PIPE,38- encoding="utf-8",39- )40- assert "Usage" in result.stdoutdeleted:tests/test_tutorial/test_options/test_tutorial003.py-41 removed
1-import subprocess2-3-import typer4-from typer.testing import CliRunner5-6-from options import tutorial003 as mod7-8-runner = CliRunner()9-10-app = typer.Typer()11-app.command()(mod.main)12-13-14-def test_option_lastname():15- result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"])16- assert result.exit_code == 017- assert "Hello Camila Gutiérrez" in result.output18-19-20-def test_option_lastname_prompt():21- result = runner.invoke(app, ["Camila"], input="Gutiérrez")22- assert result.exit_code == 023- assert "Lastname: " in result.output24- assert "Hello Camila Gutiérrez" in result.output25-26-27-def test_help():28- result = runner.invoke(app, ["--help"])29- assert result.exit_code == 030- assert "--lastname TEXT" in result.output31- assert "[required]" in result.output32-33-34-def test_script():35- result = subprocess.run(36- ["coverage", "run", mod.__file__, "--help"],37- stdout=subprocess.PIPE,38- stderr=subprocess.PIPE,39- encoding="utf-8",40- )41- assert "Usage" in result.stdoutdeleted:tests/test_tutorial/test_options/test_tutorial004.py-41 removed
1-import subprocess2-3-import typer4-from typer.testing import CliRunner5-6-from options import tutorial004 as mod7-8-runner = CliRunner()9-10-app = typer.Typer()11-app.command()(mod.main)12-13-14-def test_option_lastname():15- result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"])16- assert result.exit_code == 017- assert "Hello Camila Gutiérrez" in result.output18-19-20-def test_option_lastname_prompt():21- result = runner.invoke(app, ["Camila"], input="Gutiérrez")22- assert result.exit_code == 023- assert "Please tell me your last name: " in result.output24- assert "Hello Camila Gutiérrez" in result.output25-26-27-def test_help():28- result = runner.invoke(app, ["--help"])29- assert result.exit_code == 030- assert "--lastname TEXT" in result.output31- assert "[required]" in result.output32-33-34-def test_script():35- result = subprocess.run(36- ["coverage", "run", mod.__file__, "--help"],37- stdout=subprocess.PIPE,38- stderr=subprocess.PIPE,39- encoding="utf-8",40- )41- assert "Usage" in result.stdoutdeleted:tests/test_tutorial/test_options/test_tutorial005.py-50 removed
1-import subprocess2-3-import typer4-from typer.testing import CliRunner5-6-from options import tutorial005 as mod7-8-runner = CliRunner()9-10-app = typer.Typer()11-app.command()(mod.main)12-13-14-def test_prompt():15- result = runner.invoke(app, input="Old Project\nOld Project\n")16- assert result.exit_code == 017- assert "Deleting project Old Project" in result.output18-19-20-def test_prompt_not_equal():21- result = runner.invoke(22- app, input="Old Project\nNew Spice\nOld Project\nOld Project\n"23- )24- assert result.exit_code == 025- assert "Error: the two entered values do not match" in result.output26- assert "Deleting project Old Project" in result.output27-28-29-def test_option():30- result = runner.invoke(app, ["--project-name", "Old Project"])31- assert result.exit_code == 032- assert "Deleting project Old Project" in result.output33- assert "Project name: " not in result.output34-35-36-def test_help():37- result = runner.invoke(app, ["--help"])38- assert result.exit_code == 039- assert "--project-name TEXT" in result.output40- assert "[required]" in result.output41-42-43-def test_script():44- result = subprocess.run(45- ["coverage", "run", mod.__file__, "--help"],46- stdout=subprocess.PIPE,47- stderr=subprocess.PIPE,48- encoding="utf-8",49- )50- assert "Usage" in result.stdoutdeleted:tests/test_tutorial/test_options/test_tutorial006.py-34 removed
1-import subprocess2-3-import typer4-from typer.testing import CliRunner5-6-from options import tutorial006 as mod7-8-runner = CliRunner()9-10-app = typer.Typer()11-app.command()(mod.main)12-13-14-def test_call():15- result = runner.invoke(app)16- assert result.exit_code == 017- assert "Hello Wade Wilson" in result.output18-19-20-def test_help():21- result = runner.invoke(app, ["--help"])22- assert result.exit_code == 023- assert "--fullname TEXT" in result.output24- assert "[default: Wade Wilson]" in result.output25-26-27-def test_script():28- result = subprocess.run(29- ["coverage", "run", mod.__file__, "--help"],30- stdout=subprocess.PIPE,31- stderr=subprocess.PIPE,32- encoding="utf-8",33- )34- assert "Usage" in result.stdoutmodified:typer/params.py+4 added-2 removed
104 unmodified lines105 106 def Argument(107 # Parameter108 default: Optional[Any],109- *param_decls: str,109+ *,110 callback: Optional[Callable[[click.Context, click.Parameter, str], Any]] = None,111 metavar: Optional[str] = None,112 expose_value: bool = True,113 is_eager: bool = False,27 unmodified lines141 ) -> Any:142 return ArgumentInfo(143 # Parameter144 default=default,145+ # Arguments can only have one param declaration146+ # it will be generated from the param name145- param_decls=param_decls,147+ param_decls=None,148 callback=callback,149 metavar=metavar,150 expose_value=expose_value,151 is_eager=is_eager,26 unmodified linesmodified:mkdocs.yml+13 added-2 removed
21 unmodified lines22 - Features: 'features.md'23 - Tutorial - User Guide:24 - Tutorial - User Guide - Intro: 'tutorial/index.md'25 - First Steps: 'tutorial/first-steps.md'26- - CLI Options: 'tutorial/options.md'26 - CLI Arguments: 'tutorial/arguments.md'28- - Commands: 'tutorial/commands.md'27+ - CLI Options:28+ - CLI Options Intro: 'tutorial/options/index.md'29+ - CLI Options with Help: 'tutorial/options/help.md'30+ - Required CLI Options: 'tutorial/options/required.md'31+ - CLI Option Prompt: 'tutorial/options/prompt.md'32+ - CLI Option Name: 'tutorial/options/name.md'33+ - Commands:34+ - Commands Intro: 'tutorial/commands/index.md'35+ - Command CLI Arguments: 'tutorial/commands/arguments.md'36+ - Command CLI Options: 'tutorial/commands/options.md'37+ - Command Help: 'tutorial/commands/help.md'38+ - Custom Command Name: 'tutorial/commands/name.md'39+ - One or Multiple Commands: 'tutorial/commands/one-or-multiple.md'40 - CLI Parameter Types:41 - CLI Parameter Types Intro: 'tutorial/parameter-types/index.md'42 - Number: 'tutorial/parameter-types/number.md'43 - UUID: 'tutorial/parameter-types/uuid.md'42 unmodified lines