1

If I have variables in cookiecutter.json like so:

{
    "app": {
        "name": "myapp",
        "description": "my app description",
        "version": "v1",
        "ecr_data_classification_tag": "non-pii"
    },
    "contact": {
        "team": "monitors",
        "name": "WS Monitors",
        "url": "http://myurl.tld/"
    },
    "etc": "foo"
}

How do I override the app name when invoking cookiecutter?

I've tried supplying the "extra context" this way:

$ cookiecutter --no-input my-template app.name="my-new-name"

and:

$ cookiecutter --no-input my-template app='{"name":"my-new-name"}'

but the former does nothing and the latter just replaces the entire app parameter with the string {\"name\":\"my-new-name\"} (it doesn't parse the JSON).

I tried dumping the whole thing into a yaml file, and that works:

default_context:
  app:
    name: my-new-name

but that's not ideal. It also wipes out all the other app parameters.

Is there a way to invoke cookiecutter from the command-line and just override app.name?

1 Answers1

0

It turns out that:

  1. No, the "extra context" is just k/v pairs, and the key and value are not parsed at all
  2. As of 2.1.1, when the value is swapped in to override the config, the entire value is swapped, so for dictionaries/objects, there's no way to replace just an attribute of the value, you have to replace the entire value.
  3. cksak made a change to walk the dictionaries and only swap out the specified values, but that's in main and hasn't been released yet

So, for now, if you want to override dictionary values you have no choice but to create a separate yaml (or json) file and load that via --config-file, and until 2.1.2 is released, you will have to make sure you override all of the attributes of each dictionary unless you want them wiped out.