Move back commands/options parsing to separate shell scripts

It made the Makefile more difficult to read and this way we can
take advantage of `set -e`, `-o pipefail` and friends.

Clean up debugging targets
This commit is contained in:
Vladimir Zhelezov 2020-12-18 10:09:27 +01:00
parent effb1be0e6
commit 5c2dd6fa2f
3 changed files with 32 additions and 38 deletions

View File

@ -20,28 +20,8 @@ endif
DESTDIR ?=
# 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
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
PARSE_COMMANDS := ./parse-commands.sh
PARSE_OPTIONS := ./parse-options.sh
EXTENSIONS := ui web api
INSTALLED_EXTENSIONS := $(foreach EXT,$(EXTENSIONS),$(shell type hledger-$(EXT) >/dev/null 2>&1 && echo $(EXT)))
@ -87,25 +67,12 @@ commands-list.txt:
printf "%s," $(COMMANDS) | sed 's/,$$//' > $@
generic-options.txt:
hledger -h | $(PARSE_OPTIONS) > $@
$(PARSE_OPTIONS) > $@
options-%.txt:
hledger $* -h | $(PARSE_OPTIONS) > $@
$(PARSE_OPTIONS) $* > $@
.PHONY: clean
clean:
rm -f $(COMMANDS_TMP) commands*.txt generic-options.txt options-*.txt
rm -f commands*.txt generic-options.txt options-*.txt
rm -f hledger-completion.bash
# Basic REGEX debugging targets. Example usage:
# diff <(make -s debug-options) <(make -s debug-options CMD=reg)
.PHONY: debug-commands
debug-commands:
$(PARSE_COMMANDS)
.PHONY: debug-options
debug-options: CMD :=
debug-options:
hledger $(CMD) -h | $(PARSE_OPTIONS)

View File

@ -0,0 +1,13 @@
#!/usr/bin/env 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.
set -euo pipefail
declare commands_help
commands_help=$(hledger)
{
sed -rn 's/^\s+([a-z][-a-z]+)\s+.*/\1/p' <<< "$commands_help"
sed -rn 's/^\s+[a-z][-a-z]+\s+\(([a-z][ ,a-z]+)\).*/\1/p' <<< "$commands_help" |
sed 's/\s*,\s*/\n/g' |
sed '/^.$/d'
} | sed '/^hledger/d' | sort -u

View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
# 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 (=)
set -euo pipefail
declare subcommand=${1:-}
declare hledgerArgs=(--help)
[[ -n $subcommand ]] && hledgerArgs=("$subcommand" "${hledgerArgs[@]}")
hledger "${hledgerArgs[@]}" |
sed -rn '/^\s+-/p' |
sed -rn 's/^\s{1,4}(-.)?\s{1,4}(--[a-zA-Z][-_a-zA-Z0-9]+=?).*/\2/p' |
sort -u