Skip to content

feat(cli): add autocomplete module#644

Open
shreejaykurhade wants to merge 10 commits intoQuantConnect:masterfrom
shreejaykurhade:feat/autocomplete-aliasing
Open

feat(cli): add autocomplete module#644
shreejaykurhade wants to merge 10 commits intoQuantConnect:masterfrom
shreejaykurhade:feat/autocomplete-aliasing

Conversation

@shreejaykurhade
Copy link
Copy Markdown

@shreejaykurhade shreejaykurhade commented Mar 29, 2026

GitHub Pull Request: Advanced Autocomplete & Shorthand Command Execution

Summary

This Pull Request introduces significant usability improvements to the Lean CLI. It implements Prefix-based Command Matching (allowing shorthand like lean cl instead of lean cloud) and a Native Autocomplete Management Module that supports interactive, visual predictions in PowerShell and traditional completion in Bash/Zsh/Fish.

@jaredbroad
Copy link
Copy Markdown
Member

Interesting thank you @shreejaykurhade , we'll review and get back to you next week when MM's back.

@shreejaykurhade
Copy link
Copy Markdown
Author

Sure

Copy link
Copy Markdown
Member

@Martin-Molinero Martin-Molinero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @shreejaykurhade!
Thanks for the contribution 🙌
Overall I think it's promising, I'm not convinced installing files on the users machine is a good pattern for the autocomplete though, seems click has a dynamic way of doing this? Think we should double check what does click suggest as autocomplete approach and what are other popular libraries doing. Should a nice test suite too, there are no tests 😱 .
Also I think ideally it's enabled by default? Again would double check what click and popular libraries suggest, finally I would double check performance implications though, what's the overhead cost we might determine not worth enabling by default

Comment thread lean/components/util/click_aliased_command_group.py
Comment thread lean/main.py Outdated
@shreejaykurhade
Copy link
Copy Markdown
Author

shreejaykurhade commented Apr 15, 2026

@Martin-Molinero create and delete the cmd profile using "lean completion on" and "lean completion off"
This PR improves Lean CLI usability in two ways :

  1. adds prefix-based command matching, so unique prefixes like lean cl resolve to lean cloud
  2. adds Click-native shell completion, including PowerShell support, without modifying user shell profiles

The completion flow now follows Click’s native _LEAN_COMPLETE mechanism instead of installing scripts directly into profile files. Users can opt in by running lean completion --shell and evaluating the generated script in their shell.

Changes

  1. add unique prefix resolution for command groups
  2. add lean completion command for powershell, bash, zsh, and fish
  3. add a PowerShell shell-completion adapter using Click’s native shell completion API
  4. keep ambiguous prefixes failing with a clear error message
  5. fix click.Exit handling in lean/main.py using a local import pattern

Examples

  1. lean cl -> lean cloud
  2. lean cloud st -> lean cloud status
  3. PowerShell: lean completion --shell powershell | Out-String | Invoke-Expression
  4. Bash/Zsh: eval "$(lean completion --shell bash)"
  5. Fish: lean completion --shell fish | source

Tests

Added test coverage for:

  1. unique prefix matching
  2. ambiguous prefix errors
  3. top-level and nested shorthand execution
  4. completion script generation
  5. Click native completion flow for PowerShell and Bash

Verified with:
pytest tests/test_completion.py tests/test_click_aliased_command_group.py tests/test_main.py

Copy link
Copy Markdown
Author

@shreejaykurhade shreejaykurhade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check

Copy link
Copy Markdown
Author

@shreejaykurhade shreejaykurhade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have submitted Click-native implementation of Autocomplete. It's a basic implementation. will work on this further.

@shreejaykurhade
Copy link
Copy Markdown
Author

@Martin-Molinero please review

@shreejaykurhade
Copy link
Copy Markdown
Author

shreejaykurhade commented Apr 28, 2026

Thanks @Martin-Molinero, addressed these points.

I checked Click’s recommended autocomplete flow. Click uses the _LEAN_COMPLETE-style environment variable mechanism to generate a shell script and return completions dynamically, so the implementation now follows that path instead of relying on a custom static command tree. Bash/Zsh/Fish use Click’s built-in completion support, and PowerShell uses a small custom ShellComplete adapter because Click does not include PowerShell support by default.

On installing files: agreed, silently modifying user profile files is not a good default. Autocomplete now works through:

lean autocomplete --shell <shell>

so users can evaluate/source the generated script themselves. For the common case, users can simply run:

lean autocomplete on

and later:

lean autocomplete off

The shell is auto-detected, so --shell powershell is only an optional override. lean autocomplete on/off are explicit opt-in helpers for users who want Lean to manage the profile block. For PowerShell, once the generated script is loaded, the same lean autocomplete on/off commands also update the current session through a small wrapper, so users do not need to run a separate long Invoke-Expression cleanup command.

Legacy lean completion ... remains available as a hidden compatibility alias, but lean --help only shows autocomplete, and hidden compatibility commands are excluded from prefix matching.

On other CLIs: the common pattern is similar: CLIs expose an autocomplete/completion command that prints the script, and users either source it or save it into their shell profile/completion location. This is also what Click documents.

On enabled by default: command prefix matching is enabled by default inside Lean, so shorthand like lean cl and lean cloud st works immediately. Shell autocomplete itself cannot be fully enabled by default without registering a shell script in the user’s shell, so I kept that explicit.

On performance: PowerShell autocomplete registration is lazy. Normal Lean commands do not import/register the PowerShell autocomplete helper unless autocomplete is being generated or _LEAN_COMPLETE=powershell_* is present. Locally, importing the autocomplete helper alone measured around 139 ms, so avoiding it in the normal command path keeps the overhead out of regular CLI startup.

I also removed the PowerShell HistoryAndPlugin prediction setup from the generated autocomplete script, so Lean autocomplete only returns Lean command completions and does not surface previous mistyped commands, file names, or unrelated shell history.

Tests added/updated cover:

  • unique prefix command execution
  • ambiguous prefix errors
  • top-level and nested shorthand execution
  • hidden compatibility commands being excluded from prefix matching
  • Click-native Bash autocomplete
  • Click-native PowerShell source/autocomplete
  • PowerShell command-only autocomplete behavior
  • lean autocomplete on/off with shell auto-detection
  • profile on/off behavior using a temporary home directory, so tests do not touch the real user profile
  • legacy profile block removal
  • profile permission-denied messaging
  • current-session PowerShell autocomplete cleanup

Verified with:
pytest tests/test_autocomplete.py tests/test_click_aliased_command_group.py tests/test_main.py -q
python static_analysis.py
pytest -q

@shreejaykurhade
Copy link
Copy Markdown
Author

shreejaykurhade commented Apr 28, 2026

@Martin-Molinero @jaredbroad please review.
Fixed decativation activation error which was lingering.

@shreejaykurhade shreejaykurhade changed the title feat(cli): add prefix-based command matching and autocomplete module feat(cli): add autocomplete module Apr 28, 2026
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 this pull request may close these issues.

3 participants