Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'no_args_is_help' to Command #502

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cache/v/cache/lastfailed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

This comment was marked as off-topic.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ build
docs/_build
click.egg-info
.tox
.idea
13 changes: 12 additions & 1 deletion click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,11 +737,16 @@ class Command(BaseCommand):
shown on the command listing of the parent command.
:param add_help_option: by default each command registers a ``--help``
option. This can be disabled by this parameter.
:param no_args_is_help: this controls what happens if no arguments are
provided. If enabled this will add
``--help`` as argument if no arguments are
passed.

"""

def __init__(self, name, context_settings=None, callback=None,
params=None, help=None, epilog=None, short_help=None,
options_metavar='[OPTIONS]', add_help_option=True):
options_metavar='[OPTIONS]', add_help_option=True, no_args_is_help=None):
BaseCommand.__init__(self, name, context_settings)
#: the callback to execute when the command fires. This might be
#: `None` in which case nothing happens.
Expand All @@ -757,6 +762,7 @@ def __init__(self, name, context_settings=None, callback=None,
short_help = make_default_short_help(help)
self.short_help = short_help
self.add_help_option = add_help_option
self.no_args_is_help = no_args_is_help

def get_usage(self, ctx):
formatter = ctx.make_formatter()
Expand Down Expand Up @@ -866,6 +872,11 @@ def format_epilog(self, ctx, formatter):
formatter.write_text(self.epilog)

def parse_args(self, ctx, args):

if not args and self.no_args_is_help and not ctx.resilient_parsing:
echo(ctx.get_help(), color=ctx.color)
ctx.exit()

parser = self.make_parser(ctx)
opts, args, param_order = parser.parse_args(args=args)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,19 @@ def cli(verbose, args):
'Verbosity: 4',
'Args: -foo|-x|--muhaha|x|y|-x',
]


def test_no_args_is_help_off(runner):
@click.command()
def cli():
"""This is the help."""
result = runner.invoke(cli)
assert 'This is the help.' not in result.output


def test_no_args_is_help_on(runner):
@click.command(no_args_is_help=True)
def cli():
"""This is the help."""
result = runner.invoke(cli)
assert 'This is the help.' in result.output