From 84eaf20f9ba3e34f03507b0a3e8fd7165a0e35ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Sch=C3=B6ttl?= Date: Sat, 12 Jan 2019 12:30:06 +0100 Subject: [PATCH] Add bash completion script (an artifact) --- shell-completion/hledger-completion.bash | 1940 ++++++++++++++++++++++ 1 file changed, 1940 insertions(+) create mode 100644 shell-completion/hledger-completion.bash diff --git a/shell-completion/hledger-completion.bash b/shell-completion/hledger-completion.bash new file mode 100644 index 000000000..9d7f3f512 --- /dev/null +++ b/shell-completion/hledger-completion.bash @@ -0,0 +1,1940 @@ + +# Completion script for hledger. +# Created using a Makefile and real hledger. + +# No set -e because this file is sourced and is not supposed to quit the current shell. +set -o pipefail + +# TODO grep "^$wordToComplete" is (functional) not safe to use if the word +# contains regex special chars. But it might be no problem because of +# COMP_WORDBREAKS. + +# Working with bash arrays is nasty compared to editing a text file. Consider +# for example grepping an array or mapping a substitution on it. +# Therefore, we create temp files in RAM for completion suggestions (see below). + +readonly HLEDGER_COMPLETION_TEMPDIR=$(mktemp -d) + +hledgerCompletionFunction() { + #declare cmd=$1 + declare wordToComplete=$2 + declare precedingWord=$3 + + declare subcommand + for subcommand in "${COMP_WORDS[@]}"; do + if grep -Fxqe "$subcommand" "$HLEDGER_COMPLETION_TEMPDIR/commands.txt"; then + COMPREPLY+=( $(grep -h "^$wordToComplete" -- "$HLEDGER_COMPLETION_TEMPDIR/options-$subcommand.txt") ) + break + fi + subcommand= + done + + if [[ -z $subcommand ]]; then + + declare completeFiles filenameSoFar + case $precedingWord in + -f|--file|--rules-file) + completeFiles=1 + filenameSoFar=$wordToComplete + ;; + =) + completeFiles=1 + filenameSoFar=$wordToComplete + ;; + esac + + if [[ -n $completeFiles ]]; then + #COMP_WORDBREAKS='= ' + declare -a files + # This does not work because assignment to 'files' in the "pipe + # subshell" has no effect! + #compgen -df | grep "^$filenameSoFar" | readarray -t files + readarray -t files < <(compgen -df | grep "^$filenameSoFar") + COMPREPLY=( "${files[@]}" ) + + else + COMPREPLY+=( $(grep -h "^$wordToComplete" -- "$HLEDGER_COMPLETION_TEMPDIR/commands.txt" "$HLEDGER_COMPLETION_TEMPDIR/generic-options.txt") ) + fi + + else + + # Almost all subcommands accept [QUERY] + # -> always add accounts to completion list + + # TODO Get ledger file from -f --file arguments from COMP_WORDS and pass it to + # the 'hledger accounts' call. Note that --rules-file - if present - must also + # be passed! + + declare -a accounts + readarray -t accounts < <(hledger accounts --flat | grep "^$wordToComplete") + COMPREPLY+=( "${accounts[@]}" ) + # Special characters (e.g. '-', ':') are allowed in account names. + # Account names with spaces must be still be quoted (e.g. '"Expens') + # for completion. Setting COMP_WORDBREAKS='' would not help here! + COMP_WORDBREAKS=' ' + + fi + +} + +# Register completion function for hledger: +complete -F hledgerCompletionFunction hledger + +# Include lists of commands and options generated by the Makefile using the +# m4 macro processor. +# Included files must have exactly one newline at EOF to prevent weired errors. + +cat < "$HLEDGER_COMPLETION_TEMPDIR/commands.txt" +add +import +balancesheet +balancesheetequity +cashflow +incomestatement +accounts +activity +balance +prices +print +register +stats +tags +ui +web +close +rewrite +api +check-dates +check-dupes +print-unique +register-match +roi +test +outlet +help +bs +bse +cf +is +a +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/generic-options.txt" +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +-h +-h +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + + + + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-add.txt" +-f +-I +-h +--no-new-accounts +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-import.txt" +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--dry-run +--new +--dry-run +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-balancesheet.txt" +-H +-N +-A +-T +-S +-O +-o +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--change +--cumulative +--historical +--flat +--drop +--no-total +--tree +--average +--row-total +--no-elide +--format +--pretty-tables +--sort-amount +--output-format +--output-file +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-balancesheetequity.txt" +-H +-N +-A +-T +-S +-O +-o +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--change +--cumulative +--historical +--flat +--drop +--no-total +--tree +--average +--row-total +--no-elide +--format +--pretty-tables +--sort-amount +--output-format +--output-file +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-cashflow.txt" +-H +-N +-A +-T +-S +-O +-o +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--change +--cumulative +--historical +--flat +--drop +--no-total +--tree +--average +--row-total +--no-elide +--format +--pretty-tables +--sort-amount +--output-format +--output-file +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-incomestatement.txt" +-H +-N +-A +-T +-S +-O +-o +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--change +--cumulative +--historical +--flat +--drop +--no-total +--tree +--average +--row-total +--no-elide +--format +--pretty-tables +--sort-amount +--output-format +--output-file +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-accounts.txt" +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--declared +--used +--tree +--flat +--drop +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-activity.txt" +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-balance.txt" +-H +-A +-T +-N +-S +-O +-o +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--change +--cumulative +--historical +--tree +--flat +--average +--row-total +--no-total +--drop +--no-elide +--format +--pretty-tables +--sort-amount +--budget +--budget +--invert +--output-format +--output-file +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-prices.txt" +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--costs +--inverted-costs +--costs +--inverted-costs +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-print.txt" +-m +-x +-O +-o +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--date2 +--match +--explicit +--new +--output-format +--output-file +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-register.txt" +-H +-A +-r +-w +-w +-O +-o +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--date2 +--cumulative +--historical +--average +--empty +--related +--width +--output-format +--output-file +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-stats.txt" +-o +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--output-file +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-tags.txt" +-f +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-ui.txt" +-F +-T +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--watch +--theme +--register +--change +--flat +--tree +--future +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-web.txt" +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--server +--host +--port +--base-url +--file-url +--capabilities +--capabilities-header +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-close.txt" +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +-R +-e +-e +-f +-f +-f +-1 +--opening +--closing +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-rewrite.txt" +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +-f +-f +-f +-f +-f +-1 +-2 +--add-posting +--auto +--add-posting +--diff +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +--add-posting +--add-posting +--add-posting +--add-posting +--add-posting +--add-posting +--add-posting +--add-posting +--add-posting +--auto +--auto +--auto +--auto +--auto +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-api.txt" +-h +-f +-d +-p +-h +--swagger +--version +--file +--static-dir +--host +--port +--version +--help +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-check-dates.txt" +-f +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--date2 +--strict +--strict +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-check-dupes.txt" +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-print-unique.txt" +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-register-match.txt" +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-roi.txt" +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--cashflow +--investment +--pnl +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-test.txt" +-h +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-outlet.txt" +-n +-h +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-help.txt" +-h +--info +--man +--pager +--cat +--help +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-bs.txt" +-H +-N +-A +-T +-S +-O +-o +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--change +--cumulative +--historical +--flat +--drop +--no-total +--tree +--average +--row-total +--no-elide +--format +--pretty-tables +--sort-amount +--output-format +--output-file +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-bse.txt" +-H +-N +-A +-T +-S +-O +-o +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--change +--cumulative +--historical +--flat +--drop +--no-total +--tree +--average +--row-total +--no-elide +--format +--pretty-tables +--sort-amount +--output-format +--output-file +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-cf.txt" +-H +-N +-A +-T +-S +-O +-o +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--change +--cumulative +--historical +--flat +--drop +--no-total +--tree +--average +--row-total +--no-elide +--format +--pretty-tables +--sort-amount +--output-format +--output-file +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-is.txt" +-H +-N +-A +-T +-S +-O +-o +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--change +--cumulative +--historical +--flat +--drop +--no-total +--tree +--average +--row-total +--no-elide +--format +--pretty-tables +--sort-amount +--output-format +--output-file +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT + +cat < "$HLEDGER_COMPLETION_TEMPDIR/options-a.txt" +-f +-I +-b +-e +-D +-W +-M +-Q +-Y +-p +-U +-C +-P +-C +-R +-N +-E +-B +-V +-h +--declared +--used +--tree +--flat +--drop +--file +--rules-file +--separator +--alias +--anon +--pivot +--ignore-assertions +--begin +--end +--daily +--weekly +--monthly +--quarterly +--yearly +--period +--date2 +--unmarked +--pending +--cleared +--real +--depth +--empty +--cost +--value +--auto +--forecast +--help +--debug +--version +TEXT +