Skip to content
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

Zsh completion for sub-commands adds an extra slash and fails to complete sub-command options #153

Open
juhoautio opened this issue Jun 1, 2022 · 5 comments · Fixed by #247
Milestone

Comments

@juhoautio
Copy link
Contributor

With the beta version 1.2.0b1 of Poetry I can see that sub-commands and their options show up in the generated zsh completion script.

However, there is some weird behavior when trying to tab-complete on zsh command line.

First of all, it completes the sub-command with an additional slash: poetry env\ info – which is not a valid command..

poetry env + tab adds the slash and shows the expected subcommands:

❯ poetry env\ 
env info    -- Displays information about the current environment.
env list    -- Lists all virtualenvs associated with the current project.
env remove  -- Remove virtual environments associated with the project.
env use     -- Activates or creates a new virtualenv for the current project.

Also, with this my zsh is unable to autocomplete the option(s) for the subcommand. I was trying poetry env info --p + tab, hoping to get --path completed but zsh just offers the global options of poetry:

❯ poetry env info --p
--help            -- Display help for the given command. When no command is given display help for the list command.
--no-interaction  -- Do not ask any interactive question.
--quiet           -- Do not output any message.
--version         -- Display this application version.

References:

@mickyhale
Copy link

First of all, it completes the sub-command with an additional slash: poetry env\ info – which is not a valid command..

As far as I can tell, poetry env\ info is syntactically valid, as well as poetry 'env info' and poetry "env info" when invoking the sub-command (or any for that matter), with tab completions working for all three.

The issue of the options not getting added to the sub-command has to do with the case patterns in the com clauses in the completions script. To correct this in my project that uses cleo, I temporarily monkey patched the first element of desc in the render_zsh method for the completions command:

            desc = [
                f"            (([\\\"\\']|){command.replace(' ', '*')}([\\\"\\']|))",
                "            opts+=({})".format(" ".join(options)),
                "            ;;",
            ]

The above syntax is inelegant, but the generated glob patterns that get added to the completions script account for the 3 different ways of invoking sub-commands (that I know of), and the same goes for commands. The following invocation patterns would show associated options:

foo
'foo'
"foo"
bar\ baz
'bar baz'
"bar baz"

What I'm using:

$ which $SHELL
/bin/zsh
$ zsh --version
zsh 5.8.1 (x86_64-apple-darwin21.0)
$ python --version
Python 3.10.2
$ poetry show cleo
 name         : cleo                                                                      
 version      : 1.0.0a5                                                                   
 description  : Cleo allows you to create beautiful and testable command-line interfaces. 

dependencies
 - crashtest >=0.3.1,<0.4.0
 - pylev >=1.3.0,<2.0.0

@Secrus
Copy link
Member

Secrus commented Sep 6, 2022

Reopening, since not all issues mentioned here were resolved.

@mickyhale
Copy link

mickyhale commented Sep 6, 2022

@Secrus If you take a look at what I mentioned above, you'll see that I only made a change in one place to get the sub-commands to operate correctly. The sub-commands don't need to be quoted in the coms array. This essentially breaks sub-command invocation when using tab completion:

$ cleo-completions \'spaced\ command\' 
'spaced command'  -- Sub-command example for the purposes of testing completions script.
help              -- Displays help for a command.
list              -- Lists commands.
$ cleo-completions \'spaced\ command\' 

The command "'spaced command'" does not exist.

Did you mean this?
    spaced command

The issue in the zsh script before the most recent change appeared to be with the sub-command patterns in the com case statement. To get around this in my own project the glob pattern I used is ([\"\']|) which bookends the commands. This will match either a single quote, double quote or zero occurrences of the pattern. The * that replaces the single whitespace character for subcommands will match on whitespace or \ .

The following are examples of how I invoked the command:

$ cleo-completions spaced\ command --
--ansi            -- Force ANSI output.
--bar             -- Bar.
--foo             -- Foo.
--help            -- Display help for the given command. When no command is given display help for the list command.
--no-ansi         -- Disable ANSI output.
--no-interaction  -- Do not ask any interactive question.
--quiet           -- Do not output any message.
--verbose         -- Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.
--version         -- Display this application version.
$ cleo-completions spaced\ command   
spaced command was invoked.

or

$ cleo-completions 'spaced command' --
--ansi            -- Force ANSI output.
--bar             -- Bar.
--foo             -- Foo.
--help            -- Display help for the given command. When no command is given display help for the list command.
--no-ansi         -- Disable ANSI output.
--no-interaction  -- Do not ask any interactive question.
--quiet           -- Do not output any message.
--verbose         -- Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.
--version         -- Display this application version.
$ cleo-completions 'spaced command'   
spaced command was invoked.

or

$ cleo-completions "spaced command" --
--ansi            -- Force ANSI output.
--bar             -- Bar.
--foo             -- Foo.
--help            -- Display help for the given command. When no command is given display help for the list command.
--no-ansi         -- Disable ANSI output.
--no-interaction  -- Do not ask any interactive question.
--quiet           -- Do not output any message.
--verbose         -- Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.
--version         -- Display this application version.
$ cleo-completions "spaced command"
spaced command was invoked.

Hope this helps.

@Secrus
Copy link
Member

Secrus commented Sep 6, 2022

@mickyhale thanks. The patch I made recently was mostly addressing issues with completions script reporting errors when invoked. I have a refactor on the way that will introduce new syntax to the completions script, especially for commands with subcommands. I have it on hold since right now we want to focus on delivering a new stable version since it breaks some Poetry use cases (python-poetry/poetry#6353).

@tony
Copy link

tony commented Dec 2, 2023

Report

Completions for commands with spaces show in poetry <tab> and are wrapped in single quotes.

~ ❯ poetry --version
Poetry (version 1.7.1)
~ ❯ zsh --version
zsh 5.9 (x86_64-ubuntu-linux-gnu)
~ ❯ poetry <tab>
'cache clear'        -- Clears a Poetry cache by name.
'cache list'         -- List Poetry's caches.
'debug info'         -- Shows debug information.
'debug resolve'      -- Debugs dependency resolution.
'env info'           -- Displays information about the current environment.
'env list'           -- Lists all virtualenvs associated with the current project.
'env remove'         -- Remove virtual environments associated with the project.
'env use'            -- Activates or creates a new virtualenv for the current project.
'self add'           -- Add additional packages to Poetry's runtime environment.
'self install'       -- Install locked packages (incl. addons) required by this Poetry installation.
'self lock'          -- Lock the Poetry installation's system requirements.
'self remove'        -- Remove additional packages from Poetry's runtime environment.
'self show plugins'  -- Shows information about the currently installed plugins.
'self show'          -- Show packages from Poetry's runtime environment.
'self update'        -- Updates Poetry to the latest version.
'source add'         -- Add source configuration for project.
'source remove'      -- Remove source configured for the project.
'source show'        -- Show information about sources configured for the project.
about                -- Shows information about Poetry.
add                  -- Adds a new dependency to pyproject.toml.
build                -- Builds a package, as a tarball and a wheel by default.
check                -- Validates the content of the pyproject.toml file and its consistency with the poetry.lock file.
config               -- Manages configuration settings.
export               -- Exports the lock file to alternative formats.
help                 -- Displays help for a command.
init                 -- Creates a basic pyproject.toml file in the current directory.
install              -- Installs the project dependencies.
list                 -- Lists commands.
lock                 -- Locks the project dependencies.
new                  -- Creates a new Python project at <path>.
publish              -- Publishes a package to a remote repository.
remove               -- Removes a package from the project dependencies.
run                  -- Runs a command in the appropriate environment.
search               -- Searches for packages on remote repositories.
shell                -- Spawns a shell within the virtual environment.
show                 -- Shows information about packages.
update               -- Update the dependencies as according to the pyproject.toml file.
version              -- Shows the version of the project or bumps it when a valid bump rule is provided.
Screenshot

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants