Skip to content

Fix shell completion for arguments with non-alphanumeric characters#1930

Merged
davidism merged 1 commit into
pallets:8.0.xfrom
samschott:shell-completion-fix
Jul 4, 2021
Merged

Fix shell completion for arguments with non-alphanumeric characters#1930
davidism merged 1 commit into
pallets:8.0.xfrom
samschott:shell-completion-fix

Conversation

@samschott
Copy link
Copy Markdown
Contributor

@samschott samschott commented May 23, 2021

This PR fixes shell completion for arguments with non-alphanumeric characters by implementing a less stringent test in shell_completion._start_of_option().

Checklist:

  • Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
  • Add or update relevant docs, in the docs folder and in code.
  • Add an entry in CHANGES.rst summarizing the change and linking to the issue.
  • Add .. versionchanged:: entries in any relevant code docs.
  • Run pre-commit hooks and fix any issues.
  • Run pytest and tox, no tests failed.

@davidism davidism added this to the 8.0.2 milestone May 27, 2021
@davidism davidism changed the base branch from main to 8.0.x May 27, 2021 14:35
@davidism
Copy link
Copy Markdown
Member

Please rebase to 8.0.x:

$ git rebase --onto origin/8.0.x origin/main
$ git push -f

@samschott samschott force-pushed the shell-completion-fix branch from 1d03700 to ac7caac Compare May 27, 2021 14:40
Comment thread CHANGES.rst Outdated
@davidism
Copy link
Copy Markdown
Member

davidism commented May 27, 2021

Please use git rebase -i origin/8.0.x to squash these commits into one (with one commit message, not the combination of all).

@samschott
Copy link
Copy Markdown
Contributor Author

All of them, or just the changelog entries?

@samschott samschott force-pushed the shell-completion-fix branch from 753134c to f0e6b60 Compare May 27, 2021 15:12
@davidism
Copy link
Copy Markdown
Member

All of them.

@samschott samschott force-pushed the shell-completion-fix branch from f0e6b60 to c707a63 Compare May 27, 2021 15:21
@samschott
Copy link
Copy Markdown
Contributor Author

Any comments regarding the actual changes? Am I overlooking some cases by assuming that all options begin with a "-"? There might have been a good reason for the alphanumeric check.

@davidism davidism force-pushed the shell-completion-fix branch from c707a63 to d8fe64a Compare July 3, 2021 13:46
@davidism
Copy link
Copy Markdown
Member

davidism commented Jul 3, 2021

Options can start with any non-alpha character, not only -. Windows tools often use /. The other most common, shown in the docs, is +. Different options can have different prefixes, it doesn't have to be consistent across options or commands.

@samschott
Copy link
Copy Markdown
Contributor Author

Ah, yes, I should have red https://click.palletsprojects.com/en/8.0.x/options/#other-prefix-characters 🤦. Apologies for that.

This makes things a bit more difficult. I can see following choices:

  1. Keep the code as is and document that shell completion is only supported for values that start with alphanumeric characters. This would be a shame, especially since paths are such a common argument type for command line tools.
  2. Collect all possible option prefixes for a specific command. If the incomplete string starts with one of those prefixes, recognise it as the start of an option. This would still prioritise the completion of option names over arguments or option values (as is the case currently), but only if there are actually any option names starting with the given character.

If you agree, I can try to implement the second.

@davidism
Copy link
Copy Markdown
Member

davidism commented Jul 3, 2021

How about just don't support completion of Windows-style options, since that already wasn't supported.

@samschott
Copy link
Copy Markdown
Contributor Author

samschott commented Jul 3, 2021

So we would hard-code a set of non-alphanumeric characters that are supported for the shell completion of options? For example {"-", "+"} or just "-" as in this PR?

One advantages of the 2. approach from my the previous comment: if a command has only arguments and no options, we can offer completion for arguments that start with -.

The most rigorous solution probably would be: if the incomplete string starts with a non-alphanum character, we offer completions for both matching options and arguments. This will however be complex to implement, especially when offering completion for both a path and an option.

It's really up to you. As you say in the docs, any prefix other than "-" or "--" for options is discouraged. And the reverse, argument values that start with "-", are quite uncommon. The most probable use case is free string input and shell completion won't be very useful there.

@davidism
Copy link
Copy Markdown
Member

davidism commented Jul 4, 2021

I was thinking not value[0].isalnum() and value != "/"

@samschott samschott force-pushed the shell-completion-fix branch from d8fe64a to 27cdf95 Compare July 4, 2021 11:42
@samschott
Copy link
Copy Markdown
Contributor Author

Ok, done.

@davidism davidism force-pushed the shell-completion-fix branch from 6625191 to 91e3880 Compare July 4, 2021 13:49
@davidism davidism merged commit e5536b2 into pallets:8.0.x Jul 4, 2021
@samschott samschott deleted the shell-completion-fix branch July 4, 2021 13:59
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shell completion fails with for arguments starting with "/"

2 participants