-
-
Save benrowe/167f65131cb16942fb5032a9b29937f3 to your computer and use it in GitHub Desktop.
Add a help target to a Makefile that will allow all targets to be self documenting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY: help | |
# COLORS | |
GREEN := $(shell tput -Txterm setaf 2) | |
YELLOW := $(shell tput -Txterm setaf 3) | |
WHITE := $(shell tput -Txterm setaf 7) | |
RESET := $(shell tput -Txterm sgr0) | |
TARGET_MAX_CHAR_NUM=20 | |
## Show help | |
help: | |
@echo '' | |
@echo 'Usage:' | |
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}' | |
@echo '' | |
@echo 'Targets:' | |
@awk '/^[a-zA-Z\-\_0-9]+:/ { \ | |
helpMessage = match(lastLine, /^## (.*)/); \ | |
if (helpMessage) { \ | |
helpCommand = substr($$1, 0, index($$1, ":")-1); \ | |
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \ | |
printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \ | |
} \ | |
} \ | |
{ lastLine = $$0 }' $(MAKEFILE_LIST) | |
## example | |
target-name: | |
@echo "target-name" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This. I like this.