Skip to content

Commit

Permalink
Merge pull request #557 from Nudies/master
Browse files Browse the repository at this point in the history
issue #393: Usage error should show there is a --help option
  • Loading branch information
untitaker committed Apr 15, 2016
2 parents 2899795 + ee780c3 commit 42e2821
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
8 changes: 7 additions & 1 deletion click/exceptions.py
Expand Up @@ -44,14 +44,20 @@ class UsageError(ClickException):
def __init__(self, message, ctx=None):
ClickException.__init__(self, message)
self.ctx = ctx
self.cmd = self.ctx and self.ctx.command or None

def show(self, file=None):
if file is None:
file = get_text_stderr()
color = None
hint = ''
if (self.cmd is not None and
self.cmd.get_help_option(self.ctx) is not None):
hint = ('Try "%s %s" for help.\n'
% (self.ctx.command_path, self.ctx.help_option_names[0]))
if self.ctx is not None:
color = self.ctx.color
echo(self.ctx.get_usage() + '\n', file=file, color=color)
echo(self.ctx.get_usage() + '\n%s' % hint, file=file, color=color)
echo('Error: %s' % self.format_message(), file=file, color=color)


Expand Down
2 changes: 1 addition & 1 deletion docs/quickstart.rst
Expand Up @@ -79,7 +79,7 @@ And if you want to go back to the real world, use the following command::

$ deactivate

After doing this, the prompt of your shell should be as familar as before.
After doing this, the prompt of your shell should be as familiar as before.

Now, let's move on. Enter the following command to get Click activated in your
virtualenv::
Expand Down
67 changes: 67 additions & 0 deletions tests/test_formatting.py
Expand Up @@ -145,3 +145,70 @@ def cli():
'Options:',
' --help Show this message and exit.',
]


def test_formatting_usage_error(runner):
@click.command()
@click.argument('arg')
def cmd(arg):
click.echo('arg:' + arg)

result = runner.invoke(cmd, [])
assert result.exit_code == 2
assert result.output.splitlines() == [
'Usage: cmd [OPTIONS] ARG',
'Try "cmd --help" for help.',
'',
'Error: Missing argument "arg".'
]


def test_formatting_usage_error_nested(runner):
@click.group()
def cmd():
pass

@cmd.command()
@click.argument('bar')
def foo(bar):
click.echo('foo:' + bar)

result = runner.invoke(cmd, ['foo'])
assert result.exit_code == 2
assert result.output.splitlines() == [
'Usage: cmd foo [OPTIONS] BAR',
'Try "cmd foo --help" for help.',
'',
'Error: Missing argument "bar".'
]


def test_formatting_usage_error_no_help(runner):
@click.command(add_help_option=False)
@click.argument('arg')
def cmd(arg):
click.echo('arg:' + arg)

result = runner.invoke(cmd, [])
assert result.exit_code == 2
assert result.output.splitlines() == [
'Usage: cmd [OPTIONS] ARG',
'',
'Error: Missing argument "arg".'
]


def test_formatting_usage_custom_help(runner):
@click.command(context_settings=dict(help_option_names=['--man']))
@click.argument('arg')
def cmd(arg):
click.echo('arg:' + arg)

result = runner.invoke(cmd, [])
assert result.exit_code == 2
assert result.output.splitlines() == [
'Usage: cmd [OPTIONS] ARG',
'Try "cmd --man" for help.',
'',
'Error: Missing argument "arg".'
]

0 comments on commit 42e2821

Please sign in to comment.