Merge pull request #1167 from sirosen/no-args-help-command
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add no_args_is_help to click.Command
authored by David Lord · committed by GitHub · parents 68ecafd, 02ba36f · Browse files · 3 files·+23 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+23 added−1 removed
- tests:+10 added−0 removed
- other:+13 added−1 removed
Provenance
- Commit
43eb54d- Tree
5db8e51- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
68ecafd- Parent 2
02ba36f
3 files changed+23 added−1 removed
modified:click/core.py+10 added-1 removed
783 unmodified lines784 :param short_help: the short help to use for this command. This is785 shown on the command listing of the parent command.786 :param add_help_option: by default each command registers a ``--help``787 option. This can be disabled by this parameter.788+ :param no_args_is_help: this controls what happens if no arguments are789+ provided. This option is disabled by default.790+ If enabled this will add ``--help`` as argument791+ if no arguments are passed792 :param hidden: hide this command from help outputs.793 794 :param deprecated: issues a message indicating that795 the command is deprecated.1 unmodified line797 798 def __init__(self, name, context_settings=None, callback=None,799 params=None, help=None, epilog=None, short_help=None,800 options_metavar='[OPTIONS]', add_help_option=True,797- hidden=False, deprecated=False):801+ no_args_is_help=False, hidden=False, deprecated=False):802 BaseCommand.__init__(self, name, context_settings)803 #: the callback to execute when the command fires. This might be804 #: `None` in which case nothing happens.805 self.callback = callback9 unmodified lines815 self.epilog = epilog816 self.options_metavar = options_metavar817 self.short_help = short_help818 self.add_help_option = add_help_option819+ self.no_args_is_help = no_args_is_help820 self.hidden = hidden821 self.deprecated = deprecated822 823 def get_usage(self, ctx):112 unmodified lines936 with formatter.indentation():937 formatter.write_text(self.epilog)938 939 def parse_args(self, ctx, args):940+ if not args and self.no_args_is_help and not ctx.resilient_parsing:941+ echo(ctx.get_help(), color=ctx.color)942+ ctx.exit()943+944 parser = self.make_parser(ctx)945 opts, args, param_order = parser.parse_args(args=args)946 947 for param in iter_params_for_processing(918 unmodified linesmodified:tests/test_commands.py+10 added
66 unmodified lines67 r'special-chars\s+Login and store the token in ~/.netrc\.\s*',68 result.output) is not None69 70 71+def test_no_args_is_help(runner):72+ @click.command(no_args_is_help=True)73+ def cli():74+ pass75+76+ result = runner.invoke(cli, [])77+ assert result.exit_code == 078+ assert 'Show this message and exit.' in result.output79+80+81 def test_default_maps(runner):82 @click.group()83 def cli():84 pass209 unmodified linesmodified:CHANGES.rst+3 added
5 unmodified lines6 -----------7 8 Unreleased9 10+- Add ``no_args_is_help`` option to ``click.Command``, defaults to False (`#1167`_)11 12+.. _#1167: https://github.com/pallets/click/pull/116713+14 Version 7.015 -----------16 17 Released 2018-09-25627 unmodified lines