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-tests
Displaying 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
awk
as a single commandOr, the non-minified version in the form of a shell script
help.sh
Makefile
Explanation:
c0
,c1
,c2
are initialized using-v variable=value
, which are later used for colourizing terminal output textBEGIN
block is executed once, at the start, we use it to print the headerAvailable targets:
awk
processes 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 ifh
is 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):