-
-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes to completions #355
Fixes to completions #355
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
from cleo._compat import shell_quote | ||
from cleo.commands.command import Command | ||
from cleo.commands.completions.templates import TEMPLATES | ||
from cleo.exceptions import CleoRuntimeError | ||
|
||
|
||
if TYPE_CHECKING: | ||
|
@@ -137,10 +138,32 @@ def render(self, shell: str) -> str: | |
|
||
raise RuntimeError(f"Unrecognized shell: {shell}") | ||
|
||
@staticmethod | ||
def _get_prog_name_from_stack() -> str: | ||
package_name = "" | ||
frame = inspect.currentframe() | ||
f_back = frame.f_back if frame is not None else None | ||
f_globals = f_back.f_globals if f_back is not None else None | ||
# break reference cycle | ||
# https://docs.python.org/3/library/inspect.html#the-interpreter-stack | ||
del frame | ||
|
||
if f_globals is not None: | ||
package_name = f_globals.get("__name__") | ||
|
||
if package_name == "__main__": | ||
package_name = f_globals.get("__package__") | ||
|
||
if package_name: | ||
package_name = package_name.partition(".")[0] | ||
|
||
if not package_name: | ||
raise CleoRuntimeError("Can not determine package name") | ||
|
||
return package_name | ||
|
||
def _get_script_name_and_path(self) -> tuple[str, str]: | ||
# FIXME: when generating completions via `python -m script completions`, | ||
# we incorrectly infer `script_name` as `__main__.py` | ||
script_name = self._io.input.script_name or inspect.stack()[-1][1] | ||
script_name = self._io.input.script_name or self._get_prog_name_from_stack() | ||
script_path = posixpath.realpath(script_name) | ||
script_name = os.path.basename(script_path) | ||
|
||
|
@@ -215,6 +238,7 @@ def sanitize(s: str) -> str: | |
self._zsh_describe(f"--{opt.name}", sanitize(opt.description)) | ||
for opt in sorted(cmd.definition.options, key=lambda o: o.name) | ||
) | ||
|
||
cmds_opts += [ | ||
f" ({command_name})", | ||
f" opts+=({options})", | ||
|
@@ -258,11 +282,12 @@ def sanitize(s: str) -> str: | |
f"complete -c {script_name} -f -n '__fish{function}_no_subcommand' " | ||
f"-a {command_name} -d '{sanitize(cmd.description)}'" | ||
) | ||
|
||
cmds_opts += [ | ||
f"# {command_name}", | ||
f"# {cmd.name}", | ||
*[ | ||
f"complete -c {script_name} -A " | ||
f"-n '__fish_seen_subcommand_from {sanitize(command_name)}' " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just to make sure I understand, the syntax error was already fixed here with 6cc808d right? we just haven't had a release since then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, the fix was making |
||
f"""-n '__fish_seen_subcommand_from "{cmd.name}"' """ | ||
f"-l {opt.name} -d '{sanitize(opt.description)}'" | ||
for opt in sorted(cmd.definition.options, key=lambda o: o.name) | ||
], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did this work for you? ik it works for click, but would need tweaking
what about unifying the logic from the
help
command, which prefersself._application.name
if defined? that feels more consistent and what I would expect:cleo/src/cleo/commands/base_command.py
Lines 61 to 64 in 7448f55
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have only tested the
_get_prog_name_from_stack
method and it has returned the proper module name.Aplication.name
is not always good, because you might have your program aliased on shell level (like havingpipx
installation with suffix).