Skip to content

Instantly share code, notes, and snippets.

@PaulMarisOUMary
Created November 20, 2023 16:40
Show Gist options
  • Save PaulMarisOUMary/b14b2f9c523c360a366762eec9905fb7 to your computer and use it in GitHub Desktop.
Save PaulMarisOUMary/b14b2f9c523c360a366762eec9905fb7 to your computer and use it in GitHub Desktop.
Makefile: use arguments in the command line + help command
TARGETS := $(shell grep -E '^[a-zA-Z0-9 -]+:' $(MAKEFILE_LIST) | sort | awk -F ':.*' '{print $$1}')
ARGS = $(wordlist 2, $(words $(MAKECMDGOALS)), $(MAKECMDGOALS))
.DEFAULT_GOAL := help
help: # Show this help
@grep -E '^[a-zA-Z0-9 -]+:.*#' Makefile | sort | while read -r l; do printf "\033[1;32m$$(echo $$l | cut -f 1 -d':')\033[00m:$$(echo $$l | cut -f 2- -d'#')\n"; done
%: # Allow to pass arguments to 'make' commands without errors and show help if the command is not found
@if [ -z "$(filter $(firstword $(MAKECMDGOALS)),$(TARGETS))" ]; then \
echo "Help: Unknown command '$(firstword $(MAKECMDGOALS))'"; \
$(MAKE) --no-print-directory help; \
exit 1; \
fi
run: # Run anything here
@echo "Arguments:" $(ARGS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment