typer 0.21.1
pip install typer
Released:
Typer, build great CLIs. Easy to code. Based on Python type hints.
Navigation
Verified details
These details have beenverified by PyPIProject links
GitHub Statistics
Maintainers
Unverified details
These details havenot been verified by PyPIProject links
Meta
- License Expression: MIT
SPDXLicense Expression - Author:Sebastián Ramírez
- Requires: Python >=3.9
Classifiers
- Development Status
- Intended Audience
- Operating System
- Programming Language
- Topic
- Typing
Project description
Typer, build great CLIs. Easy to code. Based on Python type hints.
Documentation:https://typer.tiangolo.com
Source Code:https://github.com/fastapi/typer
Typer is a library for buildingCLI applications that users willlove using and developers willlove creating. Based on Python type hints.
It's also a command line tool to run scripts, automatically converting them to CLI applications.
The key features are:
- Intuitive to write: Great editor support.Completion everywhere. Less time debugging. Designed to be easy to use and learn. Less time reading docs.
- Easy to use: It's easy to use for the final users. Automatic help, and automatic completion for all shells.
- Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
- Start simple: The simplest example adds only 2 lines of code to your app:1 import, 1 function call.
- Grow large: Grow in complexity as much as you want, create arbitrarily complex trees of commands and groups of subcommands, with options and arguments.
- Run scripts: Typer includes a
typercommand/program that you can use to run scripts, automatically converting them to CLIs, even if they don't use Typer internally.
FastAPI of CLIs
Typer isFastAPI's little sibling, it's the FastAPI of CLIs.
Installation
Create and activate avirtual environment and then installTyper:
$pipinstalltyper---> 100%Successfully installed typer rich shellinghamExample
The absolute minimum
- Create a file
main.pywith:
defmain(name:str):print(f"Hello{name}")This script doesn't even use Typer internally. But you can use thetyper command to run it as a CLI application.
Run it
Run your application with thetyper command:
// Run your application$typermain.pyrun// You get a nice error, you are missing NAMEUsage: typer [PATH_OR_MODULE] run [OPTIONS] NAMETry 'typer [PATH_OR_MODULE] run --help' for help.╭─ Error ───────────────────────────────────────────╮│ Missing argument 'NAME'. │╰───────────────────────────────────────────────────╯// You get a --help for free$typermain.pyrun--helpUsage: typer [PATH_OR_MODULE] run [OPTIONS] NAMERun the provided Typer app.╭─ Arguments ───────────────────────────────────────╮│ * name TEXT [default: None] [required] |╰───────────────────────────────────────────────────╯╭─ Options ─────────────────────────────────────────╮│ --help Show this message and exit. │╰───────────────────────────────────────────────────╯// Now pass the NAME argument$typermain.pyrunCamilaHello Camila// It works! 🎉This is the simplest use case, not even using Typer internally, but it can already be quite useful for simple scripts.
Note: auto-completion works when you create a Python package and run it with--install-completion or when you use thetyper command.
Use Typer in your code
Now let's start using Typer in your own code, updatemain.py with:
importtyperdefmain(name:str):print(f"Hello{name}")if__name__=="__main__":typer.run(main)Now you could run it with Python directly:
// Run your application$pythonmain.py// You get a nice error, you are missing NAMEUsage: main.py [OPTIONS] NAMETry 'main.py --help' for help.╭─ Error ───────────────────────────────────────────╮│ Missing argument 'NAME'. │╰───────────────────────────────────────────────────╯// You get a --help for free$pythonmain.py--helpUsage: main.py [OPTIONS] NAME╭─ Arguments ───────────────────────────────────────╮│ * name TEXT [default: None] [required] |╰───────────────────────────────────────────────────╯╭─ Options ─────────────────────────────────────────╮│ --help Show this message and exit. │╰───────────────────────────────────────────────────╯// Now pass the NAME argument$pythonmain.pyCamilaHello Camila// It works! 🎉Note: you can also call this same script with thetyper command, but you don't need to.
Example upgrade
This was the simplest example possible.
Now let's see one a bit more complex.
An example with two subcommands
Modify the filemain.py.
Create atyper.Typer() app, and create two subcommands with their parameters.
importtyperapp=typer.Typer()@app.command()defhello(name:str):print(f"Hello{name}")@app.command()defgoodbye(name:str,formal:bool=False):ifformal:print(f"Goodbye Ms.{name}. Have a good day.")else:print(f"Bye{name}!")if__name__=="__main__":app()And that will:
- Explicitly create a
typer.Typerapp.- The previous
typer.runactually creates one implicitly for you.
- The previous
- Add two subcommands with
@app.command(). - Execute the
app()itself, as if it was a function (instead oftyper.run).
Run the upgraded example
Check the new help:
$pythonmain.py--help Usage: main.py [OPTIONS] COMMAND [ARGS]...╭─ Options ─────────────────────────────────────────╮│ --install-completion Install completion ││ for the current ││ shell. ││ --show-completion Show completion for ││ the current shell, ││ to copy it or ││ customize the ││ installation. ││ --help Show this message ││ and exit. │╰───────────────────────────────────────────────────╯╭─ Commands ────────────────────────────────────────╮│ goodbye ││ hello │╰───────────────────────────────────────────────────╯// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion// You have 2 subcommands (the 2 functions): goodbye and helloNow check the help for thehello command:
$pythonmain.pyhello--help Usage: main.py hello [OPTIONS] NAME╭─ Arguments ───────────────────────────────────────╮│ * name TEXT [default: None] [required] │╰───────────────────────────────────────────────────╯╭─ Options ─────────────────────────────────────────╮│ --help Show this message and exit. │╰───────────────────────────────────────────────────╯And now check the help for thegoodbye command:
$pythonmain.pygoodbye--help Usage: main.py goodbye [OPTIONS] NAME╭─ Arguments ───────────────────────────────────────╮│ * name TEXT [default: None] [required] │╰───────────────────────────────────────────────────╯╭─ Options ─────────────────────────────────────────╮│ --formal --no-formal [default: no-formal] ││ --help Show this message ││ and exit. │╰───────────────────────────────────────────────────╯// Automatic --formal and --no-formal for the bool option 🎉Now you can try out the new command line application:
// Use it with the hello command$pythonmain.pyhelloCamilaHello Camila// And with the goodbye command$pythonmain.pygoodbyeCamilaBye Camila!// And with --formal$pythonmain.pygoodbye--formalCamilaGoodbye Ms. Camila. Have a good day.Note: If your app only has one command, by default the command name isomitted in usage:python main.py Camila. However, when there are multiple commands, you mustexplicitly include the command name:python main.py hello Camila. SeeOne or Multiple Commands for more details.
Recap
In summary, you declareonce the types of parameters (CLI arguments andCLI options) as function parameters.
You do that with standard modern Python types.
You don't have to learn a new syntax, the methods or classes of a specific library, etc.
Just standardPython.
For example, for anint:
total:intor for abool flag:
force:boolAnd similarly forfiles,paths,enums (choices), etc. And there are tools to creategroups of subcommands, add metadata, extravalidation, etc.
You get: great editor support, includingcompletion andtype checks everywhere.
Your users get: automatic--help,auto-completion in their terminal (Bash, Zsh, Fish, PowerShell) when they install your package or when using thetyper command.
For a more complete example including more features, see theTutorial - User Guide.
Dependencies
Typer stands on the shoulders of a giant. Its only internal required dependency isClick.
By default it also comes with extra standard dependencies:
rich: to show nicely formatted errors automatically.shellingham: to automatically detect the current shell when installing completion.- With
shellinghamyou can just use--install-completion. - Without
shellingham, you have to pass the name of the shell to install completion for, e.g.--install-completion bash.
- With
typer-slim
If you don't want the extra standard optional dependencies, installtyper-slim instead.
When you install with:
pipinstalltyper...it includes the same code and dependencies as:
pipinstall"typer-slim[standard]"Thestandard extra dependencies arerich andshellingham.
Note: Thetyper command is only included in thetyper package.
License
This project is licensed under the terms of the MIT license.
Project details
Verified details
These details have beenverified by PyPIProject links
GitHub Statistics
Maintainers
Unverified details
These details havenot been verified by PyPIProject links
Meta
- License Expression: MIT
SPDXLicense Expression - Author:Sebastián Ramírez
- Requires: Python >=3.9
Classifiers
- Development Status
- Intended Audience
- Operating System
- Programming Language
- Topic
- Typing
Release historyRelease notifications |RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more aboutinstalling packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more aboutwheel file names.
Copy a direct link to the current filters
File details
Details for the filetyper-0.21.1.tar.gz.
File metadata
- Download URL:typer-0.21.1.tar.gz
- Upload date:
- Size: 110.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 | ea835607cd752343b6b2b7ce676893e5a0324082268b48f27aa058bdb7d2145d | |
| MD5 | 571a791e2806179e4220cc4135aa3388 | |
| BLAKE2b-256 | 36bf8825b5929afd84d0dabd606c67cd57b8388cb3ec385f7ef19c5cc2202069 |
File details
Details for the filetyper-0.21.1-py3-none-any.whl.
File metadata
- Download URL:typer-0.21.1-py3-none-any.whl
- Upload date:
- Size: 47.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 | 7985e89081c636b88d172c2ee0cfe33c253160994d47bdfdc302defd7d1f1d01 | |
| MD5 | 12b8b15f8fcc99dd606d2918f3f71cab | |
| BLAKE2b-256 | a01dd9257dd49ff2ca23ea5f132edf1281a0c4f9de8a762b9ae399b670a59235 |