from typing import Optional
from typing import Callable
import click


def commit_option(default: Optional[bool]) -> Callable:
	"""
	Decorator to add the ``--commit / --no-commit`` option to a click command.

	.. versionadded:: 0.5.0

	:param default: Whether to commit automatically.

	* :py:obj:`None` -- Ask first
	* :py:obj:`True` -- Commit automatically
	* :py:obj:`False` -- Don't commit
	"""

	if default is True:
		default_text = "Commit automatically"
	elif default is False:
		default_text = "Don't commit"
	else:
		default_text = "Ask first"

	return click.option(
			"-y/-n",
			"--commit/--no-commit",
			default=lambda: default,
			show_default=default_text,
			help="Commit or do not commit any changed files.",
			)