refactor other decorators to match version_option
Before adoption: this commit came in with the history the project adopted, so no landing records it.
option() returns a decorator directly rewrite docs
authored by David Lord · committed by David Lord · parent 77ca520 · Browse files · 1 file·+52 added−62 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+52 added−62 removed
- other:+52 added−62 removed
Provenance
- Commit
a077ed7- Tree
cedf1ab- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
77ca520
1 file changed+52 added−62 removed
modified:src/click/decorators.py+52 added-62 removed
190 unmodified lines191 192 return decorator193 194 195-def confirmation_option(*param_decls, **attrs):196- """Shortcut for confirmation prompts that can be ignored by passing197- ``--yes`` as parameter.195+def confirmation_option(*param_decls, **kwargs):196+ """Add a ``--yes`` option which shows a prompt before continuing if197+ not passed. If the prompt is declined, the program will exit.198 199- This is equivalent to decorating a function with :func:`option` with200- the following parameters::201-202- def callback(ctx, param, value):203- if not value:204- ctx.abort()205-206- @click.command()207- @click.option('--yes', is_flag=True, callback=callback,208- expose_value=False, prompt='Do you want to continue?')209- def dropdb():210- pass199+ :param param_decls: One or more option names. Defaults to the single200+ value ``"--yes"``.201+ :param kwargs: Extra arguments are passed to :func:`option`.202 """203 213- def decorator(f):214- def callback(ctx, param, value):215- if not value:216- ctx.abort()204+ def callback(ctx, param, value):205+ if not value:206+ ctx.abort()207 218- attrs.setdefault("is_flag", True)219- attrs.setdefault("callback", callback)220- attrs.setdefault("expose_value", False)221- attrs.setdefault("prompt", "Do you want to continue?")222- attrs.setdefault("help", "Confirm the action without prompting.")223- return option(*(param_decls or ("--yes",)), **attrs)(f)208+ if not param_decls:209+ param_decls = ("--yes",)210 225- return decorator211+ kwargs.setdefault("is_flag", True)212+ kwargs.setdefault("callback", callback)213+ kwargs.setdefault("expose_value", False)214+ kwargs.setdefault("prompt", "Do you want to continue?")215+ kwargs.setdefault("help", "Confirm the action without prompting.")216+ return option(*param_decls, **kwargs)217 218 228-def password_option(*param_decls, **attrs):229- """Shortcut for password prompts.219+def password_option(*param_decls, **kwargs):220+ """Add a ``--password`` option which prompts for a password, hiding221+ input and asking to enter the value again for confirmation.222 231- This is equivalent to decorating a function with :func:`option` with232- the following parameters::233-234- @click.command()235- @click.option('--password', prompt=True, confirmation_prompt=True,236- hide_input=True)237- def changeadmin(password):238- pass223+ :param param_decls: One or more option names. Defaults to the single224+ value ``"--password"``.225+ :param kwargs: Extra arguments are passed to :func:`option`.226 """227+ if not param_decls:228+ param_decls = ("--password",)229 241- def decorator(f):242- attrs.setdefault("prompt", True)243- attrs.setdefault("confirmation_prompt", True)244- attrs.setdefault("hide_input", True)245- return option(*(param_decls or ("--password",)), **attrs)(f)230+ kwargs.setdefault("prompt", True)231+ kwargs.setdefault("confirmation_prompt", True)232+ kwargs.setdefault("hide_input", True)233+ return option(*param_decls, **kwargs)234 247- return decorator235 249-236 def version_option(237 version=None,238 *param_decls,239 package_name=None,103 unmodified lines343 kwargs["callback"] = callback344 return option(*param_decls, **kwargs)345 346 361-def help_option(*param_decls, **attrs):362- """Adds a ``--help`` option which immediately ends the program363- printing out the help page. This is usually unnecessary to add as364- this is added by default to all commands unless suppressed.347+def help_option(*param_decls, **kwargs):348+ """Add a ``--help`` option which immediately prints the help page349+ and exits the program.350 366- Like :func:`version_option`, this is implemented as eager option that367- prints in the callback and exits.351+ This is usually unnecessary, as the ``--help`` option is added to352+ each command automatically unless ``add_help_option=False`` is353+ passed.354 369- All arguments are forwarded to :func:`option`.355+ :param param_decls: One or more option names. Defaults to the single356+ value ``"--help"``.357+ :param kwargs: Extra arguments are passed to :func:`option`.358 """359 372- def decorator(f):373- def callback(ctx, param, value):374- if value and not ctx.resilient_parsing:375- echo(ctx.get_help(), color=ctx.color)376- ctx.exit()360+ def callback(ctx, param, value):361+ if not value or ctx.resilient_parsing:362+ return363 378- attrs.setdefault("is_flag", True)379- attrs.setdefault("expose_value", False)380- attrs.setdefault("help", "Show this message and exit.")381- attrs.setdefault("is_eager", True)382- attrs["callback"] = callback383- return option(*(param_decls or ("--help",)), **attrs)(f)364+ echo(ctx.get_help(), color=ctx.color)365+ ctx.exit()366 385- return decorator367+ if not param_decls:368+ param_decls = ("--help",)369+370+ kwargs.setdefault("is_flag", True)371+ kwargs.setdefault("expose_value", False)372+ kwargs.setdefault("is_eager", True)373+ kwargs.setdefault("help", "Show this message and exit.")374+ kwargs["callback"] = callback375+ return option(*param_decls, **kwargs)