diff --git a/shell-completion/Makefile b/shell-completion/Makefile index b983ec2f4..d821ec213 100644 --- a/shell-completion/Makefile +++ b/shell-completion/Makefile @@ -4,9 +4,32 @@ # get parallel jobs. MAKEFLAGS += --jobs=$(shell nproc 2>/dev/null || printf 8) -all: hledger-completion.bash +# Parse hledger's help and output all commands and command aliases in +# parenthesis. Do not output single letter command aliases, it's not useful. +COMMANDS_TMP := commands.tmp -COMMANDS := $(sort $(shell hledger | ./output-commands.sh | grep -v ^hledger | sort -u) ui web api) +define PARSE_COMMANDS := +hledger > $(COMMANDS_TMP) ; +{ \ + sed -rn 's/^\s+([a-z][-a-z]+)\s+.*/\1/p' $(COMMANDS_TMP) ; \ + sed -rn 's/^\s+[a-z][-a-z]+\s+\(([a-z][ ,a-z]+)\).*/\1/p' $(COMMANDS_TMP) | \ + sed 's/\s*,\s*/\n/g' | \ + sed '/^.$$/d' ; \ +} | sed '/^hledger/d' | sort -u +endef + +# Parse hledger's help and output long options. Do not propose single letter +# completions. Options requiring an argument make that explicit by appending the +# equal sign (=) +define PARSE_OPTIONS := +sed -rn '/^\s+-/p' | \ +sed -rn 's/^\s{1,4}(-.)?\s{1,4}(--[a-zA-Z][-_a-zA-Z0-9]+=?).*/\2/p' | \ +sort -u +endef + +EXTENSIONS := ui web api + +COMMANDS := $(sort $(shell $(PARSE_COMMANDS)) $(EXTENSIONS)) CMDOPTFILES := $(foreach CMD,$(COMMANDS),options-$(CMD).txt) define M4DEPS := @@ -19,27 +42,35 @@ generic-options.txt \ $(CMDOPTFILES) endef +all: hledger-completion.bash + hledger-completion.bash: $(M4DEPS) m4 hledger-completion.bash.m4 > $@ -generic-options.txt: - hledger -h | ./output-options.sh | sort -u > $@ - commands.txt: printf "%s\n" $(COMMANDS) > $@ commands-list.txt: printf "%s," $(COMMANDS) | sed 's/,$$//' > $@ -#query-filters.txt: - # The query filters are hard to extract! - # hledger help --cat hledger | sed -n '/^QUERIES/,/^[A-Z]/p' +generic-options.txt: + hledger -h | $(PARSE_OPTIONS) > $@ options-%.txt: - hledger $* -h | ./output-options.sh $* | sort -u > $@ + hledger $* -h | $(PARSE_OPTIONS) > $@ .PHONY: clean clean: - rm -f commands*.txt generic-options.txt options-*.txt + rm -f commands*.{txt,tmp} generic-options.txt options-*.txt rm -f hledger-completion.bash - rm -rf _{commands,options}.tmp + + +# Basic REGEX debugging targets +.PHONY: debug-commands debug-options + +debug-commands: + $(PARSE_COMMANDS) + +debug-options: CMD := +debug-options: + hledger $(CMD) -h | $(PARSE_OPTIONS) diff --git a/shell-completion/output-commands.sh b/shell-completion/output-commands.sh deleted file mode 100755 index ae1b67987..000000000 --- a/shell-completion/output-commands.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash -# Output subcommands from man/usage text - -set -o errexit -o pipefail -o nounset - -main() { - declare tmp="_commands.tmp" - cat > "$tmp" - - sed -rn 's/^\s+([a-z][-a-z]+)\s+.*/\1/p' "$tmp" - - # Output command aliases in parenthesis: - # Do not output single letter command aliases, it's not useful. - sed -rn 's/^\s+[a-z][-a-z]+\s+\(([a-z][ ,a-z]+)\).*/\1/p' "$tmp" | - sed 's/\s*,\s*/\n/g' | - sed '/^.$/d' -} - -main "$@" diff --git a/shell-completion/output-options.sh b/shell-completion/output-options.sh deleted file mode 100755 index fbc9e5471..000000000 --- a/shell-completion/output-options.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash -# Output short and long options from man/usage text - -set -o errexit -o pipefail -o nounset - -main() { - declare tmpdir="_options.tmp" - declare tmp="${tmpdir}/${1:-generic}" - - mkdir -p "$tmpdir" - cat > "$tmp" - - # Do not propose single letter completions. It's not useful, it's noisy - # and it makes completion slower: - # Display all 200 possibilities? (y or n) - # sed -rn 's/.* (-[a-zA-Z0-9]).*/\1/gp' < "$tmp" - - # Options requiring an argument make that explicit by appending - # the equal sign (=) - sed -rn '/^\s+-/p' "$tmp" | - sed -rn 's/^\s{1,4}(-.)?\s{1,4}(--[a-zA-Z][-_a-zA-Z0-9]+=?).*/\2/p' -} - -main "$@"