From 9e50c7401d5185df2d03db6d709cea0ade0143d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nirgal=20Vourg=C3=A8re?= Date: Tue, 27 Oct 2020 00:05:26 +0100 Subject: [PATCH] Modernize bash-completion Use _init_completion Support long options with or without = Auto generate options from the --help using _parse_help --- src/util/bash-completion/mdb-count | 16 +++--------- src/util/bash-completion/mdb-export | 35 ++++++++++----------------- src/util/bash-completion/mdb-hexdump | 7 +++--- src/util/bash-completion/mdb-import | 30 +++++++++-------------- src/util/bash-completion/mdb-json | 28 +++++++++------------ src/util/bash-completion/mdb-parsecsv | 7 +++--- src/util/bash-completion/mdb-prop | 24 ++++++------------ src/util/bash-completion/mdb-queries | 21 ++++------------ src/util/bash-completion/mdb-schema | 26 ++++++++------------ src/util/bash-completion/mdb-sql | 24 ++++++++---------- src/util/bash-completion/mdb-tables | 23 ++++++++---------- src/util/bash-completion/mdb-ver | 9 +++---- 12 files changed, 93 insertions(+), 157 deletions(-) diff --git a/src/util/bash-completion/mdb-count b/src/util/bash-completion/mdb-count index 1513d6b..848413b 100644 --- a/src/util/bash-completion/mdb-count +++ b/src/util/bash-completion/mdb-count @@ -1,26 +1,18 @@ #-*- mode: shell-script;-*- _mdb_count() { - local cur prev - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} - prev=${COMP_WORDS[COMP_CWORD-1]} + local cur prev words cword + _init_completion || return if [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then local dbname local tablenames - local tablename dbname=$prev __expand_tilde_by_ref dbname local IFS=$'\n' - tablenames="" - while read tablename - do - #shell-quote each line - tablenames="$tablenames"$'\n'"$(printf %q "${tablename}")" - done < <(eval mdb-tables -S -1 "${dbname}" 2>/dev/null) + tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)" compopt -o filenames - COMPREPLY=( $( compgen -W "${tablenames}" -- $cur ) ) + COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) ) else _filedir '@(mdb|mdw|accdb)' fi diff --git a/src/util/bash-completion/mdb-export b/src/util/bash-completion/mdb-export index ba8ba40..7a41482 100644 --- a/src/util/bash-completion/mdb-export +++ b/src/util/bash-completion/mdb-export @@ -1,40 +1,31 @@ #-*- mode: shell-script;-*- _mdb_export() { - local cur prev - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} - prev=${COMP_WORDS[COMP_CWORD-1]} + local cur prev words cword split + _init_completion -s || return 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 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 - COMPREPLY=( $( compgen -W 'strip raw octal hex' -- $cur ) ) - elif [[ "$cur" == -* ]]; then - COMPREPLY=( $( compgen -W '-H -d -R -Q -q -X -I -N -S -D -T -0 -B -h \ - --no-header --delimiter --row-delimiter \ - --no-quote --quote --escape \ - --insert --namespace --batch-size \ - --date-format --datetime-format \ - --null --bin --boolean-words - --help' -- $cur ) ) + COMPREPLY=( $( compgen -W 'strip raw octal hex' -- "$cur" ) ) + fi + + $split && return + + if [[ "$cur" == -* ]]; then + COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur")) + [[ $COMPREPLY == *= ]] && compopt -o nospace elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then local dbname local tablenames - local tablename dbname=$prev __expand_tilde_by_ref dbname local IFS=$'\n' - tablenames="" - while read tablename - do - #shell-quote each line - tablenames="$tablenames"$'\n'"$(printf %q "${tablename}")" - done < <(eval mdb-tables -S -1 "${dbname}" 2>/dev/null) + tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)" compopt -o filenames - COMPREPLY=( $( compgen -W "${tablenames}" -- $cur ) ) + COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) ) else _filedir '@(mdb|mdw|accdb)' fi diff --git a/src/util/bash-completion/mdb-hexdump b/src/util/bash-completion/mdb-hexdump index 13d5d43..d13a3b8 100644 --- a/src/util/bash-completion/mdb-hexdump +++ b/src/util/bash-completion/mdb-hexdump @@ -1,11 +1,10 @@ #-*- mode: shell-script;-*- _mdb_hexdump() { - local cur - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} + local cur prev words cword + _init_completion || return - if (( COMP_CWORD == 1 )); then + if (( $cword == 1 )); then _filedir '@(mdb|mdw|accdb)' fi return 0 diff --git a/src/util/bash-completion/mdb-import b/src/util/bash-completion/mdb-import index 88b29c4..733dea6 100644 --- a/src/util/bash-completion/mdb-import +++ b/src/util/bash-completion/mdb-import @@ -1,33 +1,27 @@ #-*- mode: shell-script;-*- _mdb_import() { - local cur - - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} - prev=${COMP_WORDS[COMP_CWORD-1]} + local cur prev words cword split + _init_completion -s || return - if [[ "$prev" == -@(d|-delimiter) ]]; then + if [[ "$prev" == -@(H|--header|d|-delimiter) ]]; then return 0 - elif [[ "$cur" == -* ]]; then - COMPREPLY=( $( compgen -W '-H --header \ - -d --delimiter \ - -h --help' -- $cur ) ) + fi + + $split && return + + if [[ "$cur" == -* ]]; then + COMPREPLY=( $( compgen -W '--header= --delimiter= --help' -- "$cur" ) ) + [[ $COMPREPLY == *= ]] && compopt -o nospace elif [[ "$prev" == @(*mdb|*mdw|*accdb) ]]; then local dbname local tablenames - local tablename dbname=$prev __expand_tilde_by_ref dbname local IFS=$'\n' - tablenames="" - while read tablename - do - #shell-quote each line - tablenames="$tablenames"$'\n'"$(printf %q "${tablename}")" - done < <(eval mdb-tables -S -1 "${dbname}" 2>/dev/null) + tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)" compopt -o filenames - COMPREPLY=( $( compgen -W "${tablenames}" -- $cur ) ) + COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) ) else _filedir '@(mdb|mdw|accdb|txt|csv)' fi diff --git a/src/util/bash-completion/mdb-json b/src/util/bash-completion/mdb-json index beb201f..d2e7b35 100644 --- a/src/util/bash-completion/mdb-json +++ b/src/util/bash-completion/mdb-json @@ -1,33 +1,27 @@ #-*- mode: shell-script;-*- _mdb_json() { - local cur prev - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} - prev=${COMP_WORDS[COMP_CWORD-1]} + local cur prev words cword split + _init_completion -s || return if [[ "$prev" == -@(D|-date-format|T|-datetime-format|U|--no-unprintable|h|-help) ]] ; then return 0 - elif [[ "$cur" == -* ]]; then - COMPREPLY=( $( compgen -W '-h -D -T -U \ - --date-format --datetime-format \ - --no-unprintable \ - --help' -- $cur ) ) + fi + + $split && return + + if [[ "$cur" == -* ]]; then + COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur")) + [[ $COMPREPLY == *= ]] && compopt -o nospace elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then local dbname local tablenames - local tablename dbname=$prev __expand_tilde_by_ref dbname local IFS=$'\n' - tablenames="" - while read tablename - do - #shell-quote each line - tablenames="$tablenames"$'\n'"$(printf %q "${tablename}")" - done < <(eval mdb-tables -S -1 "${dbname}" 2>/dev/null) + tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)" compopt -o filenames - COMPREPLY=( $( compgen -W "${tablenames}" -- $cur ) ) + COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) ) else _filedir '@(mdb|mdw|accdb)' fi diff --git a/src/util/bash-completion/mdb-parsecsv b/src/util/bash-completion/mdb-parsecsv index 5e7eb28..5a24fb3 100644 --- a/src/util/bash-completion/mdb-parsecsv +++ b/src/util/bash-completion/mdb-parsecsv @@ -1,11 +1,10 @@ #-*- mode: shell-script;-*- _mdb_parsecsv() { - local cur - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} + local cur prev words cword + _init_completion || return - if (( COMP_CWORD == 1 )); then + if (( $cword == 1 )); then _filedir '@(txt|csv)' fi return 0 diff --git a/src/util/bash-completion/mdb-prop b/src/util/bash-completion/mdb-prop index 4f9d7bf..951559f 100644 --- a/src/util/bash-completion/mdb-prop +++ b/src/util/bash-completion/mdb-prop @@ -1,30 +1,22 @@ #-*- mode: shell-script;-*- _mdb_prop() { - local cur - - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} + local cur prev words cword + _init_completion || return - if (( COMP_CWORD == 1 )); then + if (( $cword == 1 )); then _filedir '@(mdb|mdw|accdb)' - elif (( COMP_CWORD == 2 )); then + elif (( $cword == 2 )); then local dbname local tablenames - local tablename dbname=$prev __expand_tilde_by_ref dbname local IFS=$'\n' - tablenames="" - while read tablename - do - #shell-quote each line - tablenames="$tablenames"$'\n'"$(printf %q "${tablename}")" - done < <(eval mdb-tables -S -1 "${dbname}" 2>/dev/null) + tablenames="$(eval mdb-tables -S -1 "${dbname}" 2>/dev/null)" compopt -o filenames - COMPREPLY=( $( compgen -W "${tablenames}" -- $cur ) ) - elif (( COMP_CWORD == 3 )); then - COMPREPLY=( $( compgen -W 'Lv LvProp LvModule LvExtra' -- $cur ) ) + COMPREPLY=( $( compgen -W '${tablenames}' -- "$cur" ) ) + elif (( $cword == 3 )); then + COMPREPLY=( $( compgen -W 'Lv LvProp LvModule LvExtra' -- "$cur" ) ) fi return 0 } && diff --git a/src/util/bash-completion/mdb-queries b/src/util/bash-completion/mdb-queries index 1ea920f..fd14a43 100644 --- a/src/util/bash-completion/mdb-queries +++ b/src/util/bash-completion/mdb-queries @@ -1,33 +1,22 @@ #-*- mode: shell-script;-*- _mdb_queries() { - local cur prev - - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} - prev=${COMP_WORDS[COMP_CWORD-1]} + local cur prev words cword + _init_completion || return if [[ "$prev" == -@(d) ]]; then return 0 elif [[ "$cur" == -* ]]; then - COMPREPLY=( $( compgen -W '-L\ - -1 \ - -d' -- $cur ) ) + COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur")) elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then local dbname local querynames - local queryname dbname=$prev __expand_tilde_by_ref dbname local IFS=$'\n' - querynames="" - while read queryname - do - #shell-quote each line - querynames="$querynames"$'\n'"$(printf %q "${queryname}")" - done < <(eval mdb-queries -1 "${dbname}" 2>/dev/null) + querynames="$(eval mdb-queries -1 "${dbname}" 2>/dev/null)" compopt -o filenames - COMPREPLY=( $( compgen -W "${querynames}" -- $cur ) ) + COMPREPLY=( $( compgen -W "${querynames}" -- "$cur" ) ) else _filedir '@(mdb|mdw|accdb)' fi diff --git a/src/util/bash-completion/mdb-schema b/src/util/bash-completion/mdb-schema index 8f90ce1..123ff7c 100644 --- a/src/util/bash-completion/mdb-schema +++ b/src/util/bash-completion/mdb-schema @@ -1,26 +1,20 @@ #-*- mode: shell-script;-*- _mdb_schema() { - local cur prev - - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} - prev=${COMP_WORDS[COMP_CWORD-1]} + local cur prev words cword split + _init_completion -s || return if [[ "$prev" == -@(T|-table|N|-namespace) ]] ; then return 0 - elif [[ "$cur" == -* ]]; then - COMPREPLY=( $( compgen -W '-T --table \ - -N --namespace \ - --drop-table --no-drop-table \ - --not-null --no-not-null \ - --default-values --no-default-values \ - --not-empty --no-not-empty \ - --indexes --no-indexes \ - --relations --no-relations - -h --help' -- $cur ) ) + fi + + $split && return + + if [[ "$cur" == -* ]]; then + COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur")) + [[ $COMPREPLY == *= ]] && compopt -o nospace 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 _filedir '@(mdb|mdw|accdb)' fi diff --git a/src/util/bash-completion/mdb-sql b/src/util/bash-completion/mdb-sql index 2063fac..a3cc420 100644 --- a/src/util/bash-completion/mdb-sql +++ b/src/util/bash-completion/mdb-sql @@ -1,24 +1,20 @@ #-*- mode: shell-script;-*- _mdb_sql() { - local cur prev - - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} - prev=${COMP_WORDS[COMP_CWORD-1]} + local cur prev words cword split + _init_completion -s || return - if [[ "$prev" == -d ]] ; then + if [[ "$prev" == -@(P|-no-pretty-print|H|-no-header|F|-no-footer) ]] ; then 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 _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 _filedir '@(mdb|mdw|accdb)' fi diff --git a/src/util/bash-completion/mdb-tables b/src/util/bash-completion/mdb-tables index 5057a3b..1cb608a 100644 --- a/src/util/bash-completion/mdb-tables +++ b/src/util/bash-completion/mdb-tables @@ -1,24 +1,21 @@ #-*- mode: shell-script;-*- _mdb_tables() { - local cur prev - - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} - prev=${COMP_WORDS[COMP_CWORD-1]} + local cur prev words cword split + _init_completion -s || return if [[ "$prev" == -@(d|-delimiter) ]]; then return 0 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 - elif [[ "$cur" == -* ]]; then - COMPREPLY=( $( compgen -W '-S --system\ - -1 --single-column \ - -d --delimiter \ - -t --type \ - -T --showtype \ - -h --help' -- $cur ) ) + fi + + $split && return + + if [[ "$cur" == -* ]]; then + COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur")) + [[ $COMPREPLY == *= ]] && compopt -o nospace else _filedir '@(mdb|mdw|accdb)' fi diff --git a/src/util/bash-completion/mdb-ver b/src/util/bash-completion/mdb-ver index 7b614c2..b67bf89 100644 --- a/src/util/bash-completion/mdb-ver +++ b/src/util/bash-completion/mdb-ver @@ -1,13 +1,12 @@ #-*- mode: shell-script;-*- _mdb_ver() { - local cur - - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} + local cur prev words cword + _init_completion || return if [[ "$cur" == -* ]]; then - COMPREPLY=( $( compgen -W '-M -h --help' -- $cur ) ) + COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur")) + #COMPREPLY=($(compgen -W '--help --mdbtools' -- "$cur")) else _filedir '@(mdb|mdw|accdb)' fi