Modernize bash-completion

Use _init_completion
Support long options with or without =
Auto generate options from the --help using _parse_help
This commit is contained in:
Nirgal Vourgère
2020-10-27 00:05:26 +01:00
parent 58f240ac6f
commit 9e50c7401d
12 changed files with 93 additions and 157 deletions

View File

@@ -1,26 +1,18 @@
#-*- mode: shell-script;-*- #-*- mode: shell-script;-*-
_mdb_count() _mdb_count()
{ {
local cur prev local cur prev words cword
COMPREPLY=() _init_completion || return
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then if [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then
local dbname local dbname
local tablenames local tablenames
local tablename
dbname=$prev dbname=$prev
__expand_tilde_by_ref dbname __expand_tilde_by_ref dbname
local IFS=$'\n' local IFS=$'\n'
tablenames="" tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)"
while read tablename
do
#shell-quote each line
tablenames="$tablenames"$'\n'"$(printf %q "${tablename}")"
done < <(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)
compopt -o filenames compopt -o filenames
COMPREPLY=( $( compgen -W "${tablenames}" -- $cur ) ) COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) )
else else
_filedir '@(mdb|mdw|accdb)' _filedir '@(mdb|mdw|accdb)'
fi fi

View File

@@ -1,40 +1,31 @@
#-*- mode: shell-script;-*- #-*- mode: shell-script;-*-
_mdb_export() _mdb_export()
{ {
local cur prev local cur prev words cword split
COMPREPLY=() _init_completion -s || return
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == -@(d|-delimiter|R|-row-delimiter|q|-quote|X|-escape|N|-namespace|S|-batch-size|D|-date-format|T|-datetime-format|0|-null|h|-help) ]] ; then if [[ "$prev" == -@(d|-delimiter|R|-row-delimiter|q|-quote|X|-escape|N|-namespace|S|-batch-size|D|-date-format|T|-datetime-format|0|-null|h|-help) ]] ; then
return 0 return 0
elif [[ "$prev" == -@(I|-insert) ]] ; then elif [[ "$prev" == -@(I|-insert) ]] ; then
COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql sqlite' -- $cur ) ) COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql sqlite' -- "$cur" ) )
elif [[ "$prev" == -@(b|-bin) ]] ; then elif [[ "$prev" == -@(b|-bin) ]] ; then
COMPREPLY=( $( compgen -W 'strip raw octal hex' -- $cur ) ) COMPREPLY=( $( compgen -W 'strip raw octal hex' -- "$cur" ) )
elif [[ "$cur" == -* ]]; then fi
COMPREPLY=( $( compgen -W '-H -d -R -Q -q -X -I -N -S -D -T -0 -B -h \
--no-header --delimiter --row-delimiter \ $split && return
--no-quote --quote --escape \
--insert --namespace --batch-size \ if [[ "$cur" == -* ]]; then
--date-format --datetime-format \ COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
--null --bin --boolean-words [[ $COMPREPLY == *= ]] && compopt -o nospace
--help' -- $cur ) )
elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then
local dbname local dbname
local tablenames local tablenames
local tablename
dbname=$prev dbname=$prev
__expand_tilde_by_ref dbname __expand_tilde_by_ref dbname
local IFS=$'\n' local IFS=$'\n'
tablenames="" tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)"
while read tablename
do
#shell-quote each line
tablenames="$tablenames"$'\n'"$(printf %q "${tablename}")"
done < <(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)
compopt -o filenames compopt -o filenames
COMPREPLY=( $( compgen -W "${tablenames}" -- $cur ) ) COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) )
else else
_filedir '@(mdb|mdw|accdb)' _filedir '@(mdb|mdw|accdb)'
fi fi

View File

@@ -1,11 +1,10 @@
#-*- mode: shell-script;-*- #-*- mode: shell-script;-*-
_mdb_hexdump() _mdb_hexdump()
{ {
local cur local cur prev words cword
COMPREPLY=() _init_completion || return
cur=${COMP_WORDS[COMP_CWORD]}
if (( COMP_CWORD == 1 )); then if (( $cword == 1 )); then
_filedir '@(mdb|mdw|accdb)' _filedir '@(mdb|mdw|accdb)'
fi fi
return 0 return 0

View File

@@ -1,33 +1,27 @@
#-*- mode: shell-script;-*- #-*- mode: shell-script;-*-
_mdb_import() _mdb_import()
{ {
local cur local cur prev words cword split
_init_completion -s || return
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == -@(d|-delimiter) ]]; then if [[ "$prev" == -@(H|--header|d|-delimiter) ]]; then
return 0 return 0
elif [[ "$cur" == -* ]]; then fi
COMPREPLY=( $( compgen -W '-H --header \
-d --delimiter \ $split && return
-h --help' -- $cur ) )
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '--header= --delimiter= --help' -- "$cur" ) )
[[ $COMPREPLY == *= ]] && compopt -o nospace
elif [[ "$prev" == @(*mdb|*mdw|*accdb) ]]; then elif [[ "$prev" == @(*mdb|*mdw|*accdb) ]]; then
local dbname local dbname
local tablenames local tablenames
local tablename
dbname=$prev dbname=$prev
__expand_tilde_by_ref dbname __expand_tilde_by_ref dbname
local IFS=$'\n' local IFS=$'\n'
tablenames="" tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)"
while read tablename
do
#shell-quote each line
tablenames="$tablenames"$'\n'"$(printf %q "${tablename}")"
done < <(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)
compopt -o filenames compopt -o filenames
COMPREPLY=( $( compgen -W "${tablenames}" -- $cur ) ) COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) )
else else
_filedir '@(mdb|mdw|accdb|txt|csv)' _filedir '@(mdb|mdw|accdb|txt|csv)'
fi fi

View File

@@ -1,33 +1,27 @@
#-*- mode: shell-script;-*- #-*- mode: shell-script;-*-
_mdb_json() _mdb_json()
{ {
local cur prev local cur prev words cword split
COMPREPLY=() _init_completion -s || return
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == -@(D|-date-format|T|-datetime-format|U|--no-unprintable|h|-help) ]] ; then if [[ "$prev" == -@(D|-date-format|T|-datetime-format|U|--no-unprintable|h|-help) ]] ; then
return 0 return 0
elif [[ "$cur" == -* ]]; then fi
COMPREPLY=( $( compgen -W '-h -D -T -U \
--date-format --datetime-format \ $split && return
--no-unprintable \
--help' -- $cur ) ) if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
[[ $COMPREPLY == *= ]] && compopt -o nospace
elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then
local dbname local dbname
local tablenames local tablenames
local tablename
dbname=$prev dbname=$prev
__expand_tilde_by_ref dbname __expand_tilde_by_ref dbname
local IFS=$'\n' local IFS=$'\n'
tablenames="" tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)"
while read tablename
do
#shell-quote each line
tablenames="$tablenames"$'\n'"$(printf %q "${tablename}")"
done < <(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)
compopt -o filenames compopt -o filenames
COMPREPLY=( $( compgen -W "${tablenames}" -- $cur ) ) COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) )
else else
_filedir '@(mdb|mdw|accdb)' _filedir '@(mdb|mdw|accdb)'
fi fi

View File

@@ -1,11 +1,10 @@
#-*- mode: shell-script;-*- #-*- mode: shell-script;-*-
_mdb_parsecsv() _mdb_parsecsv()
{ {
local cur local cur prev words cword
COMPREPLY=() _init_completion || return
cur=${COMP_WORDS[COMP_CWORD]}
if (( COMP_CWORD == 1 )); then if (( $cword == 1 )); then
_filedir '@(txt|csv)' _filedir '@(txt|csv)'
fi fi
return 0 return 0

View File

@@ -1,30 +1,22 @@
#-*- mode: shell-script;-*- #-*- mode: shell-script;-*-
_mdb_prop() _mdb_prop()
{ {
local cur local cur prev words cword
_init_completion || return
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
if (( COMP_CWORD == 1 )); then if (( $cword == 1 )); then
_filedir '@(mdb|mdw|accdb)' _filedir '@(mdb|mdw|accdb)'
elif (( COMP_CWORD == 2 )); then elif (( $cword == 2 )); then
local dbname local dbname
local tablenames local tablenames
local tablename
dbname=$prev dbname=$prev
__expand_tilde_by_ref dbname __expand_tilde_by_ref dbname
local IFS=$'\n' local IFS=$'\n'
tablenames="" tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)"
while read tablename
do
#shell-quote each line
tablenames="$tablenames"$'\n'"$(printf %q "${tablename}")"
done < <(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)
compopt -o filenames compopt -o filenames
COMPREPLY=( $( compgen -W "${tablenames}" -- $cur ) ) COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) )
elif (( COMP_CWORD == 3 )); then elif (( $cword == 3 )); then
COMPREPLY=( $( compgen -W 'Lv LvProp LvModule LvExtra' -- $cur ) ) COMPREPLY=( $( compgen -W 'Lv LvProp LvModule LvExtra' -- "$cur" ) )
fi fi
return 0 return 0
} && } &&

View File

@@ -1,33 +1,22 @@
#-*- mode: shell-script;-*- #-*- mode: shell-script;-*-
_mdb_queries() _mdb_queries()
{ {
local cur prev local cur prev words cword
_init_completion || return
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == -@(d) ]]; then if [[ "$prev" == -@(d) ]]; then
return 0 return 0
elif [[ "$cur" == -* ]]; then elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-L\ COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
-1 \
-d' -- $cur ) )
elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then
local dbname local dbname
local querynames local querynames
local queryname
dbname=$prev dbname=$prev
__expand_tilde_by_ref dbname __expand_tilde_by_ref dbname
local IFS=$'\n' local IFS=$'\n'
querynames="" querynames="$(eval mdb-queries -1 "${dbname}" 2>/dev/null)"
while read queryname
do
#shell-quote each line
querynames="$querynames"$'\n'"$(printf %q "${queryname}")"
done < <(eval mdb-queries -1 "${dbname}" 2>/dev/null)
compopt -o filenames compopt -o filenames
COMPREPLY=( $( compgen -W "${querynames}" -- $cur ) ) COMPREPLY=( $( compgen -W "${querynames}" -- "$cur" ) )
else else
_filedir '@(mdb|mdw|accdb)' _filedir '@(mdb|mdw|accdb)'
fi fi

View File

@@ -1,26 +1,20 @@
#-*- mode: shell-script;-*- #-*- mode: shell-script;-*-
_mdb_schema() _mdb_schema()
{ {
local cur prev local cur prev words cword split
_init_completion -s || return
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == -@(T|-table|N|-namespace) ]] ; then if [[ "$prev" == -@(T|-table|N|-namespace) ]] ; then
return 0 return 0
elif [[ "$cur" == -* ]]; then fi
COMPREPLY=( $( compgen -W '-T --table \
-N --namespace \ $split && return
--drop-table --no-drop-table \
--not-null --no-not-null \ if [[ "$cur" == -* ]]; then
--default-values --no-default-values \ COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
--not-empty --no-not-empty \ [[ $COMPREPLY == *= ]] && compopt -o nospace
--indexes --no-indexes \
--relations --no-relations
-h --help' -- $cur ) )
elif [[ "$prev" == @(*mdb|*mdw|*accdb) ]]; then elif [[ "$prev" == @(*mdb|*mdw|*accdb) ]]; then
COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql' -- $cur ) ) COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql sqlite' -- "$cur" ) )
else else
_filedir '@(mdb|mdw|accdb)' _filedir '@(mdb|mdw|accdb)'
fi fi

View File

@@ -1,24 +1,20 @@
#-*- mode: shell-script;-*- #-*- mode: shell-script;-*-
_mdb_sql() _mdb_sql()
{ {
local cur prev local cur prev words cword split
_init_completion -s || return
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == -d ]] ; then if [[ "$prev" == -@(P|-no-pretty-print|H|-no-header|F|-no-footer) ]] ; then
return 0 return 0
fi
$split && return
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '$(_parse_help "$1")' -- "$cur"))
[[ $COMPREPLY == *= ]] && compopt -o nospace
elif [[ "$prev" == -@(i|-input|o|-output) ]] ; then elif [[ "$prev" == -@(i|-input|o|-output) ]] ; then
_filedir _filedir
elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-H --no-header \
-F --no-footer \
-p --no-pretty-print \
-d --delimiter \
-i --input \
-o --output \
-h --help' -- $cur ) )
else else
_filedir '@(mdb|mdw|accdb)' _filedir '@(mdb|mdw|accdb)'
fi fi

View File

@@ -1,24 +1,21 @@
#-*- mode: shell-script;-*- #-*- mode: shell-script;-*-
_mdb_tables() _mdb_tables()
{ {
local cur prev local cur prev words cword split
_init_completion -s || return
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == -@(d|-delimiter) ]]; then if [[ "$prev" == -@(d|-delimiter) ]]; then
return 0 return 0
elif [[ "$prev" == -@(t|-type) ]]; then elif [[ "$prev" == -@(t|-type) ]]; then
COMPREPLY=( $( compgen -W 'form table macro systable report query linkedtable module relationship dbprop any all' -- $cur ) ) COMPREPLY=( $( compgen -W 'form table macro systable report query linkedtable module relationship dbprop any all' -- "$cur" ) )
return 0 return 0
elif [[ "$cur" == -* ]]; then fi
COMPREPLY=( $( compgen -W '-S --system\
-1 --single-column \ $split && return
-d --delimiter \
-t --type \ if [[ "$cur" == -* ]]; then
-T --showtype \ COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
-h --help' -- $cur ) ) [[ $COMPREPLY == *= ]] && compopt -o nospace
else else
_filedir '@(mdb|mdw|accdb)' _filedir '@(mdb|mdw|accdb)'
fi fi

View File

@@ -1,13 +1,12 @@
#-*- mode: shell-script;-*- #-*- mode: shell-script;-*-
_mdb_ver() _mdb_ver()
{ {
local cur local cur prev words cword
_init_completion || return
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
if [[ "$cur" == -* ]]; then if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-M -h --help' -- $cur ) ) COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
#COMPREPLY=($(compgen -W '--help --mdbtools' -- "$cur"))
else else
_filedir '@(mdb|mdw|accdb)' _filedir '@(mdb|mdw|accdb)'
fi fi