pacman
were taken as a basis (sincerely hope that the founding fathers of Arch did not invent many bikes). 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;_proga_profiles()
, unlike other variables, should return the current array: # variables _proga_arglist=() _proga_settings=() _proga_tabs=() _proga_profiles() {}
# work block _proga() {}
complete -F _proga proga
_proga_arglist=( '-h' '--help' '-e' '--essid' '-c' '--config' '-o' '--open' '-t' '--tab' '--set-opts' )
_proga_profiles() { print $(find /some/path -maxdepth 1 -type f -printf "%f\n") }
_proga_profiles() { echo $(find /some/path -maxdepth 1 -type f -printf "%f\n") }
COMPREPLY
is responsible. To track the current state, call the _get_comp_words_by_ref
function with the cur
(current option) and prev
(previous, the actual state) parameters. Well, you need a few points on which to collapse in a certain part of the case (variables want*
). compgen
used to generate auto-completion. After the -W
flag he is given a list of words. (There is also a -F
flag that calls a function, but in addition to me, it also produces a vorning.) The last argument is the current line, to which you need to generate auto-completion. _proga() { COMPREPLY=() wantfiles='-@(c|-config)' wantprofiles='-@(o|-open|s|-select)' wantsettings='-@(-set-opts)' wanttabs='-@(t|-tab)' _get_comp_words_by_ref cur prev if [[ $prev = $wantstring ]]; then # , COMPREPLY=() elif [[ $prev = $wantfiles ]]; then # _filedir elif [[ $prev = $wantprofiles ]]; then # COMPREPLY=($(compgen -W '${_proga_profiles[@]}' -- "$cur")) elif [[ $prev = $wanttabs ]]; then # COMPREPLY=($(compgen -W '${_proga_tabs[@]}' -- "$cur")) elif [[ $prev = $wantsettings ]]; then # # -S , =( COMPREPLY=($(compgen -S ',' -W '${_proga_settings[@]}' -- "$cur")) else # COMPREPLY=($(compgen -W '${_proga_arglist[@]}' -- "$cur")) fi true }
/usr/share/bash-completion/completions/
with an arbitrary name. The sample file can be fully found in my repository .Source: https://habr.com/ru/post/230425/
All Articles