-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbash_completion
42 lines (41 loc) · 1.38 KB
/
bash_completion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#/usr/bin/env bash
if [ ${#} != "1" ] || ! [ -f $1 ]; then
if ! [ -f "${1}" ]; then
>&2 echo "'${1}' is not a file"
fi
>&2 echo "usage:" ${1}
>&2 echo " \$ source ${0} [executable]"
else
function sargparse_GetOpts ()
{
WORD_TO_COMPLETE=${COMP_WORDS[COMP_CWORD]}
LINE="${COMP_LINE[@]:0:COMP_POINT}"
if [ "${COMP_LINE[@]:COMP_POINT-1:1}" = ' ' ]; then
LINE="${LINE} ''"
WORD_TO_COMPLETE=""
fi
mapfile -t HINTS <<< $(${LINE} --bash_completion)
if [ "${HINTS}" == " -d " ]; then
compopt -o filenames
local IFS=$'\n'
COMPREPLY=( $(compgen -d -- ${2}) )
elif [ "${HINTS}" == " -f " ]; then
compopt -o filenames
local IFS=$'\n'
COMPREPLY=( $(compgen -f -- ${2}) )
elif [[ "${HINTS}" =~ " -f " ]]; then
compopt -o filenames
EXT="$(echo "${HINTS}" | cut -d ' ' -f 3)"
local IFS=$'\n'
COMPREPLY=( $(compgen -d -- ${2}) )
COMPREPLY+=( $(compgen -f -X '!*'${EXT} -- ${2}) )
elif [ ${#HINTS} -eq 0 ]; then
COMPREPLY=()
else
HINTS=$(printf "%q:" "${HINTS[@]}")
local IFS=":"
mapfile -t COMPREPLY <<< $(compgen -W '${HINTS}' -- "${WORD_TO_COMPLETE}")
fi
}
complete -F sargparse_GetOpts $1
fi