Skip to content

Commit

Permalink
Merge pull request cookiecutter#1705 from italomaia/patch-1
Browse files Browse the repository at this point in the history
Updated: Boolean Variables documentation and docstrings
  • Loading branch information
insspb committed Jun 7, 2022
2 parents f39f120 + 6217392 commit 4406bc7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Expand Up @@ -72,6 +72,7 @@ jobs:
- name: Project security test
run: "nox -p ${{ matrix.python }} -s safety_tests"
- name: Send coverage report to codeclimate
continue-on-error: true
uses: paambaati/codeclimate-action@v3.0.0
with:
coverageCommand: echo "Ignore rerun"
Expand Down
14 changes: 7 additions & 7 deletions cookiecutter/prompt.py
Expand Up @@ -16,20 +16,23 @@ def read_user_variable(var_name, default_value):
:param str var_name: Variable of the context to query the user
:param default_value: Value that will be returned if no input happens
"""
# Please see https://click.palletsprojects.com/en/7.x/api/#click.prompt
return click.prompt(var_name, default=default_value)


def read_user_yes_no(question, default_value):
"""Prompt the user to reply with 'yes' or 'no' (or equivalent values).
Note:
Possible choices are 'true', '1', 'yes', 'y' or 'false', '0', 'no', 'n'
- These input values will be converted to ``True``:
"1", "true", "t", "yes", "y", "on"
- These input values will be converted to ``False``:
"0", "false", "f", "no", "n", "off"
Actual parsing done by :func:`click.prompt`; Check this function codebase change in
case of unexpected behaviour.
:param str question: Question to the user
:param default_value: Value that will be returned if no input happens
"""
# Please see https://click.palletsprojects.com/en/7.x/api/#click.prompt
return click.prompt(question, default=default_value, type=click.BOOL)


Expand All @@ -38,7 +41,6 @@ def read_repo_password(question):
:param str question: Question to the user
"""
# Please see https://click.palletsprojects.com/en/7.x/api/#click.prompt
return click.prompt(question, hide_input=True)


Expand All @@ -51,7 +53,6 @@ def read_user_choice(var_name, options):
:param list options: Sequence of options that are available to select from
:return: Exactly one item of ``options`` that has been chosen by the user
"""
# Please see https://click.palletsprojects.com/en/7.x/api/#click.prompt
if not isinstance(options, list):
raise TypeError

Expand Down Expand Up @@ -109,7 +110,6 @@ def read_user_dict(var_name, default_value):
:param default_value: Value that will be returned if no input is provided
:return: A Python dictionary to use in the context.
"""
# Please see https://click.palletsprojects.com/en/7.x/api/#click.prompt
if not isinstance(default_value, dict):
raise TypeError

Expand Down
27 changes: 17 additions & 10 deletions docs/advanced/boolean_variables.rst
@@ -1,29 +1,35 @@
.. _boolean-variables:
Boolean Variables
-----------------

Boolean Variables (2.2+)
------------------------
.. versionadded:: 2.2.0

Boolean variables are used for answering True/False questions.

Basic Usage
~~~~~~~~~~~

Boolean variables are regular key / value pairs, but with the value being True/False.
Boolean variables are regular key / value pairs, but with the value being
``True``/``False``.

For example, if you provide the following boolean variable in your ``cookiecutter.json``::
For example, if you provide the following boolean variable in your
``cookiecutter.json``::

{
"run_as_docker": true
}

you'd get the following user input when running Cookiecutter::
you will get the following user input when running Cookiecutter::

run_as_docker [True]:

Depending on the user's input, a different condition will be selected.
User input will be parsed by :func:`~cookiecutter.prompt.read_user_yes_no`. The
following values are considered as valid user input:

The above ``run_as_docker`` boolean variable creates ``cookiecutter.run_as_docker``, which
can be used like this::
- ``True`` values: "1", "true", "t", "yes", "y", "on"
- ``False`` values: "0", "false", "f", "no", "n", "off"

The above ``run_as_docker`` boolean variable creates ``cookiecutter.run_as_docker``,
which can be used like this::

{%- if cookiecutter.run_as_docker -%}
# In case of True add your content here
Expand All @@ -33,7 +39,8 @@ can be used like this::

{% endif %}

Cookiecutter is using `Jinja2's if conditional expression <http://jinja.pocoo.org/docs/dev/templates/#if>`_ to determine the correct ``run_as_docker``.
Cookiecutter is using `Jinja2's if conditional expression <https://jinja.palletsprojects
.com/en/latest/templates/#if>`_ to determine the correct ``run_as_docker``.

Input Validation
~~~~~~~~~~~~~~~~
Expand Down
6 changes: 5 additions & 1 deletion docs/conf.py
Expand Up @@ -353,7 +353,11 @@


# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {'https://docs.python.org/3': None}
intersphinx_mapping = {
"python": ("https://docs.python.org/3", None),
"requests": ("https://requests.readthedocs.io/en/latest/", None),
"click": ("https://click.palletsprojects.com/en/latest", None),
}
myst_enable_extensions = [
"tasklist",
"strikethrough",
Expand Down
10 changes: 8 additions & 2 deletions noxfile.py
Expand Up @@ -55,7 +55,7 @@ def docs(session, batch_run: bool = False):
"""Build the documentation or serve documentation interactively."""
shutil.rmtree(Path("docs").joinpath("_build"), ignore_errors=True)
session.install("-r", "docs/requirements.txt")
session.install(".")
session.install("-e", ".")
session.cd("docs")
sphinx_args = ["-b", "html", "-W", ".", "_build/html"]

Expand All @@ -69,7 +69,13 @@ def docs(session, batch_run: bool = False):
"--port",
"9812",
"--watch",
"../",
"../*.md",
"--watch",
"../*.rst",
"--watch",
"../*.py",
"--watch",
"../cookiecutter",
]
)

Expand Down

0 comments on commit 4406bc7

Please sign in to comment.