Fix: pass *all* accounts and rules files to hledger calls

_hledger_optarg() is used to parse the options on the command line
and provide their arguments for context aware completion suggestions
This commit is contained in:
Vladimir Zhelezov 2020-12-08 07:09:36 +01:00
parent d98ff3b93d
commit dee25d4811
2 changed files with 66 additions and 48 deletions

View File

@ -263,40 +263,49 @@ _hledger_compreply_query() {
return 0 return 0
} }
# Get ledger file from -f --file arguments from COMP_WORDS and pass it to the # Parse the command line so far and fill the array $optarg with the arguments to
# 'hledger' call. Note that --rules-file - if present - must also be passed! # given options. $optarg should be declared by the caller
_hledger() { _hledger_optarg() {
local hledgerArgs=("$@") local options=("$@")
local hledgerFile local i j offset
local hledgerRulesFile optarg=()
local i
# hledger balance --file ~/ledger _ # hledger balance --file ~/ledger _
# 0 1 2 3 4 # 0 1 2 3 4
for (( i=1; i < ${#COMP_WORDS[@]} - 2; i++ )); do for (( i=1; i < ${#COMP_WORDS[@]} - 2; i++ )); do
case ${COMP_WORDS[i]} in offset=0
-f|--file) for j in "${!options[@]}"; do
if [[ ${COMP_WORDS[i]} == "${options[j]}" ]]; then
if [[ ${COMP_WORDS[i+1]} == '=' ]]; then if [[ ${COMP_WORDS[i+1]} == '=' ]]; then
hledgerFile=${COMP_WORDS[i+2]} offset=2
else else
hledgerFile=${COMP_WORDS[i+1]} offset=1
fi fi
# Pass it through compgen to unescape it # Pass it through compgen to unescape it
hledgerFile=$(compgen -W "$hledgerFile") optarg+=("$(compgen -W "${COMP_WORDS[i + offset]}")")
;; fi
--rules-file) done
if [[ ${COMP_WORDS[i+1]} == '=' ]]; then ((i += offset))
hledgerRulesFile=${COMP_WORDS[i+2]} done
else }
hledgerRulesFile=${COMP_WORDS[i+1]}
fi # Get ledger file from -f --file arguments from COMP_WORDS and pass it to the
hledgerRulesFile=$(compgen -W "$hledgerRulesFile") # 'hledger' call. Note that --rules-file - if present - must also be passed!
;; # Multiple files are allowed so pass them all in the order of appearance.
esac _hledger() {
local hledgerArgs=("$@")
local file
local -a optarg
_hledger_optarg -f --file
for file in "${optarg[@]}"; do
[[ -f $file ]] && hledgerArgs+=(--file "$file")
done done
[[ -f $hledgerFile ]] && hledgerArgs+=(--file "$hledgerFile") _hledger_optarg --rules-file
[[ -f $hledgerRulesFile ]] && hledgerArgs+=(--rules-file "$hledgerRulesFile") for file in "${optarg[@]}"; do
[[ -f $file ]] && hledgerArgs+=(--rules-file "$file")
done
# Discard errors. Is there a way to validate files before using them? # Discard errors. Is there a way to validate files before using them?
hledger "${hledgerArgs[@]}" 2>/dev/null hledger "${hledgerArgs[@]}" 2>/dev/null

View File

@ -263,40 +263,49 @@ _hledger_compreply_query() {
return 0 return 0
} }
# Get ledger file from -f --file arguments from COMP_WORDS and pass it to the # Parse the command line so far and fill the array $optarg with the arguments to
# 'hledger' call. Note that --rules-file - if present - must also be passed! # given options. $optarg should be declared by the caller
_hledger() { _hledger_optarg() {
local hledgerArgs=("$@") local options=("$@")
local hledgerFile local i j offset
local hledgerRulesFile optarg=()
local i
# hledger balance --file ~/ledger _ # hledger balance --file ~/ledger _
# 0 1 2 3 4 # 0 1 2 3 4
for (( i=1; i < ${#COMP_WORDS[@]} - 2; i++ )); do for (( i=1; i < ${#COMP_WORDS[@]} - 2; i++ )); do
case ${COMP_WORDS[i]} in offset=0
-f|--file) for j in "${!options[@]}"; do
if [[ ${COMP_WORDS[i]} == "${options[j]}" ]]; then
if [[ ${COMP_WORDS[i+1]} == '=' ]]; then if [[ ${COMP_WORDS[i+1]} == '=' ]]; then
hledgerFile=${COMP_WORDS[i+2]} offset=2
else else
hledgerFile=${COMP_WORDS[i+1]} offset=1
fi fi
# Pass it through compgen to unescape it # Pass it through compgen to unescape it
hledgerFile=$(compgen -W "$hledgerFile") optarg+=("$(compgen -W "${COMP_WORDS[i + offset]}")")
;; fi
--rules-file) done
if [[ ${COMP_WORDS[i+1]} == '=' ]]; then ((i += offset))
hledgerRulesFile=${COMP_WORDS[i+2]} done
else }
hledgerRulesFile=${COMP_WORDS[i+1]}
fi # Get ledger file from -f --file arguments from COMP_WORDS and pass it to the
hledgerRulesFile=$(compgen -W "$hledgerRulesFile") # 'hledger' call. Note that --rules-file - if present - must also be passed!
;; # Multiple files are allowed so pass them all in the order of appearance.
esac _hledger() {
local hledgerArgs=("$@")
local file
local -a optarg
_hledger_optarg -f --file
for file in "${optarg[@]}"; do
[[ -f $file ]] && hledgerArgs+=(--file "$file")
done done
[[ -f $hledgerFile ]] && hledgerArgs+=(--file "$hledgerFile") _hledger_optarg --rules-file
[[ -f $hledgerRulesFile ]] && hledgerArgs+=(--rules-file "$hledgerRulesFile") for file in "${optarg[@]}"; do
[[ -f $file ]] && hledgerArgs+=(--rules-file "$file")
done
# Discard errors. Is there a way to validate files before using them? # Discard errors. Is there a way to validate files before using them?
hledger "${hledgerArgs[@]}" 2>/dev/null hledger "${hledgerArgs[@]}" 2>/dev/null