D2014-complete-mandb-for-command.bash (1948B)
1 # -*- mode: sh-bash -*- 2 3 # This is a test case provided by "mozirilla213" at the following URL: 4 # https://github.com/akinomyoga/ble.sh/discussions/287 5 # 6 # Step to reproduce: 7 # 8 # $ myscript.sh -<TAB> # Menu completions from the man page appear 9 # $ runme -<TAB> # Menu completions don't appear of course 10 # 11 12 function memo/D2014-initialize { 13 local base=$_ble_base_repository/memo/D2014 14 15 export MANPATH=$base/man 16 PATH+=:$base/bin 17 18 if [[ ! -s $base/man/man8/myscript.sh.8 ]]; then 19 mkdir -p "$base"/man/man8 20 21 # The manpage was named "myscript.sh" 22 cat <<\EOF > "$base"/man/man8/myscript.sh.8 23 .TH MYSCRIPT.SH 8 "myscript.sh Manual" 24 .SH SYNOPSIS 25 .SY runme 26 .SH DESCRIPTION 27 .PP 28 Change directory to /etc 29 .SH OPTIONS 30 .TP 31 .B \-h ", " \-\-help 32 Option -h 33 .TP 34 .B \-a ", " \-\-all 35 Option -a 36 EOF 37 fi 38 39 if [[ ! -s $base/bin/myscript.sh ]]; then 40 mkdir -p "$base"/bin 41 42 # The executable script was named "myscript.sh" 43 cat <<\EOF > "$base"/bin/myscript.sh 44 #!/usr/bin/env bash 45 echo "/etc" 46 EOF 47 chmod +x "$base"/bin/myscript.sh 48 fi 49 } 50 memo/D2014-initialize 51 52 # It connects to a function that runs in current environment because 53 # it `cd`s, changes env variables, etc. 54 runme() { 55 cd "$(myscript.sh "$@")" 56 export SCRIPT_PATH=$PWD 57 } 58 59 #------------------------------------------------------------------------------ 60 # An example to define a completion function that generates option names based 61 # on the man page of a different command. 62 63 complete -F _comp_runme runme 64 _comp_runme() { 65 # If bash-completion is loaded, we handle basic stuff with _init_completion. 66 if declare -f _init_completion &>/dev/null; then 67 local cur prev words cword split 68 _init_completion -s || return 0 69 "$split" && return 0 70 fi 71 72 # If ble.sh is loaded and active, we call the man-page based completions. 73 if [[ ${BLE_ATTACHED-} ]]; then 74 ble/complete/source:option/generate-for-command myscript.sh "${comp_words[@]:1:comp_cword-1}" 75 fi 76 }