forked from fastapi/typer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Get help for params from docstrings
Resolves issue fastapi#336.
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import typer | ||
from typer.testing import CliRunner | ||
|
||
runner = CliRunner() | ||
|
||
app = typer.Typer() | ||
|
||
|
||
@app.command() | ||
def cmd( | ||
arg1: float, | ||
arg2: str = typer.Argument("bar"), | ||
opt1: str = typer.Option("foo"), | ||
opt2: bool = typer.Option(False), | ||
opt3: int = typer.Option(1), | ||
): | ||
""" | ||
Some command | ||
:param opt1: First option | ||
:param opt2: Second option | ||
:param opt3: | ||
Third | ||
option | ||
:param arg1: First argument | ||
:param arg2: Second argument | ||
""" | ||
|
||
|
||
@app.callback() | ||
def main(global_opt: bool = False): | ||
""" | ||
Callback | ||
:param global_opt: Global option | ||
""" | ||
|
||
|
||
def test_help_main(): | ||
result = runner.invoke(app, ["--help"]) | ||
assert result.exit_code == 0 | ||
assert "Callback" in result.output | ||
assert ":param" not in result.output | ||
assert "--global-opt / --no-global-opt" in result.output | ||
assert "Global option" in result.output | ||
assert "[default: no-global-opt]" in result.output | ||
|
||
|
||
def test_help_cmd(): | ||
result = runner.invoke(app, ["cmd", "--help"]) | ||
assert result.exit_code == 0 | ||
assert "Some command" in result.output | ||
assert ":param" not in result.output | ||
assert "ARG1" in result.output | ||
assert "First argument" in result.output | ||
assert "[required]" in result.output | ||
assert "[ARG2]" in result.output | ||
assert "Second argument" in result.output | ||
assert "[default: bar]" in result.output | ||
assert "--opt1 TEXT" in result.output | ||
assert "First option" in result.output | ||
assert "[default: foo]" in result.output | ||
assert "--opt2 / --no-opt2" in result.output | ||
assert "Second option" in result.output | ||
assert "[default: no-opt2]" in result.output | ||
assert "--opt3 INTEGER" in result.output | ||
assert "Third option" in result.output | ||
assert "[default: 1]" in result.output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import docutils.frontend | ||
import docutils.nodes | ||
import docutils.parsers.rst | ||
import docutils.utils | ||
|
||
|
||
def parse(text: str) -> docutils.nodes.document: | ||
parser = docutils.parsers.rst.Parser() | ||
settings = docutils.frontend.OptionParser( | ||
components=(docutils.parsers.rst.Parser,) | ||
).get_default_values() | ||
document = docutils.utils.new_document("<rst-doc>", settings=settings) | ||
parser.parse(text, document) | ||
return document |