proga [ -h | --help ] [ -e ESSID | --essid ESSID ] [ - FILE | --config FILE ] [ -o PROFILE | --open PROFILE ] [ -t NUM | --tab NUM ] [ --set-opts OPTIONS ]
-h
and --help
do not require arguments;-e
and --essid
require an argument as a string, without autocompletion;-c
and --config
require an argument as a string, a file with an arbitrary location;-o
and --open
require an argument as a string, autocompletion on files from a specific directory;-t
and --tab
require an argument as a string, autocompletion from the specified array;--set-opts
requires an argument as a string, auto-completion from the specified array, separated by commas; #compdef proga
# variables _proga_arglist=() _proga_settings=() _proga_tabs=() _proga_profiles() {}
# work block _proga() {}
case "$service" in proga) _proga "$@" && return 0 ;; esac
_arguments
command, which requires a specific variable format. It looks like this: []::
{(_2)_1,(_1)_2}[]::
{,}[]::
- the message to be shown,
- the action to be performed after this flag. In the case of this tutorial,
will look like ->
. _proga_arglist=( {'(--help)-h','(-h)--help'}'[show help and exit]' {'(--essid)-e','(-e)--essid'}'[select ESSID]:type ESSID:->essid' {'(--config)-c','(-c)--config'}'[read configuration from this file]:select file:->files' {'(--open)-o','(-o)--open'}'[open profile]:select profile:->profiles' {'(--tab)-t','(-t)--tab'}'[open a tab with specified number]:select tab:->tab' {'--set-opts','--set-opts'}'[set options for this run, comma separated]:comma separated:->settings' )
_proga_settings=( 'CTRL_DIR' 'CTRL_GROUP' ) _proga_tabs=( '1' '2' )
_proga_profiles() { print $(find /some/path -maxdepth 1 -type f -printf "%f\n") }
$state
variable, and a check is made in the function body for what it is equal to in order to select the appropriate actions. At the beginning also need not forget to call _arguments
with our flags. _proga() { _arguments $_proga_gui_arglist case "$state" in essid) # , ;; files) # _files ;; profiles) # # # _values 'profiles' $(_proga_profiles) ;; tab) # _values 'tab' $_proga_tabs ;; settings) # # -s _values -s ',' 'settings' $_proga_settings ;; esac }
/usr/share/zsh/site-functions/
directory with an arbitrary, in general, name with the prefix _
. The example file can be fully found in my repository (do not count for advertising, I specifically raised all the names).Source: https://habr.com/ru/post/230421/
All Articles