Skip to content

Instantly share code, notes, and snippets.

@sjparkinson
Created August 26, 2016 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjparkinson/f0413d429b12877ecb087c6fc30c1f0a to your computer and use it in GitHub Desktop.
Save sjparkinson/f0413d429b12877ecb087c6fc30c1f0a to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link
Author

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