Pooja G
Pooja G

Reputation: 51

Cookiecutter : Stop the execution of the cookiecutter if user gives no as input to one parameter

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.

Upvotes: 0

Views: 544

Answers (1)

Kulasangar
Kulasangar

Reputation: 9434

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)

Upvotes: 0

Related Questions