Makefile 之 help信息生成小技巧

警告
本文最后更新于 2023-03-09,文中内容可能已过时。

参考《go语言项目开发实战》 孔令飞

根据注释(##)自动生成帮助信息。

1
2
3
4
5
.PHONY: help
help: Makefile
        @echo "\nUsage: make <TARGETS> <OPTIONS> ...\n\nTargets:"
        @sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
        @echo "$$USAGE_OPTIONS"

小demo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## build: Build source code for host platform.
.PHONY: build
build:
        @echo "this is command 1"
        
## build.multiarch: Build source code for multiple platforms. See option PLATFORM
.PHONY: build.multiarch
build.multiarch:
        @echo "this is command 2"
## image: Build docker images for host arch.
.PHONY: image
image:
        @echo "this is command 3"

## push: Build docker images for host arch and push images to registry.
.PHONY: push
push:
        @echo "this is command 4"

## ca: Generate CA files for all iam components.
.PHONY: ca
ca:
        @echo "this is command 5"

## help: Show this help info.
.PHONY: help
help: Makefile
        @echo "\nUsage: make <TARGETS> <OPTIONS> ...\n\nTargets:"
        @sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
        @echo "$$USAGE_OPTIONS"

效果:

image-20220516091906058
image-20220516091906058

Buy Me a Coffee ~~
hiifong 支付宝支付宝
hiifong 微信微信
0%