Created
November 27, 2024 13:00
-
-
Save hermannolafs/ae911d2a2cd5d38b35ec90d1ac8f0764 to your computer and use it in GitHub Desktop.
AWK Makefile help message,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.DEFAULT_GOAL := help | |
.PHONY: help | |
help: ## This help message | |
@awk -F '[, ]+' '/^[a-zA-Z0-9_-]+:.*##/ { \ | |
target = $$0; \ | |
sub(/^[ \t]*/, "", target); \ | |
sub(/:.*##/, "##", target); \ | |
split(target, parts, "##"); \ | |
gsub(/^[ \t]*|[ \t]*$$/, "", parts[1]); \ | |
gsub(/^[ \t]*|[ \t]*$$/, "", parts[2]); \ | |
printf "%-30s %s\n", parts[1], parts[2]; \ | |
}' $(MAKEFILE_LIST) | |
create-venv: ## Create python virtual environment at .venv | |
python3.12 -m venv .venv |
Here's another implementation (by claude-sonnet-3.7-thinking
)
help: ## Display this help message
@echo "Usage:"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | grep -v "\.PHONY" | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-30s\033[0m %s\n", $$1, $$2}'
Furthermore, I asked it which was better:
My target is generally better because:
- It's more standard and widely used in Makefiles
- It has colored output which improves readability
- It has a cleaner implementation with less complex string manipulation
- It includes a helpful "Usage:" header
The only advantage Hermano's approach might have is supporting numeric characters in target names, but this is rarely needed and the standard help implementation covers most use cases more elegantly.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is adopted from this article: https://diamantidis.github.io/tips/2020/07/01/list-makefile-targets
I thought this could probably be solved with my favorite language, AWK, so I did.