English 中文(简体)
如何从指挥线向马克茨提出论据?
原标题:How to pass argument to Makefile from command line?

如何从指挥线向马克茨提出论据?

I understand I can do

$ make action VAR="value"
$ value

www.un.org/spanish/ecosoc

VAR = "default"
action:
    @echo $(VAR)

我如何得到以下行为?

$ make action value
value

如何

$make action value1 value2
value1 value2
最佳回答

你也许不这样做;你重开如何发挥作用的基本模式。 但这里的是:

action:
        @echo action $(filter-out $@,$(MAKECMDGOALS))

%:      # thanks to chakrit
    @:    # thanks to William Pursell

EDIT:
To explain the first command,

$(MAKECMDGOALS) is the list of “targets” specified on the control line, e.g. “action Value1 Value2”.

automatic,用于该规则目标的名称,此处为“action”。

<代码>filter-out是一种功能,从名单中删除一些内容。 ∗∗∗∗∗∗∗∗∗∗ (它可能更微妙,但在此我们不需要微妙。)

这些数字加在一起,$ (filter-out$@,$ (MAKECMDGOALS)回到除“action”外的指挥线上列明的目标清单,该清单可能“价值1” 。

问题回答

Here is a generic working solution based on @Beta s

I m using GNUMak 4.1 with SHELL=/bin/bash atop myMakfile, so YMMV!

This allows us to accept extra arguments (by doing nothing when we get a job that doesn t match, rather than throwing an error).

%:
    @:

这是一项给我们带来动力的宏观:

args = `arg="$(filter-out $@,$(MAKECMDGOALS))" && echo $${arg:-${1}}`

Here is a job which might call this one:

test:
    @echo $(call args,defaultstring)

结果是:

$ make test
defaultstring
$ make test hi
hi

Note! You might be better off using a "Taskfile", which is a bash pattern that works similarly to make, only without the nuances of Maketools. See https://github.com/adriancooney/Taskfile

更为容易。 考虑一项任务:

provision:
        ansible-playbook -vvvv 
        -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory 
        --private-key=.vagrant/machines/default/virtualbox/private_key 
        --start-at-task="$(AT)" 
        -u vagrant playbook.yml

我现在要说的话,我只是说:

<条码>AT=“造型资产”

或者说:

<编码>make provision in this case AT is an food string

You will be better of defining variables and calling your make instead of using parameters:

文件

action: ## My action helper
    @echo $$VAR_NAME

终端站

> VAR_NAME="Hello World" make action
Hello World




相关问题
Command Line Parameters

I m trying to add my file in Visual Studio as command line parameters. I know my code works since when I use fopen("whole path here", "r"), it runs. I then add the file as a command line parameter ...

Read from File, or STDIN

I ve written a command line utility that uses getopt for parsing arguments given on the command line. I would also like to have a filename be an optional argument, such as it is in other utilities ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

热门标签