-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Completion for program with nested subcommands and multiple kinds of arguments #8773
Comments
What you want to do is something like the git completions do - see fish-shell/share/completions/git.fish Lines 561 to 578 in 959e17f
This uses a function that tells argparse to parse the options from the current commandline, which leaves the command intact. For git it's easier because it only has a few global options, so you can then use fish-shell/share/completions/git.fish Lines 646 to 658 in 959e17f
Really, the only options you need to define for this pass are the ones that
And that is, already, roughly what you are doing. That set -l line (commandline -opc)[2..-1] # remove the "spack" because we know we're spack
# the globally valid options are checked here
# you might also want to leave out the `-s` to remove them from the entire commandline,
# if they are valid everywhere
argparse -i -s $optspecs -- $line
# options that are like commands - --help, --version and such
set -q _flag_help; and return 1
set -q argv[1]
or return 1 # no command, so a faulty return
echo $argv[1]
switch $argv[1]
case view
argparse -i -s dependencies= -- $argv
set -q argv[1]
or return 0 # we have *a command*
echo -- $argv[1]
# if this can nest further, we would add another switch here
# if that becomes too much, you can extract this into another function
# __fish_spack_extract_view_command
case some-other-subcommand
# ...
end (yes, this is overly complicated, which is why #7107 exists) |
Question was answered, closing |
I'm writing a completion generator for
spack
.Background
Spack is a package management tool written in Python. It uses
argparse
to handle arguments. It's command can have nested subcommands, making it very complicated, for example:Each level of command can have its own options, and each option may have its own arguments.
It can also contain more than one positional arguments.
They have already provided script for generating
bash
completion, but that just can't handle any option with argument. Such an argument corrupts all following completion.However I prefer fish than bash, so I'm trying to take that as an example, and write one for
fish
.Attempt
To make complicated nested subcommands easier to parse, I have defined some functions.
Then I generated completion for all commands from spack's python code, the generated code has 1000+
complete
s, some look like:Problem
These
complete
s will runs__fish_spack_using_command
over and over again, make completion time very slow.To solve this, I have considered using a single function to handle all completions, but then I can't take advantage of fish's
complete -s/-l
feature for argument completion.During debugging I also find most time are spent on
__fish_spack_extract_command
, so I have tried to cache its result in a global variable, and return the same value as long as command line does not change. It works well, but I hope there is a better way.For example, if there is a function that run only once every time user press
tab
, the problem can be easier to solve.Related
#7107
The text was updated successfully, but these errors were encountered: