1

I have a script foo, which takes few optional commandline arguments in the form of:

foo -A <NUM> -B <NUM> -i

or it can also be without space:

foo -A<NUM> -B<NUM> -i

where NUM is integer number. The script should not complete filenames / directories.

I have following completion rules:

#compdef foo

 local -a args

_arguments -S \
    '*-A' \
    '*-B' \
    '*-i'

What do I have to add, so that the completion is aware that the options -A or -B are not complete by themselves, but need a number before the completion can continue?

In another script, I have similar situation, the only differece is the option uses =:

foo --port=<PORT>

How could I do this also?

UPDATE:

I am experiencing weird issue, with following completion:

#compdef foo

_arguments -S \
    '-A+:' \
    '-B+:' \
    '-i'

For some reason, the first argument A is ignored, ie:

foo -<TAB>
-B -i

But when I put some other option in front of A

#compdef foo

_arguments -S \
    '-x+:' \
    '-A+:' \
    '-B+:' \
    '-i'

then not only is A shown, but also this other new option:

foo -<TAB>
-A  -B  -i  -x

what is happening here ?

1 Answer 1

2

Tell _arguments that the option requires an argument. The + after the option indicates that the option can either be in the same word or in a separate word. The part after the first : is a message that is displayed when matches are generated, which is pointless here since there won't be any completion. The part after the second column is the completion function, of which there is presumably none.

_arguments -S : \
    '*-A+:number:' \
    '*-B+:number:' \
    '*-i'

For a long option, put = rather than : after the option name if both --port 123 and --port=123 are accepted, or =- if only --port=123 is accepted. For a network (TCP/UDP) port, if your command accepts port names, use _port as the completion function.

3
  • thank you, but I ran into a very weird problem. Could you please look at the UPDATE in my original question? Aug 19, 2021 at 11:57
  • @400theCat Sorry, I'd forgotten the : to separate options that tune the behavior of _arguments from specifications of options to complete. This is necessary when the first specification looks like an option to _arguments, which -A does but not -x or *-A. Annoyingly the way to convey this is :, the standard method -- has a completely different meaning for _arguments. Aug 19, 2021 at 12:09
  • @illes 'SO- stop being evil' - thank you, now it works. Aug 19, 2021 at 12:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .