Using and Mastering Cookiecutter Transcripts
Chapter: Creating Cookiecutter templates
Lecture: Concept: Required values
0:01 Blank defaults make a lot of sense,
0:03 sometimes you just don't want to have a value unless they type it in,
0:06 like you have no idea what the default could be,
0:09 or maybe you want to have a blank one and indicate you must enter this,
0:12 there is no choice but to type here something
0:14 that makes sense for your project.
0:17 So let's look at an example.
0:19 We come here and we run this cookiecutter template,
0:22 maybe it's going to come along and ask your name and your favorite color,
0:25 alright, well how do we make this like a required value.
0:28 Well, if we just wanted blank with no defaults,
0:31 and it's going to be empty unless they put something in,
0:34 this is fine, all we have to do is have our json file have empty value
0:38 and you have an empty non default, but what it really means is
0:42 the default is an empty string, so it still could go a little bit wrong.
0:46 We saw that we tried to have some sort of keyword here
0:50 like hey let's try required and of course, we put required over there,
0:53 but that doesn't really do anything, so we saw that it will just use required.
0:56 What is required to make this keyword work or something like that is
1:00 writing a little bit of Python code in what is called a pre generation hook,
1:05 we're going to talk about hooks at the end of this chapter,
1:08 but a pre generation hook is code that runs after the data is collected here,
1:12 but before it actually executes the process that creates the directories,
1:17 copies the files, does the replacement, and you'll get a look
1:19 at what is called the context, and this context is all the values,
1:22 the keys and the values, basically listed in cookiecutter.json,
1:26 so we could say if the value favorite color is still required,
1:29 or if it doesn't seem like some kind of color we could say no, no,
1:33 we're not going to run, we're going to cancel this,
1:36 tell you you need to enter a favorite color,
1:38 it's not amazing, but that's what it is.