Skip to content
Myr

:memo: Update First Steps to include how to document in a docstring

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

authored by Sebastián Ramírez · committed by Sebastián Ramírez · parent 8bf9ecc · Browse files · 2 files+59 added−2 removed
This commit

State

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

Diff stats

2 files+59 added−2 removed

  • other:+59 added−2 removed

2 files+59 added−2 removed

added:docs/src/first_steps/tutorial006.py+17 added
1import typer234def main(name: str, lastname: str = "", formal: bool = False):5 """6 Say hi to NAME, optionally with a --lastname.78 If --formal is used, say hi very formally.9 """10 if formal:11 typer.echo(f"Good day Ms. {name} {lastname}.")12 else:13 typer.echo(f"Hello {name} {lastname}")141516if __name__ == "__main__":17 typer.run(main)
modified:docs/tutorial/first-steps.md+42 added-2 removed
20 unmodified lines
2122// Now check the --help23$ python main.py --help2425Usage: main.py [OPTIONS]25Usage: tryit.py [OPTIONS]2627Options:28 --install-completion Install completion for the current shell.29 --show-completion Show completion for the current shell, to copy it or customize the installation.28 --help Show this message and exit.30 --help Show this message and exit.31```3233</div>34
268 unmodified lines
303```304305</div>306307## Document your CLI app308309If you add a <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr> to your function it will be used in the help text:310311```Python hl_lines="5 6 7 8 9"312{!./src/first_steps/tutorial006.py!}313```314315Now see it with the `--help` option:316317<div class="termy">318319```console320$ python main.py --help321322Usage: tutorial006.py [OPTIONS] NAME323324 Say hi to NAME, optionally with a --lastname.325326 If --formal is used, say hi very formally.327328Options:329 --lastname TEXT330 --formal / --no-formal331 --install-completion Install completion for the current shell.332 --show-completion Show completion for the current shell, to copy it or customize the installation.333 --help Show this message and exit.334```335336</div>337338!!! tip339 You should document the *CLI arguments* in the docstring.340341 There is another place to document the *CLI options* that will show up next to them in the help text as with `--install-completion` or `--help`, you will learn that later in the tutorial.342343 But *CLI arguments* are normally used for the most necessary things, so you should document them here in the *docstring*.344345## Arguments, options, parameters, optional, required346347Be aware that these terms refer to multiple things depending on the context, and sadly, those "contexts" mix frequently, so it's easy to get confused.348
65 unmodified lines