A “simple” make rule that allows pretty-printing short documentation for the
rules inside a Makefile:
Easy: simply copy everything starting at .DEFAULT_GOAL := show-help to the end
of your own Makefile (or include show-help-minified.make, and copy that file
into your project). Then document any rules by adding a single line starting
with ## immediately before the rule. E.g.:
## Run unit tests
test:
./run-testsDisplaying the documentation is done by simply executing make. This overrides
any previously set default command — you may not wish to do so; in that case,
simply remove the line that sets the .DEFAULT_GOAL. You can then display the
help via make show-help. This makes it less discoverable, of course.


A simplified version of the deno task inspired help script from @takuyahara, using only
awkas a single commandOr, the non-minified version in the form of a shell script
help.shMakefileExplanation:
c0,c1,c2are initialized using-v variable=value, which are later used for colourizing terminal output textBEGINblock is executed once, at the start, we use it to print the headerAvailable targets:awkprocesses line by line, we try to match a line that looks like rule definition, and prints the accumulated help text;the logic for processing each line is as follows:
a. For a line that begins with
##, we accumulate the help text in variableh, then jumps to process the next line (which skips over step 3c)b. For a line that doesn't begin with a tab and has a colon, this line is identified as a target; if there is any accumulated help text in variable
h, we print the target name followed by the help text, or do nothing ifhis emptyc. For any line, we clear any text stored in variable
h, any stored value is no longer needed because they are either printed in step 3b, or the accumulated help text does not belong to any targetExample output:

Generated from this Makefile (click to expand):
# See https://gist.github.com/klmr/575726c7e05d8780505a?permalink_comment_id=5676387#gistcomment-5676387 for explanation ## Print help text for Makefile help: @awk -v c0=$$(tput sgr0) -v c1=$$(tput setaf 2) -v c2=$$(tput setaf 6) 'BEGIN{print c1 "Available targets:" c0}/^## /{h=h"\n "substr($$0,4);next;}/^([^\t].*):/{if(h=="")next;sub(/:.*/,"");print"- "c2 $$0 c0 h}{h=""}' $(MAKEFILE_LIST) .PHONY: help## Make ALL the things; this includes: ## building the target; ## testing it; ## deploying to the server. all: ; @: .PHONY: all
## Run unit tests test: ; @: .PHONY: test
## Deploy to production server deploy: ; @: .PHONY: deploy
## Remove temporary build files clean: ; @: .PHONY: clean