2

My zsh script takes following arguments:

./script --connect server --foo 3 --bar 5 name 

--foo and --bar take integers, name is the only non-option argument, and --connect should complete from predefinded list of servers. I am stuck at the --connect part, where I tried to define server list.

_arguments -S : \ '--foo' \ '--bar' \ '--connect' \ '*:server:->servers' _servers() { local -a server_list server_list=( 'server1' 'server2' ) _describe -t server-names "server name" server_list } 

it will offer the options, when I type -<TAB>, but it does not offer me the servers after --connect. Also, I don't know how to incorporate the name argument, which is non-option argument.

And I also need to specify that --foo and --bar need an integer

1 Answer 1

1
+300

I wrote a completion script that seems to have the correct behavior. I'm not sure everything is right, and this script can probably be improved.

For example, if all options are required and name must be suggested only at the end of all options, then I don't know how to do.

The options are suggested even after name, if they wasn't written before it. If you don't want the user to see options suggestions after name, then replace line 12 of the script by:

'(- :)'':string:->names' 

Here is the completion script:

#compdef script local state local -a options_list options_list=( \ '(--foo)'--foo'[foo desc]:number:->foos' \ '(--bar)'--bar'[bar desc]:number:->bars' \ '(--connect)'--connect'[connect to server]:string:->servers') _arguments -S : $options_list \ ':string:->names' \ && ret=0 case $state in foos) local -a foo_number_list foo_number_list=( \ '80' \ '8080') _values 'Foo (integer)' $foo_number_list \ && ret=0 ;; bars) # '-d 123' will indicate 123 as default number: _numbers -d 123 'bar number (integer)' \ && ret=0 ;; servers) local -a server_lists server_lists=( \ 'server1' \ 'server2') _values 'Servers' $server_lists \ && ret=0 ;; names) _arguments -S : $options_list local -a name_list name_list=( \ 'name1' \ 'name2') _values 'Names' $name_list \ && ret=0 ;; esac return $ret 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.