clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
14.02k stars 1.03k forks source link

Stablize Rust-Native Completion Engine Tracking Issue #3166

Open epage opened 2 years ago

epage commented 2 years ago

Maintainer's notes:

Remaining work for feature parity

Non-blocking work

Design considerations

Prior art


3022 was a tipping point for me in realizing that maybe our current approach to completions doesn't work. We effectively have to implement a mostly-untested parser within each shell. Examples of other problems that seem to stem from this:

If we take the approach of argcomplete where we do the parsing in our core code, rather than in each completion script, this will help us share parsing logic between shell, share some or all parsing logic with clap itself, and make a subset of the logic more testable.

We also need to decide whether to morph the existing parser into supporting this or create a custom parser (since the needs are pretty special). If we do a custom parser, we should probably do https://github.com/clap-rs/clap/issues/2915 first so we can reuse lexing logic between clap and the completion generation. I've also been considering https://github.com/clap-rs/clap/issues/2912 which would allow reusing the completion logic with any CLI parser.

epage commented 2 years ago

Bash's expectations

Inputs for -F and -C:

Inputs for -F:

Output for -C:

Output for -F:

See

epage commented 2 years ago

argcomplete emulates bash's interface in fish by

    set -x COMP_LINE (commandline -p)
    set -x COMP_POINT (string length (commandline -cp))
    set -x COMP_TYPE

and then just registers that function

https://github.com/kislyuk/argcomplete/blob/develop/argcomplete/shell_integration.py#L60

epage commented 2 years ago

Value hints are supported on

Tooltips are supported on

We should make sure we don't lose these features as part of this transition, ie we shouldn't drop to the lowest common denominator.

epage commented 2 years ago

For Powershell

Register-ArgumentCompleter
        -CommandName <String[]>
        -ScriptBlock <ScriptBlock>
        [-Native]
        [<CommonParameters>]

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/register-argumentcompleter?view=powershell-7.2

The block receives

When you specify the Native parameter, the script block must take the following parameters in the specified order. The names of the parameters aren't important because PowerShell passes in the values by position.

  • $wordToComplete (Position 0) - This parameter is set to value the user has provided before they pressed Tab. Your script block should use this value to determine tab completion values.
  • $commandAst (Position 1) - This parameter is set to the Abstract Syntax Tree (AST) for the current input line. For more information, see Ast Class.
  • $cursorPosition (Position 2) - This parameter is set to the position of the cursor when the user pressed Tab.

The block provides CompletionResult

The CompletionResult object allows you to provide additional details to each returned value:

  • completionText (String) - The text to be used as the auto completion result. This is the value sent to the command.
  • listItemText (String) - The text to be displayed in a list, such as when the user presses Ctrl+Space. This is used for display only and is not passed to the command when selected.
  • resultType (CompletionResultType) - The type of completion result.
  • toolTip (String) - The text for the tooltip with details to be displayed about the object. This is visible when the user selects an item after pressing Ctrl+Space.

So it seems like Powershell can fit within rust-driven completions and provide the full feature set.

epage commented 2 years ago

I'm starting small and prototyping for just bash support. Looking at argcomplete, it seems they had to use their own shlex implementation. Hoping we can avoid that. The first step is https://github.com/comex/rust-shlex/issues/12.

happenslol commented 2 years ago

Hi, I'm interested in helping with this this I'm developing a few personal tools using clap that would hugely benefit from dynamic completions. Is there any good issues that nobody is working on you could point me towards?

epage commented 2 years ago

@happenslol Thanks!

Anything unchecked in the "Remaining work" section is up for grabs; just post here that you are getting started on it. The highest priority is the support for each shell as that will help gauge the feasibility and provide feedback on the API. The rest is flushing out parsing implementation.

I should split those out into individual issues to make it easier for people to coordinate on those (and to add bounties) but I probably won't have time for another week or so.

happenslol commented 2 years ago

Alright, had a busy week, so I'm only getting back around to this now.

I've looked at the work done in #3656, and it looks like the next steps to add zsh and fish support would be to pull some of the functionality out of clap_complete::dynamic::bash, and adapt it to the other shells to provide the same functionality. Am I correct in assuming that?

I'd look at argcomplete for the other implementations here, since the current one also seems to be coming from there.

epage commented 2 years ago

@happenslol Yes, that is correct. I'd like to focus on shell support initially as that is where the most unknowns exist

happenslol commented 2 years ago

Started work here. I'm looking at cobra for zsh completions since argcomplete basically takes the easy way out and tells people to enable bash completion support for zsh :sweat_smile:

epage commented 2 years ago

Oh if cobra is doing the same type of dynamic completions as argcomplete, that is great! That'll serve as a much better example!

happenslol commented 2 years ago

Yeah, the cobra code is very well documented and easy to adapt.

One thing I was wondering about is the way options like space/no-space are passed to the completion command. cobra deals with this in a pretty involved way by encoding all options (compile-time and run-time) into a bitfield, and then parsing that bitfield in their completion script. It wouldn't be too hard to adapt this, but I feel like there isn't a clear definition of what options we even want to offer and the completion output would have to be changed, so that's probably better for another PR.

For a start, I would try to keep the features that are implemented for bash.

Should I open a new issue for zsh communication for questions like these, or is there some chat/forum where we could discuss them?

epage commented 2 years ago

Yeah, the cobra code is very well documented and easy to adapt.

One thing I was wondering about is the way options like space/no-space are passed to the completion command. cobra deals with this in a pretty involved way by encoding all options (compile-time and run-time) into a bitfield, and then parsing that bitfield in their completion script. It wouldn't be too hard to adapt this, but I feel like there isn't a clear definition of what options we even want to offer and the completion output would have to be changed, so that's probably better for another PR.

For a start, I would try to keep the features that are implemented for bash.

Should I open a new issue for zsh communication for questions like these, or is there some chat/forum where we could discuss them?

I created https://github.com/clap-rs/clap/issues/3916. We can also talk on rust-lang zulip on the WG-CLI stream

blaggacao commented 1 year ago

I want to apport a data point:

Implements cobra completion with advanced things like a fs cache for dynamic completion querys (e.g. with provenience from remote APIs) for an x amount of seconds.

Imagine a aws CLI wrapper that interacts with your custom infrastructure and things like it.