Skip to content

Commit 22c2da0

Browse files
committed
fixes #40
1 parent 56b08e7 commit 22c2da0

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

django_typer/__init__.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
Verbosity,
106106
Version,
107107
)
108-
from .utils import _command_context, traceback_config, with_typehint
108+
from .utils import _command_context, get_usage_script, traceback_config, with_typehint
109109

110110
if sys.version_info < (3, 10):
111111
from typing_extensions import ParamSpec
@@ -1846,7 +1846,7 @@ def create_parser( # pyright: ignore[reportIncompatibleMethodOverride]
18461846
:param subcommand: the name of the django command
18471847
"""
18481848
with self:
1849-
return TyperParser(self, prog_name, subcommand)
1849+
return TyperParser(self, get_usage_script(prog_name), subcommand)
18501850

18511851
def print_help(self, prog_name: str, subcommand: str, *cmd_path: str):
18521852
"""
@@ -1859,8 +1859,7 @@ def print_help(self, prog_name: str, subcommand: str, *cmd_path: str):
18591859
typer/click have different helps for each subgroup or subcommand.
18601860
"""
18611861
with self:
1862-
parser = self.create_parser(prog_name, subcommand)
1863-
parser.print_help(*cmd_path)
1862+
TyperParser(self, prog_name, subcommand).print_help(*cmd_path)
18641863

18651864
def __call__(self, *args, **kwargs):
18661865
"""

django_typer/management/commands/shellcompletion.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import inspect
2727
import io
2828
import os
29-
import shutil
3029
import sys
3130
import typing as t
3231
from pathlib import Path
@@ -54,6 +53,7 @@
5453
from typer.completion import completion_init
5554

5655
from django_typer import TyperCommand, command, get_command
56+
from django_typer.utils import get_usage_script
5757

5858
DETECTED_SHELL = None
5959

@@ -124,11 +124,10 @@ def manage_script(self) -> t.Union[str, Path]:
124124
# with a manage.py script being invoked directly as a script. Completion should work in
125125
# this case as well, but it does complicate the installation for some shell's so we must
126126
# first figure out which mode we are in.
127-
128-
cmd_pth = Path(sys.argv[0])
129-
if shutil.which(cmd_pth.name):
130-
return cmd_pth.name
131-
return cmd_pth.absolute()
127+
script = get_usage_script()
128+
if isinstance(script, Path):
129+
return script.absolute()
130+
return script
132131

133132
@cached_property
134133
def manage_script_name(self) -> str:

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)

django_typer/utils.py

+18
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,32 @@
22
A collection of useful utilities.
33
"""
44

5+
import shutil
6+
import sys
7+
import os
58
import typing as t
9+
from pathlib import Path
610
from threading import local
711

812
from django.conf import settings
913

1014
# DO NOT IMPORT ANYTHING FROM TYPER HERE - SEE patch.py
1115

1216

17+
def get_usage_script(script: t.Optional[str] = None) -> t.Union[Path, str]:
18+
"""
19+
Return the script name if it is on the path or the absolute path to the script
20+
if it is not.
21+
22+
:param script: The script name to check. If None the current script is used.
23+
:return: The script name or the relative path to the script from cwd.
24+
"""
25+
cmd_pth = Path(script or sys.argv[0])
26+
if shutil.which(cmd_pth.name):
27+
return cmd_pth.name
28+
return cmd_pth.absolute().relative_to(Path(os.getcwd()))
29+
30+
1331
def traceback_config() -> t.Union[bool, t.Dict[str, t.Any]]:
1432
"""
1533
Fetch the rich traceback installation parameters from our settings. By default

doc/source/changelog.rst

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Change Log
55
v1.0.4
66
======
77

8+
* Fixed `Help sometimes shows full script path in Usage: when it shouldnt. <https://github.com/bckohan/django-typer/issues/40>`_
89
* Fixed `METAVAR when ModelObjectParser supplied should default to model name <https://github.com/bckohan/django-typer/issues/39>`_
910

1011
v1.0.3

0 commit comments

Comments
 (0)