0

Cookiecutter.json has a parameter which accepts "yes" or "no":

{ "test" : [ "yes", "no"] }

If a user selects "yes" then it should continue accepting the inputs further, if not it should stop. The same logic is included in pre_gen_project.py under the hooks folder.

try:
    if "{{ cookiecutter.test }}" == "yes":
        cookiecutter.prompt.read_user_variable("full_name","your_synthetic_test_name")

    else:
       sys.exit(0)
except Exception as ex:
    print("Exception in pre_gen_project script")

Problem here is, it is entering the else condition, but the cookie cutter execution is not stopping here. Any suggestion, Let me know about it. Thank you.

Pooja G
  • 41
  • 1
  • 14
  • `"{{ cookiecutter.test }}" == "yes"` This will never be true. They are 2 completely different strings. I'm not sure what you expected to happen there. – matszwecja May 09 '23 at 11:31
  • @matszwecja , Here i am expecting it to further take the input variables from user , say if my further input is to take name, age etc. then it has to take it user gives yes , or successfully end if he says no – Pooja G May 09 '23 at 11:59
  • No, I mean specifically the `if` condition. `{{ cookiecutter.test }}` is just a piece of text that very clearly is different than `yes`. – matszwecja May 09 '23 at 12:01
  • For me it works . Because i give "yes" when i execute this cookiecutter and that is how the data will have "yes" . I am able to validate by printing some print stmt inside it. – Pooja G May 09 '23 at 12:02
  • Whenever i try to create the project out of this cookiecutter, Select test: 1 - yes 2 - no Choose from 1, 2 [1]: 2 i m in else condition:::: Unable to create directory '..\..\my-package\src\cookiecutter-package\{{cookiecutter.age_name}}' Error message: 'collections.OrderedDict object' has no attribute 'age_name' . It is trying to fetch some other data which i do not want it to fetch. – Pooja G May 09 '23 at 12:09

1 Answers1

0

I think the check that you're doing is wrong. Ideally you should check whether the values that the user parses includes in the allowed set of values you've mentioned in the json

First you need to read the json file:

with open('Cookiecutter.json', 'r') as file:
    json_data = json.load(file)

then check whether the value the user selects exist in the set of values in the json

# userpassed_val could be either yes or no or some other random val
if userpassed_val in json_data['test']:
    cookiecutter.prompt.read_user_variable("full_name","your_synthetic_test_name")
else:
    # value does not exist under test key in the json
    sys.exit(0)
Kulasangar
  • 9,046
  • 5
  • 51
  • 82