首页 > 解决方案 > Is it possible to use positional, rather than named, parameters in a Makefile?

问题描述

I have created a working target in my Makefile, called test-path-testname:

## Runs tests that match a file pattern and test name to speed up test time
test-path-testname: ensure-env
    docker-compose run -p 9229:9229 \
      -e TYPEORM_URL=postgres://postgres@postgres/someapp_test \
      -e DATABASE_URL_READONLY=postgres://postgres@postgres/someapp_test \
      server npm run test-all -t $(path) -- \
      --detectOpenHandles --watchAll --verbose=false -t "$(testname)" 
.PHONY: test-path-testname

It functions perfectly, using the path and testname parameters:

make path=usersArea testname="should create a new user" test-path-testname

However that command quite long - is there way I can use positional, rather than named, parameters in a Makefile?

For example, I'd like to be able to run the above with:

make usersArea "should create a new user" test-path-testname

标签: makefile

解决方案


No it is not possible because all non-options that do not contain a = are treated as targets.

Edit after your comment with the motivation explained:

You are solving an XY problem. Instead of more variables, pick apart the target name $@ with substitutions:

test-path-testname:
    @echo path=$(word 2,$(subst -, ,$@)) testname=$(word 3,$(subst -, ,$@))
    docker-compose ... -t $(word 3,$(subst -, ,$@)) ...

This assumes there are exactly two hyphens in the target name.


推荐阅读