Skip to content

Commit 1d8cc2c

Browse files
committed
bash-completion: Add initial draft
Signed-off-by: Steffen Hemer <s.hemer@phytec.de>
1 parent d8a306b commit 1d8cc2c

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

partup-completion.bash

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#/usr/bin/env bash
2+
3+
partup_commands="install package show version"
4+
help_options="-h --help"
5+
app_options="-q --quiet -d --debug -D --debug-domains"
6+
install_options="-s --skip-checksums"
7+
package_options="-f --force -C, --directory"
8+
show_options="-s --size"
9+
10+
11+
function options()
12+
{
13+
local current="${COMP_WORDS[COMP_CWORD]}"
14+
15+
if [[ ${current} == -* ]] ; then
16+
local options=$(${1:-${COMP_WORDS[0]}} -h 2>&1 | grep ' -' | sed -r -e 's%^\s+(-.).+(--.*)%\1 \2 %' -e 's%=% %' | cut -d ' ' -f "1 2")
17+
COMPREPLY=( $(compgen -W "${options}" -- ${current}) )
18+
return 0
19+
fi
20+
return 1
21+
}
22+
23+
function exists_in_list()
24+
{
25+
LIST=$1
26+
DELIMITER=$2
27+
VALUE=$3
28+
[[ "$LIST" =~ ($DELIMITER|^)$VALUE($DELIMITER|$) ]]
29+
}
30+
31+
function add_command_help()
32+
{
33+
if [[ ${previous} == ${command} ]] ; then
34+
#only add help if we are first after command
35+
COMPREPLY+=( $(compgen -W "${help_options}" -- ${current}) )
36+
fi
37+
if exists_in_list "${help_options}" " " "${previous}" ; then
38+
# if we have choosen help option, nothing should follow
39+
return 0
40+
fi
41+
}
42+
43+
44+
_partup_completions()
45+
{
46+
local current=""
47+
local previous=""
48+
local command=""
49+
50+
local partup_commands="install package show version"
51+
52+
if [[ ${COMP_CWORD} > 0 ]] ; then
53+
current="${COMP_WORDS[COMP_CWORD]}"
54+
55+
if [[ ${COMP_CWORD} > 1 ]] ; then
56+
# iterate over all COMP_WORDS
57+
for s in "${COMP_WORDS[@]:1}"; do
58+
if exists_in_list "${partup_commands}" " " "${s}" ; then
59+
command="${s}"
60+
fi
61+
((i++))
62+
done
63+
previous="${COMP_WORDS[COMP_CWORD - 1]}"
64+
fi
65+
fi
66+
67+
if [[ -z ${command} ]] ; then
68+
COMPREPLY=( $(compgen -W "${help_options} ${partup_commands} ${app_options}" -- ${current}) )
69+
return 0
70+
fi
71+
72+
# we may have things like "partup --debug install ..."
73+
74+
if [[ ${command} == install ]] ; then
75+
add_command_help ${command} ${current} ${previous}
76+
77+
COMPREPLY+=( $(compgen -W "${install_options} ${app_options}" -- ${current}) )
78+
79+
# PACKAGE
80+
COMPREPLY+=( $(compgen -o plusdirs -f -X '!*.partup' -- ${current}) )
81+
82+
# TODO DEVICE -> if previous ends with partup, complete only dev?!
83+
84+
return 0
85+
86+
fi
87+
88+
if [[ ${command} == package ]] ; then
89+
add_command_help ${command} ${current} ${previous}
90+
91+
COMPREPLY+=( $(compgen -W "${package_options} ${app_options}" -- ${current}) )
92+
93+
# -C, --directory=DIR
94+
# PACKAGE FILES…
95+
96+
if exists_in_list "${package_options}" " " ${previous} ; then
97+
#if output is previous, a file must follow
98+
COMPREPLY=( $(compgen -f -- ${current}) )
99+
return 0
100+
fi
101+
fi
102+
103+
if [[ ${command} == show ]] ; then
104+
add_command_help ${command} ${current} ${previous}
105+
106+
COMPREPLY+=( $(compgen -W "${show_options} ${app_options}" -- ${current}) )
107+
108+
# PACKAGE
109+
COMPREPLY+=( $(compgen -o plusdirs -f -X '!*.partup' -- ${current}) )
110+
fi
111+
}
112+
113+
complete -o filenames -F _partup_completions partup

0 commit comments

Comments
 (0)