0

I have a target in the makefile called 'data' (taken from cookiecutter) and I am trying to pass an argument to this target which is calling a file like this:

## Make Dataset
data: requirements
    $(PYTHON_INTERPRETER) src/data/make_dataset.py

I want to pass some arguments to this make target so they can be captured by the make_dataset.py file and used in main.

The part I tried in make_dataset.py looks like:

@click.command()
@click.option('--cg', default='scope', help= "some help")
def main(cg):

Can someone help me to solve this issue? I want to use this make target like:

make data --cg=xyz
bazinga
  • 2,120
  • 4
  • 21
  • 35

2 Answers2

3

This is not how make works. What you ask is typically done by declaring a variable in the Makefile which you can then override from the command line.

CGFLAGS=

## Make Dataset
data: requirements
    $(PYTHON_INTERPRETER) src/data/make_dataset.py $(CGFLAGS)

Now if you pass in make CGFLAGS='--cg=xyz' data the value of the variable you specified in the command line will explicitly be interpolated into the data recipe.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

You could do something like:

CG:=
CG_OPT:=$(addprefix --cg=,$(CG))

data: requirements
    $(PYTHON_INTERPRETER) src/data/make_dataset.py $(CG_OPT)

And then do:

make data CG=xyz

Notice that $(CG_OPT) will be blank if you do not specify a CG=... on the make command line. Unfortunately using variable names that start with -- can confuse things, though it can be done as well:

 --cg:=
 data: requirements
    $(PYTHON_INTERPRETER) src/data/make_dataset.py $(addprefix,--cg=,$(--cg))

But then, to prevent make from interpreting the --cg as an internal make option, you have to add a -- option before it on the command line as follows:

 make data -- --cg=xyz

Note: this all assumes you want to do a specific variable name --cg=. If you wanted it to work for any variable name, you would have to do something similar to what @tripleee suggested

HardcoreHenry
  • 5,909
  • 2
  • 19
  • 44