3 ms·
I prefer a help rule in the Makefile, like so: help: @grep -P '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {prin
by 0xmohit 10y ago
I prefer a help rule in the Makefile, like so:
help:
@grep -P '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
Assuming that the trimmed Makefile read:
build: ## Build the binary
clean: ## Clean the binary and intermediate files
distclean: clean ## Clean dependencies, intermediate files and binary
deps: ## Install dependencies
all: deps build ## Get dependencies and build binary
release: distclean all ## distclean + all
Now issuing make help would display:
all Get dependencies and build binary
build Build the binary
clean Clean the binary and intermediate files
deps Install dependencies
distclean Clean dependencies, intermediate files and binary
release distclean + all
- jloughry 10y agoLovely! I'm going to start using this in all my Makefiles. Seriously, thanks. You solved a problem I had.
- urda 10y agoFirst off I wanted to say this is amazing and I'm going to roll it into my current and future Makefiles. Second, if you're on OS X the `-P` flag is going to have problems [1]. Works fine if you just replace `grep -P` with something like `ack`! [1] http://stackoverflow.com/q/16658333/169153 http://stackoverflow.com/q/16658333/169153
- spinlock 10y agoThis is great. I think `help` commands are fantastic things to add to projects right from the start. It really helps the next person to get up to speed.
- jklowden 10y agoSimplified: (no color, no grep) help: @awk -F ':.*##' '$$0 ~ FS \ {printf "%15s%s\n", $$1 ":", $$2}' \ $(MAKEFILE_LIST) | sort
- jloughry 10y agoIf your grep is like mine you may need to add "-h" to the "-P" option to get the desired output.