Skip to content

Commit f809620

Browse files
committed
only do script check if called from the command line, better addresses #40
1 parent 7d513f2 commit f809620

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

django_typer/__init__.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import typing as t
7070
from copy import deepcopy
7171
from importlib import import_module
72+
from pathlib import Path
7273
from types import MethodType, SimpleNamespace
7374

7475
import click
@@ -1846,7 +1847,16 @@ def create_parser( # pyright: ignore[reportIncompatibleMethodOverride]
18461847
:param subcommand: the name of the django command
18471848
"""
18481849
with self:
1849-
return TyperParser(self, get_usage_script(prog_name), subcommand)
1850+
if getattr(self, "_called_from_command_line", False):
1851+
script = get_usage_script(prog_name)
1852+
if isinstance(script, Path):
1853+
prog_name = str(script)
1854+
if not str(prog_name).startswith(("..", "/", ".")):
1855+
prog_name = f"./{prog_name}"
1856+
else:
1857+
prog_name = str(script)
1858+
1859+
return TyperParser(self, prog_name, subcommand)
18501860

18511861
def print_help(self, prog_name: str, subcommand: str, *cmd_path: str):
18521862
"""
@@ -1859,7 +1869,7 @@ def print_help(self, prog_name: str, subcommand: str, *cmd_path: str):
18591869
typer/click have different helps for each subgroup or subcommand.
18601870
"""
18611871
with self:
1862-
TyperParser(self, prog_name, subcommand).print_help(*cmd_path)
1872+
self.create_parser(prog_name, subcommand).print_help(*cmd_path)
18631873

18641874
def __call__(self, *args, **kwargs):
18651875
"""

django_typer/tests/tests.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,9 @@ def test_helps(self, top_level_only=False):
423423
buffer = StringIO()
424424
cmd = get_command(self.cmd_name, stdout=buffer, no_color=True)
425425
help_output_top = run_command(self.cmd_name, "--no-color", "--help")[0]
426-
cmd.print_help("manage.py", self.cmd_name)
426+
cmd.print_help("./manage.py", self.cmd_name)
427427
self.assertEqual(help_output_top.strip(), buffer.getvalue().strip())
428-
self.assertIn(f"Usage: manage.py {self.cmd_name} [OPTIONS]", help_output_top)
428+
self.assertIn(f"Usage: ./manage.py {self.cmd_name} [OPTIONS]", help_output_top)
429429

430430
if not top_level_only:
431431
buffer.truncate(0)

0 commit comments

Comments
 (0)