0

I am writing a custom competition function for zsh. Currently, I have the following code

_arguments \
...
'(-G --glitchpct)'{-G+,--glitchpct=}'[Controls how often the characters on screen glitch]'

I am currently specifying that -G VALUE or glitchpct=VALUE or --glitchpct VALUE are acceptable(mutually exclusive as they are part of the same group).

What if I need to have 2 argument values instead of one? That is, I expect to have -G VALUE1 VALUE2 or --glitchpct=VALUE1,VALUE2. How could I specify this? Also, how would it be different if there are 3, etc. arguments?

Additionally, would it be possible to display a custom message for each argument? Say VALUE1 should display 'float from 0 to 50', and VALUE2 should display 'float from 100 to 150' on the screen. I assume it is possible to do that through the _message builtin but I am unsure how to make it work. I tried specifying the state like this(this is for only 1 value as I haven't figured out how to use two):

_arguments \
...
'(-G --glitchpct)'{-G+,--glitchpct=}'[Controls how often the characters on screen glitch]:float:->display_float'
...
case "$state" in
        (display_float)
            _message "sample message"
        ;;
    esac

but this didn't see to display anything. The following form didn't work either:

'(-G --glitchpct)'{-G+,--glitchpct=}'[Controls how often the characters on screen glitch]:float:_message "sample message"
5
  • Does this help: See Passing Arguments to a Bash/Zsh Script: Section 2? In that example age is the optional argument. Notice that there is no shift included in the last bit of the case state which is what makes it optional. The shift after esac concludes the argument passing.
    – eyoung100
    Commented Apr 11 at 20:25
  • @eyoung100 it's not a bad article but it doesn't use the zsh's competition system, which is supposed to make things easier. That's what I am looking for
    – Alex.Kh
    Commented Apr 11 at 21:07
  • My apologies. See the example at The zsh-completion Howto using flags
    – eyoung100
    Commented Apr 11 at 21:32
  • @eyoung100 that's a pretty neat guide that I have been relying on as well. Unfortunately, it has only 2 mentions of _message and 0 mentions of having more than one options for arguments like I need (:
    – Alex.Kh
    Commented Apr 11 at 21:50
  • See my answer. I believe you might be overlooking the argument array.
    – eyoung100
    Commented Apr 11 at 22:49

1 Answer 1

0

I provided the comment above in hopes it would lead you to the example containing an argument array. Here's the example copied directly from the link:

_arguments '-m[music file]:filename:->files' '-f[flags]:flag:->flags'
case "$state" in
    files)
        local -a music_files
        music_files=( Music/**/*.{mp3,wav,flac,ogg} )
        _multi_parts / music_files
        ;;
    flags)
        _values -s , 'flags' a b c d e
        ;;
esac

The above example taken from the Howto shows how to treat the argument passing as an array. Breaking apart the example:

  1. The argument -m may also be -ma. Including the a lists all music files removing the need for filename.
  2. The flags section while not as fleshed out allows -f to be initialized as -fs with possible values of -fa through -fe

Relating to your code: Consider Something Like the code above in the following format:

_arguments '-G[glitchpercentage]:gpercent:->percents' '-f[flags]:flag:->flags'
case "$state" in
    percents)
        local -a percents
        glitchpercentage=\\do something to set to 100%
        _message "Some message for all option"
        local -h percents
        glitchpercentage=//do something to set to 50%
        _message "Some message for half option"
        ;;
    flags)
         // Add these to control the program, i.e. use these options
         // if they dont control something in the percents array.
        _values -s , 'flags' a b c d e
        ;;
esac

The above hasn't been tested so please treat it as psuedo-code. Using an array as above is what I think you're looking for.

3
  • Thanks! I will give it a try and see if I can make the _message work, so stay tuned!
    – Alex.Kh
    Commented Apr 12 at 0:26
  • Just to double-check my understanding of the _message function, on TAB it is supposed to display a competition message under the current line and not the competition itself, correct?
    – Alex.Kh
    Commented Apr 12 at 0:35
  • I believe the _message function is akin to a "Windows Tooltip." I may have the display order backwards. _message may need to go first, and yes the message should tell what the option does, not after it's completed.
    – eyoung100
    Commented Apr 12 at 13:42

You must log in to answer this question.

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