Skip to content

Commit 2c94075

Browse files
author
Brian Kohan
committed
fix code example formatting #48
1 parent db1fd3e commit 2c94075

File tree

1 file changed

+74
-37
lines changed

1 file changed

+74
-37
lines changed

README.md

Lines changed: 74 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,36 @@ class Command(TyperCommand):
7777
Commands with multiple subcommands can be defined:
7878

7979
```python
80-
import typing as t
81-
from django.utils.translation import gettext_lazy as _
82-
from typer import Argument
83-
from django_typer import TyperCommand, command
80+
import typing as t
8481

85-
class Command(TyperCommand):
86-
@command()
87-
def create(self, name: t.Annotated[str, Argument(help=_("The name of the object to create."))]):
88-
"""
89-
Create an object.
90-
"""
82+
from django.utils.translation import gettext_lazy as _
83+
from typer import Argument
84+
85+
from django_typer import TyperCommand, command
86+
87+
88+
class Command(TyperCommand):
89+
"""
90+
A command that defines subcommands.
91+
"""
92+
93+
@command()
94+
def create(
95+
self,
96+
name: t.Annotated[str, Argument(help=_("The name of the object to create."))],
97+
):
98+
"""
99+
Create an object.
100+
"""
101+
102+
@command()
103+
def delete(
104+
self, id: t.Annotated[int, Argument(help=_("The id of the object to delete."))]
105+
):
106+
"""
107+
Delete an object.
108+
"""
91109

92-
@command()
93-
def delete(self, id: t.Annotated[int, Argument(help=_("The id of the object to delete."))]):
94-
"""
95-
Delete an object.
96-
"""
97110
```
98111

99112
![Multiple Subcommands Example](https://raw.githubusercontent.com/bckohan/django-typer/main/django_typer/examples/helps/multi.svg)
@@ -110,29 +123,53 @@ More complex groups and subcommand hierarchies can be defined. For example, this
110123
```
111124

112125
```python
113-
import typing as t
114-
from functools import reduce
115-
from django.utils.translation import gettext_lazy as _
116-
from typer import Argument, Option
117-
from django_typer import TyperCommand, group
118-
119-
class Command(TyperCommand):
120-
@group(help=_("Do some math at the given precision."))
121-
def math(self, precision: t.Annotated[int, Option(help=_("The number of decimal places to output."))] = 2):
122-
self.precision = precision
126+
import typing as t
127+
from functools import reduce
128+
129+
from django.utils.translation import gettext_lazy as _
130+
from typer import Argument, Option
131+
132+
from django_typer import TyperCommand, group
133+
134+
135+
class Command(TyperCommand):
136+
137+
help = _("A more complex command that defines a hierarchy of subcommands.")
138+
139+
precision = 2
140+
141+
@group(help=_("Do some math at the given precision."))
142+
def math(
143+
self,
144+
precision: t.Annotated[
145+
int, Option(help=_("The number of decimal places to output."))
146+
] = precision,
147+
):
148+
self.precision = precision
149+
150+
@math.command(help=_("Multiply the given numbers."))
151+
def multiply(
152+
self,
153+
numbers: t.Annotated[
154+
t.List[float], Argument(help=_("The numbers to multiply"))
155+
],
156+
):
157+
return f"{reduce(lambda x, y: x * y, [1, *numbers]):.{self.precision}f}"
158+
159+
@math.command()
160+
def divide(
161+
self,
162+
numerator: t.Annotated[float, Argument(help=_("The numerator"))],
163+
denominator: t.Annotated[float, Argument(help=_("The denominator"))],
164+
floor: t.Annotated[bool, Option(help=_("Use floor division"))] = False,
165+
):
166+
"""
167+
Divide the given numbers.
168+
"""
169+
if floor:
170+
return str(numerator // denominator)
171+
return f"{numerator / denominator:.{self.precision}f}"
123172

124-
@math.command(help=_("Multiply the given numbers."))
125-
def multiply(self, numbers: t.Annotated[t.List[float], Argument(help=_("The numbers to multiply"))]):
126-
return f"{reduce(lambda x, y: x * y, [1, *numbers]):.{self.precision}f}"
127-
128-
@math.command()
129-
def divide(self, numerator: float, denominator: float, floor: bool = False):
130-
"""
131-
Divide the given numbers.
132-
"""
133-
if floor:
134-
return str(numerator // denominator)
135-
return f"{numerator / denominator:.{self.precision}f}"
136173
```
137174

138175
![Grouping and Hierarchies Example](https://raw.githubusercontent.com/bckohan/django-typer/main/django_typer/examples/helps/hierarchy.svg)

0 commit comments

Comments
 (0)