Skip to content

Instantly share code, notes, and snippets.

@sjparkinson sjparkinson/Makefile
Created Aug 26, 2016

Embed
What would you like to do?
Automatic help target using ## comments.
help: ## Show this help message.
echo "usage: make [target] ..."
echo ""
echo "targets:"
fgrep --no-filename "##" ${MAKEFILE_LIST} | head -n '-1' | column -s ':#' -t -c 2
@sjparkinson

This comment has been minimized.

Copy link
Owner Author

commented Aug 26, 2016

For example:

clean: ## Clean up the build.
    rm -rf build/*

Would output the following when calling make help:

usage: make [target] ...

targets:
clean        Clean up the build.
help         Show this help message.

How it Works

1. fgrep --no-filename "##" ${MAKEFILE_LIST}

${MAKEFILE_LIST} is a special variable that contains a list of the Makefile file names. From this we use fgrep to find all lines containing ## in the Makefiles.

2. head -n '-1'

Removes the last line of output, which is the fgrep one.

3. column -s ':#' -t -c 2

Pretty prints out the targets into columns, removing the colons and comment symbols.

@sjparkinson

This comment has been minimized.

Copy link
Owner Author

commented Aug 26, 2016

And the original source of inspiration https://gist.github.com/prwhite/8168133.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.