Skip to content

Commit

Permalink
only do script check if called from the command line, better addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Mar 13, 2024
1 parent 7d513f2 commit f809620
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 12 additions & 2 deletions django_typer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import typing as t
from copy import deepcopy
from importlib import import_module
from pathlib import Path
from types import MethodType, SimpleNamespace

import click
Expand Down Expand Up @@ -1846,7 +1847,16 @@ def create_parser( # pyright: ignore[reportIncompatibleMethodOverride]
:param subcommand: the name of the django command
"""
with self:
return TyperParser(self, get_usage_script(prog_name), subcommand)
if getattr(self, "_called_from_command_line", False):
script = get_usage_script(prog_name)
if isinstance(script, Path):
prog_name = str(script)
if not str(prog_name).startswith(("..", "/", ".")):
prog_name = f"./{prog_name}"
else:
prog_name = str(script)

return TyperParser(self, prog_name, subcommand)

def print_help(self, prog_name: str, subcommand: str, *cmd_path: str):
"""
Expand All @@ -1859,7 +1869,7 @@ def print_help(self, prog_name: str, subcommand: str, *cmd_path: str):
typer/click have different helps for each subgroup or subcommand.
"""
with self:
TyperParser(self, prog_name, subcommand).print_help(*cmd_path)
self.create_parser(prog_name, subcommand).print_help(*cmd_path)

def __call__(self, *args, **kwargs):
"""
Expand Down
4 changes: 2 additions & 2 deletions django_typer/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,9 @@ def test_helps(self, top_level_only=False):
buffer = StringIO()
cmd = get_command(self.cmd_name, stdout=buffer, no_color=True)
help_output_top = run_command(self.cmd_name, "--no-color", "--help")[0]
cmd.print_help("manage.py", self.cmd_name)
cmd.print_help("./manage.py", self.cmd_name)
self.assertEqual(help_output_top.strip(), buffer.getvalue().strip())
self.assertIn(f"Usage: manage.py {self.cmd_name} [OPTIONS]", help_output_top)
self.assertIn(f"Usage: ./manage.py {self.cmd_name} [OPTIONS]", help_output_top)

if not top_level_only:
buffer.truncate(0)
Expand Down

0 comments on commit f809620

Please sign in to comment.