Skip to content

Instantly share code, notes, and snippets.

@onjin
Created July 5, 2024 09:04
Show Gist options
  • Save onjin/bfd25955956038c1bc4f98f99a2da581 to your computer and use it in GitHub Desktop.
Save onjin/bfd25955956038c1bc4f98f99a2da581 to your computer and use it in GitHub Desktop.
Makefile help generator
.DEFAULT_GOAL := help
# use '# >>> Build commands' to create section
# use '# target: target description' to create help for target
# example output from this file:
# $ make
# Usage:
#
# >>> Section 1
# - option1 help for option 1
# - option2 help for option 2
#
# >>> Section 2
# - option3 help for option 3
#
.PHONY: help
help:
@echo "Usage:"
@cat $(MAKEFILE_LIST) | grep -E '^# >>>|^# [A-Za-z0-9_.-]+:' | sed -E 's/^# //' | awk ' \
BEGIN { \
green="\033[32m"; \
yellow="\033[33m"; \
reset="\033[0m"; \
section=""; \
} \
/^>>>/ { \
section=substr($$0, 5); \
printf "\n" green ">>> %s" reset "\n", section; \
next; \
} \
/^([A-Za-z0-9_.-]+):/ { \
target=$$1; \
gsub(/:$$/, "", target); \
description=substr($$0, index($$0, ":") + 2); \
if (description == "") { description="-"; } \
printf " - " yellow "%-35s" reset " %s\n", target, description; \
} \
'
# >>> Section 1
# option1: help for option 1
# option2: help for option 2
# >>> Section 2
# option3: help for option 3
option3:
@echo 'option3'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment