-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
382 additions
and
13 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
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,17 @@ | ||
import typing as t | ||
from django_typer.management import TyperCommand, command, finalize | ||
|
||
|
||
# chain=True allows multiple subroutines to be called from the command line | ||
class Command(TyperCommand, chain=True): | ||
@finalize() | ||
def to_csv(self, results: t.List[str]): | ||
return ", ".join(results) | ||
|
||
@command() | ||
def cmd1(self): | ||
return "result1" | ||
|
||
@command() | ||
def cmd2(self): | ||
return "result2" |
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,37 @@ | ||
import typing as t | ||
from django_typer.management import TyperCommand, command, finalize, group | ||
|
||
|
||
class Command(TyperCommand, chain=True): | ||
""" | ||
Show that finalizers are hierarchical and results are collected and | ||
passed to the finalizer of the parent group if one exists. | ||
""" | ||
|
||
@finalize() | ||
def to_csv(self, results: t.List[str]): | ||
return ", ".join(results) | ||
|
||
@command() | ||
def cmd1(self): | ||
return "result1" | ||
|
||
@command() | ||
def cmd2(self): | ||
return "result2" | ||
|
||
@group(chain=True) | ||
def grp(self): | ||
return "grp" | ||
|
||
@grp.finalize() | ||
def to_upper_csv(self, results): | ||
return ", ".join([result.upper() for result in results]) | ||
|
||
@grp.command() | ||
def cmd3(self): | ||
return "result3" | ||
|
||
@grp.command() | ||
def cmd4(self): | ||
return "result4" |
36 changes: 36 additions & 0 deletions
36
tests/apps/howto/management/commands/finalize_group_typer.py
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,36 @@ | ||
from django_typer.management import Typer | ||
|
||
# use native Typer interface to achieve the same result | ||
|
||
def to_csv(results, **_): | ||
return ", ".join(results) | ||
|
||
|
||
def to_upper_csv(results, **_): | ||
return ", ".join([result.upper() for result in results]) | ||
|
||
|
||
app = Typer(result_callback=to_csv, chain=True) | ||
|
||
grp = Typer(result_callback=to_upper_csv, chain=True) | ||
app.add_typer(grp, name="grp") | ||
|
||
|
||
@app.command() | ||
def cmd1(): | ||
return "result1" | ||
|
||
|
||
@app.command() | ||
def cmd2(): | ||
return "result2" | ||
|
||
|
||
@grp.command() | ||
def cmd3(): | ||
return "result3" | ||
|
||
|
||
@grp.command() | ||
def cmd4(): | ||
return "result4" |
40 changes: 40 additions & 0 deletions
40
tests/apps/howto/management/commands/finalize_group_typer_ext.py
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,40 @@ | ||
from django_typer.management import Typer | ||
|
||
# Use extensions to the typer interface to improve clarity | ||
|
||
app = Typer(chain=True) | ||
|
||
|
||
@app.finalize() | ||
def to_csv(results): | ||
return ", ".join(results) | ||
|
||
|
||
@app.group(chain=True) | ||
def grp(): | ||
pass | ||
|
||
|
||
@grp.finalize() | ||
def to_upper_csv(results): | ||
return ", ".join([result.upper() for result in results]) | ||
|
||
|
||
@app.command() | ||
def cmd1(): | ||
return "result1" | ||
|
||
|
||
@app.command() | ||
def cmd2(): | ||
return "result2" | ||
|
||
|
||
@grp.command() | ||
def cmd3(): | ||
return "result3" | ||
|
||
|
||
@grp.command() | ||
def cmd4(): | ||
return "result4" |
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,20 @@ | ||
from django_typer.management import Typer | ||
|
||
|
||
def to_csv(results, **_): | ||
# result_callback is passed the CLI parameters on the current context | ||
# if we are uninterested in them, we can use the **_ syntax to ignore them | ||
return ", ".join(results) | ||
|
||
|
||
app = Typer(result_callback=to_csv, chain=True) | ||
|
||
|
||
@app.command() | ||
def cmd1(): | ||
return "result1" | ||
|
||
|
||
@app.command() | ||
def cmd2(): | ||
return "result2" |
22 changes: 22 additions & 0 deletions
22
tests/apps/howto/management/commands/finalize_typer_ext.py
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,22 @@ | ||
from django_typer.management import Typer | ||
|
||
# alternatively we can use the finalize nomenclature of the TyperCommand | ||
# interface - this is a non-standard Typer extension | ||
|
||
app = Typer(chain=True) | ||
|
||
|
||
# The Typer interface is extended with the finalize decorator | ||
@app.finalize() | ||
def to_csv(results): | ||
return ", ".join(results) | ||
|
||
|
||
@app.command() | ||
def cmd1(): | ||
return "result1" | ||
|
||
|
||
@app.command() | ||
def cmd2(): | ||
return "result2" |
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
Oops, something went wrong.