📝 Add docs for get_app_dir()
This commit is contained in:
commit
e4d6047cf8
3 changed files with 52 additions and 0 deletions
16
docs/src/app_dir/tutorial001.py
Normal file
16
docs/src/app_dir/tutorial001.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
from pathlib import Path
|
||||
|
||||
import typer
|
||||
|
||||
APP_NAME = "my-super-cli-app"
|
||||
|
||||
|
||||
def main():
|
||||
app_dir = typer.get_app_dir(APP_NAME)
|
||||
config_path = Path(app_dir) / "config.json"
|
||||
if not config_path.is_file():
|
||||
typer.echo("Config file doesn't exist yet")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
typer.run(main)
|
35
docs/tutorial/app-dir.md
Normal file
35
docs/tutorial/app-dir.md
Normal file
|
@ -0,0 +1,35 @@
|
|||
You can get the application directory where you can, for example, save configuration files with `typer.get_app_dir()`:
|
||||
|
||||
```Python hl_lines="9"
|
||||
{!./src/app_dir/tutorial001.py!}
|
||||
```
|
||||
|
||||
It will give you a directory for storing configurations appropriate for your CLI program for the current user in each operating system.
|
||||
|
||||
Check it:
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ python main.py
|
||||
|
||||
Config file doesn't exist yet
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
## About `Path`
|
||||
|
||||
If you hadn't seen something like that:
|
||||
|
||||
```Python
|
||||
Path(app_dir) / "config.json"
|
||||
```
|
||||
|
||||
A `Path` object can be used with `/` and it will convert it to the separator for the current system (`/` for Unix systems and `\` for Windows).
|
||||
|
||||
If the first element is a `Path` object the next ones (after the `/`) can be `str`.
|
||||
|
||||
And it will create a new `Path` object from that.
|
||||
|
||||
If you want a quick guide on using `Path()` you can check <a href="https://realpython.com/python-pathlib/" target="_blank">this post on Real Python</a> or <a href="https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/" target="_blank">this post by Trey Hunner</a>.
|
|
@ -49,6 +49,7 @@ nav:
|
|||
- File: 'tutorial/parameter-types/file.md'
|
||||
- Ask with Prompt: 'tutorial/prompt.md'
|
||||
- Progress Bar: 'tutorial/progressbar.md'
|
||||
- CLI Application Directory: 'tutorial/app-dir.md'
|
||||
- Alternatives, Inspiration and Comparisons: 'alternatives.md'
|
||||
- Help Typer - Get Help: 'help-typer.md'
|
||||
- Development - Contributing: 'contributing.md'
|
||||
|
|
Loading…
Reference in a new issue