Last active
September 28, 2024 13:29
-
-
Save rcmachado/af3db315e31383502660 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
.SILENT: | |
.PHONY: help | |
# Based on https://gist.github.com/prwhite/8168133#comment-1313022 | |
## This help screen | |
help: | |
printf "Available targets\n\n" | |
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 "%-15s %s\n", helpCommand, helpMessage; \ | |
} \ | |
} \ | |
{ lastLine = $$0 }' $(MAKEFILE_LIST) | |
## Another dummy task | |
another-dummy-task: | |
echo dummy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improvements:
output
The below simpler version uses command
column
instead of the trickygrep | wc -L
but:':'
in HelpMsgoutput
Let's complicate a bit the above
Makefile
using$$'\t'
and colorize first column usinggrep --color
.The
grep
color can be controlled by the environment variableGREP_COLORS
.Try
GREP_COLORS='mt=1;41;37' make help
or be inspired from many color posibilities.(You can embed the
GREP_COLORS
value within theMakefile
)output