Refactor _hledger_compreply_optarg()

It used to do a lot more work parsing the command line that is
no longer necessary. Clean up redundant code.
This commit is contained in:
Vladimir Zhelezov 2020-12-11 08:50:03 +01:00
parent 9d94f84781
commit d30913c6a8
2 changed files with 8 additions and 32 deletions

View File

@ -188,16 +188,12 @@ _hledger_compgen() {
# Try required option argument completion. Set COMPREPLY and return 0 on # Try required option argument completion. Set COMPREPLY and return 0 on
# success, 1 if option doesn't require an argument or out of context # success, 1 if option doesn't require an argument or out of context
_hledger_compreply_optarg() { _hledger_compreply_optarg() {
local optionIndex=${1:-$((cword - 1))} local match wordlist
local recursionLevel=${2:-0}
local wordlist
local error=0
local match
# Match the empty string on --file=<TAB> # Match the empty string on --file=<TAB>
[[ $cur == = ]] || match=$cur [[ $cur == = ]] || match=$cur
case ${words[optionIndex]} in case $prev in
--alias) --alias)
compopt -o nospace compopt -o nospace
_hledger_compreply "$(_hledger_compgen "$(_hledger accounts --flat)" "" "$match")" _hledger_compreply "$(_hledger_compgen "$(_hledger accounts --flat)" "" "$match")"
@ -238,20 +234,12 @@ _hledger_compreply_optarg() {
-b|--begin|-e|--end|-p|--period|--depth|--drop) -b|--begin|-e|--end|-p|--period|--depth|--drop)
_hledger_compreply "" _hledger_compreply ""
;; ;;
=)
# Recurse only once!
((recursionLevel > 1)) && return 1
if [[ ${words[optionIndex - 1]} == -* ]]; then
_hledger_compreply_optarg $((optionIndex - 1)) $((recursionLevel + 1))
error=$?
fi
;;
*) *)
error=1 return 1
;; ;;
esac esac
return $error return 0
} }
# Query filter completion through introspection # Query filter completion through introspection

View File

@ -188,16 +188,12 @@ _hledger_compgen() {
# Try required option argument completion. Set COMPREPLY and return 0 on # Try required option argument completion. Set COMPREPLY and return 0 on
# success, 1 if option doesn't require an argument or out of context # success, 1 if option doesn't require an argument or out of context
_hledger_compreply_optarg() { _hledger_compreply_optarg() {
local optionIndex=${1:-$((cword - 1))} local match wordlist
local recursionLevel=${2:-0}
local wordlist
local error=0
local match
# Match the empty string on --file=<TAB> # Match the empty string on --file=<TAB>
[[ $cur == = ]] || match=$cur [[ $cur == = ]] || match=$cur
case ${words[optionIndex]} in case $prev in
--alias) --alias)
compopt -o nospace compopt -o nospace
_hledger_compreply "$(_hledger_compgen "$(_hledger accounts --flat)" "" "$match")" _hledger_compreply "$(_hledger_compgen "$(_hledger accounts --flat)" "" "$match")"
@ -238,20 +234,12 @@ _hledger_compreply_optarg() {
-b|--begin|-e|--end|-p|--period|--depth|--drop) -b|--begin|-e|--end|-p|--period|--depth|--drop)
_hledger_compreply "" _hledger_compreply ""
;; ;;
=)
# Recurse only once!
((recursionLevel > 1)) && return 1
if [[ ${words[optionIndex - 1]} == -* ]]; then
_hledger_compreply_optarg $((optionIndex - 1)) $((recursionLevel + 1))
error=$?
fi
;;
*) *)
error=1 return 1
;; ;;
esac esac
return $error return 0
} }
# Query filter completion through introspection # Query filter completion through introspection