Skip to content
Merged
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
8 changes: 7 additions & 1 deletion click/exceptions.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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".'
]