Move commands and options parsing into the Makefile
It is just a pipe of sed regex filters and all can be viewed/edited in one place. Add a couple of debug targets to see easily the effects of regex changes.
This commit is contained in:
		
							parent
							
								
									0498b8ff7c
								
							
						
					
					
						commit
						1b6f968a6a
					
				| @ -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) | ||||
|  | ||||
| @ -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 "$@" | ||||
| @ -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 "$@" | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user