'Cookiecutter - choices with a custom entry
I am working with the cookicutter package and I would like to know if there is any way to have something like this:
#cookiecutter.json
{
"name": "default name",
"folder_name": ["foldername1", "foldername2", custom-created-folder-name]
}
So, then when creating the repo, I would pick option 3 and be prompted to enter my own folder_name. I tried looking through the docs, but I could not find any reference to this.
Solution 1:[1]
You don't have a really nice way to do this, as you always should show the prompt for a custom folder but you can do something like this:
# cookiecutter.json
{
"folder_name": ["foldername1", "foldername2", "other"],
"custom_folder_name": ""
}
# your template folder
{% if not cookiecutter.folder_name == "other" %}{{cookiecutter.folder_name}}{% else %}{{cookiecutter.custom_folder_name}}{% endif %}
Here is what happens:
$ cookiecutter template
Select folder_name:
1 - foldername1
2 - foldername2
3 - other
Choose from 1, 2, 3 (1, 2, 3) [1]: 1
custom_folder_name []:
$ ls
foldername1 template
$ cookiecutter template
Select folder_name:
1 - foldername1
2 - foldername2
3 - other
Choose from 1, 2, 3 (1, 2, 3) [1]: 3
custom_folder_name []:
Error: "." directory already exists
$ cookiecutter template
Select folder_name:
1 - foldername1
2 - foldername2
3 - other
Choose from 1, 2, 3 (1, 2, 3) [1]: 3
custom_folder_name []: custom_folder
$ ls
custom_folder foldername1 template
$ cookiecutter template
Select folder_name:
1 - foldername1
2 - foldername2
3 - other
Choose from 1, 2, 3 (1, 2, 3) [1]: 2
custom_folder_name []: not_used
$ ls
custom_folder foldername1 foldername2 template
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | wankata |
