Skip to content

Commit ddbf033

Browse files
committed
📝 Add README and CONTRIBUTING
1 parent 6512713 commit ddbf033

File tree

2 files changed

+299
-4
lines changed

2 files changed

+299
-4
lines changed

CONTRIBUTING.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Please read the [Development - Contributing](https://typer.tiangolo.com/contributing/) guidelines in the documentation site.

README.md

+298-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,304 @@
1-
# Typer
1+
<p align="center">
2+
<a href="https://typer.tiangolo.com"><img src="/img/logo-margin/logo-margin-vector.svg" alt="Typer"></a>
3+
</p>
4+
<p align="center">
5+
<em>Typer: CLIs with autocompletion. While developing and using.</em>
6+
</p>
7+
<p align="center">
8+
<a href="https://travis-ci.org/tiangolo/typer" target="_blank">
9+
<img src="https://travis-ci.org/tiangolo/typer.svg?branch=master" alt="Build Status">
10+
</a>
11+
<a href="https://codecov.io/gh/tiangolo/typer" target="_blank">
12+
<img src="https://codecov.io/gh/tiangolo/typer/branch/master/graph/badge.svg" alt="Coverage">
13+
</a>
14+
<a href="https://pypi.org/project/typer" target="_blank">
15+
<img src="https://badge.fury.io/py/typer.svg" alt="Package version">
16+
</a>
17+
</p>
218

3-
An intuitive CLI library based on optional type hints.
19+
---
420

5-
The [FastAPI](https://fastapi.tiangolo.com/) of CLIs.
21+
**Documentation**: <a href="https://typer.tiangolo.com" target="_blank">https://typer.tiangolo.com</a>
622

7-
Work in progress.
23+
**Source Code**: <a href="https://github.com/tiangolo/typer" target="_blank">https://github.com/tiangolo/typer</a>
24+
25+
---
26+
27+
Typer is library to build <abbr title="command line interface, programs executed from a terminal">CLI</abbr> applications that users love using and developers love creating. Based on Python 3.6+ type hints.
28+
29+
**Typer** is the little sibling of <a href="https://fastapi.tiangolo.com" target="_blank">FastAPI</a>. And it's intended to be the FastAPI of CLIs.
30+
31+
The key features are:
32+
33+
* **Intuitive to write**: Great editor support. <abbr title="also known as auto-complete, autocompletion, IntelliSense">Completion</abbr> everywhere. Less time debugging. Designed to be easy to use and learn. Less time reading docs.
34+
* **Easy to use**: It's easy to use for the final users. Automatic help commands, and (optional) automatic completion for all shells.
35+
* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
36+
* **Start simple**: The simplest example adds only 2 lines of code to your app: **1 import, 1 function call**.
37+
* **Grow large**: Grow in complexity as much as you want create arbitrarily complex trees of commands and groups sub-commands, with options and arguments.
38+
39+
## Requirements
40+
41+
Python 3.6+
42+
43+
Typer stands on the shoulders of a giant. Internally it uses <a href="https://click.palletsprojects.com/" target="_blank">Click</a>, that's the only dependency.
44+
45+
## Installation
46+
47+
```bash
48+
pip install typer
49+
```
50+
51+
## Example
52+
53+
### The absolute minimum
54+
55+
* Create a file `main.py` with:
56+
57+
```Python
58+
import typer
59+
60+
61+
def main(name: str):
62+
typer.echo(f"Hello {name}")
63+
64+
65+
if __name__ == "__main__":
66+
typer.run(main)
67+
```
68+
69+
### Run it
70+
71+
Run your application:
72+
73+
```bash
74+
python main.py
75+
```
76+
77+
you will get a response like:
78+
79+
```
80+
Usage: main.py [OPTIONS] NAME
81+
Try "main.py --help" for help.
82+
83+
Error: Missing argument "NAME".
84+
```
85+
86+
Now pass the `NAME` *argument*:
87+
88+
```bash
89+
python main.py Camila
90+
```
91+
92+
You will get a response like:
93+
94+
```
95+
Hello Camila
96+
```
97+
98+
And you automatically get a `--help` command:
99+
100+
```bash
101+
python main.py --help
102+
```
103+
104+
shows:
105+
106+
```
107+
Usage: main.py [OPTIONS] NAME
108+
109+
Options:
110+
--help Show this message and exit.
111+
```
112+
113+
## Example upgrade
114+
115+
The previous example was the extreme in terms of simplicity.
116+
117+
Now let's see one a bit more complex.
118+
119+
### An example with two sub-commands
120+
121+
Modify the file `main.py`.
122+
123+
Create a `typer.Typer()` app, and create two sub-commands with their parameters.
124+
125+
```Python hl_lines="3 6 11 20"
126+
import typer
127+
128+
app = typer.Typer()
129+
130+
131+
@app.command()
132+
def hello(name: str):
133+
typer.echo(f"Hello {name}")
134+
135+
136+
@app.command()
137+
def goodbye(name: str, formal: bool = False):
138+
if formal:
139+
typer.echo(f"Goodbye Ms. {name}. Have a good day.")
140+
else:
141+
typer.echo(f"Bye {name}!")
142+
143+
144+
if __name__ == "__main__":
145+
app()
146+
```
147+
148+
And that will:
149+
150+
* Explicitly create a `typer.Typer` app.
151+
* The previous `typer.run` actually creates one implicitly for you.
152+
* Add two sub-commands with `@app.command()`.
153+
* Execute the `app()` itself, as if it was a function (instead of `typer.run`).
154+
155+
### Run the upgraded example
156+
157+
Get the main `--help`:
158+
159+
```bash
160+
python main.py --help
161+
```
162+
163+
shows:
164+
165+
```
166+
Usage: main.py [OPTIONS] COMMAND [ARGS]...
167+
168+
Options:
169+
--help Show this message and exit.
170+
171+
Commands:
172+
goodbye
173+
hello
174+
```
175+
176+
You have 2 sub-commands (the 2 functions), `goodbye` and `hello`.
177+
178+
Now get the help for `hello`:
179+
180+
```bash
181+
python main.py hello --help
182+
```
183+
184+
shows:
185+
186+
```
187+
Usage: main.py hello [OPTIONS] NAME
188+
189+
Options:
190+
--help Show this message and exit.
191+
```
192+
193+
And now get the help for `goodbye`:
194+
195+
```bash
196+
python main.py goodbye --help
197+
```
198+
199+
shows:
200+
201+
```
202+
Usage: main.py goodbye [OPTIONS] NAME
203+
204+
Options:
205+
--formal / --no-formal
206+
--help Show this message and exit.
207+
```
208+
209+
Notice how it automatically creates a `--formal` and `--no-formal` for your `bool` *option*.
210+
211+
---
212+
213+
And of course, if you use it, it does what you expect:
214+
215+
```bash
216+
python main.py hello Camila
217+
```
218+
219+
shows:
220+
221+
```
222+
Hello Camila
223+
```
224+
225+
Then:
226+
227+
```bash
228+
python main.py goodbye Camila
229+
```
230+
231+
shows:
232+
233+
```
234+
Bye Camila!
235+
```
236+
237+
And:
238+
239+
```bash
240+
python main.py goodbye --formal Camila
241+
```
242+
243+
shows:
244+
245+
```
246+
Goodbye Ms. Camila. Have a good day.
247+
```
248+
249+
### Recap
250+
251+
In summary, you declare **once** the types of parameters (*arguments* and *options*) as function parameters.
252+
253+
You do that with standard modern Python types.
254+
255+
You don't have to learn a new syntax, the methods or classes of a specific library, etc.
256+
257+
Just standard **Python 3.6+**.
258+
259+
For example, for an `int`:
260+
261+
```Python
262+
total: int
263+
```
264+
265+
or for a `bool` flag:
266+
267+
```Python
268+
force: bool
269+
```
270+
271+
And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of sub-commands**, add metadata, extra **validation**, etc.
272+
273+
**You get**: great editor support, including **completion** and **type checks** everywhere.
274+
275+
**Your users get**: automatic **`--help`**, (optional) **autocompletion** in their terminal (Bash, Zsh, Fish, PowerShell).
276+
277+
For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/intro/">Tutorial - User Guide</a>.
278+
279+
## Optional Dependencies
280+
281+
Typer uses <a href="https://click.palletsprojects.com/" target="_blank">Click</a> internally. That's the only dependency.
282+
283+
But you can install extras:
284+
285+
* <a href="https://pypi.org/project/colorama/" target="_blank"><code>colorama</code></a>: and Click will automatically use it to make sure colors always work correctly, even in Windows.
286+
* Then you can use any tool you want to output colors in all the systems, including the integrated `typer.style()` and `typer.secho()` (provided by Click).
287+
* Or any other tool, e.g. <a href="https://pypi.org/project/wasabi/" target="_blank"><code>wasabi</code></a>, <a href="https://github.com/erikrose/blessings" target="_blank"><code>blessings</code></a>.
288+
* <a href="https://github.com/click-contrib/click-completion" target="_blank"><code>click-completion</code></a>: and Typer will automatically configure it to provide completion for all the shells, including installation commands.
289+
290+
You can install `typer` with `colorama` and `click-completion` with `pip3 install typer[all]`.
291+
292+
## Other tools and plug-ins
293+
294+
Click has many plug-ins available that you can use. And there are many tools that help with command line applications that you can use as well, even if they are not related to Typer or Click.
295+
296+
For example:
297+
298+
* <a href="https://github.com/click-contrib/click-spinner" target="_blank"><code>click-spinner</code></a>: to show the user that you are loading data. A Click plug-in.
299+
* * There are several other Click plug-ins at <a href="https://github.com/click-contrib" target="_blank">click-contrib</a> that you can explore.
300+
* <a href="https://pypi.org/project/tabulate/" target="_blank"><code>tabulate</code></a>: to automatically display tabular data nicely. Independent of Click or typer.
301+
* etc... you can re-use many of the great available tools for building CLIs.
8302

9303
## License
10304

0 commit comments

Comments
 (0)