Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?

Use a help target in your Makefile

In certainly one of my earlier blog. I wrote how you would make your life simpler if you begin utilizing Makefile. However if you begin utilizing Makefile in lots of initiatives. The targets that you just use could range from mission to mission.

On this weblog submit I’ll deal with a trick how you would write a assist goal. Lets think about that we have now a mission that let you construct, begin and cease a docker container. These names are fairly simple. However with out wanting within the Makefile you by no means know for positive.

By calling the assist goal you would record all out there targets:

make assist
Enter fullscreen mode

Exit fullscreen mode

If you take a look on the Makefile used for this weblog submit. It appears to be like like this:

.DEFAULT_GOAL:=assist
.PHONY: assist
assist:  ## Show this assist
    $(information Instance Makefile for my weblog submit)
    awk 'BEGIN {FS = ":.*##"; printf "nUsage:n  make 33[36m<target>33[0mn"} /^[a-zA-Z0-9_-]+:.*?##/ { printf "  33[36m%-15s33[0m %sn", $$1, $$2 } /^##@/ { printf "n33[1m%s33[0mn", substr($$0, 5) } ' $(MAKEFILE_LIST)

.PHONY: build
build: ## Build the container image
    docker build -t my-container:latest .

.PHONY: stop
stop: ## Stop the container
    docker stop my-named-container > /dev/null 2>&1 || True
    docker rm my-named-container > /dev/null 2>&1 || True

.PHONY: start
start: stop ## Start the container
    docker run --name my-named-container my-container:latest > /dev/null

.PHONY: shell
shell: ## Start a shell session on the container
    docker run -it my-named-container:latest bash

.PHONY: logs
logs: ## Tail the logs of the running container
    docker logs my-named-container -f

$(VERBOSE).SILENT:
Enter fullscreen mode

Exit fullscreen mode

You need to add the help target. And for each target you need to supply a help text. And each target needs a prefix with ##.

By adding a help target to your Makefile you make it easier to use for others. But also for yourself because you don’t need to remember each target in the Makefile.

Photo by lalesh aldarwish

Add a Comment

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?