Skip to content
Myr

Merge pull request #557 from Nudies/master

Before adoption: this commit came in with the history the project adopted, so no landing records it.

issue #393: Usage error should show there is a --help option

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents 2899795, ee780c3 · Browse files · 3 files+75 added−2 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+75 added−2 removed

  • tests:+67 added−0 removed
  • other:+8 added−2 removed

3 files+75 added−2 removed

modified:click/exceptions.py+7 added-1 removed
42 unmodified lines
4344 def __init__(self, message, ctx=None):45 ClickException.__init__(self, message)46 self.ctx = ctx47 self.cmd = self.ctx and self.ctx.command or None4849 def show(self, file=None):50 if file is None:51 file = get_text_stderr()52 color = None53 hint = ''54 if (self.cmd is not None and55 self.cmd.get_help_option(self.ctx) is not None):56 hint = ('Try "%s %s" for help.\n'57 % (self.ctx.command_path, self.ctx.help_option_names[0]))58 if self.ctx is not None:59 color = self.ctx.color54 echo(self.ctx.get_usage() + '\n', file=file, color=color)60 echo(self.ctx.get_usage() + '\n%s' % hint, file=file, color=color)61 echo('Error: %s' % self.format_message(), file=file, color=color)626364class BadParameter(UsageError):
153 unmodified lines
modified:docs/quickstart.rst+1 added-1 removed
77 unmodified lines
78And if you want to go back to the real world, use the following command::7980 $ deactivate8182After doing this, the prompt of your shell should be as familar as before.82After doing this, the prompt of your shell should be as familiar as before.8384Now, let's move on. Enter the following command to get Click activated in your85virtualenv::86
188 unmodified lines
modified:tests/test_formatting.py+67 added
143 unmodified lines
144 '',145 'Options:',146 ' --help Show this message and exit.',147 ]148149150def test_formatting_usage_error(runner):151 @click.command()152 @click.argument('arg')153 def cmd(arg):154 click.echo('arg:' + arg)155156 result = runner.invoke(cmd, [])157 assert result.exit_code == 2158 assert result.output.splitlines() == [159 'Usage: cmd [OPTIONS] ARG',160 'Try "cmd --help" for help.',161 '',162 'Error: Missing argument "arg".'163 ]164165166def test_formatting_usage_error_nested(runner):167 @click.group()168 def cmd():169 pass170171 @cmd.command()172 @click.argument('bar')173 def foo(bar):174 click.echo('foo:' + bar)175176 result = runner.invoke(cmd, ['foo'])177 assert result.exit_code == 2178 assert result.output.splitlines() == [179 'Usage: cmd foo [OPTIONS] BAR',180 'Try "cmd foo --help" for help.',181 '',182 'Error: Missing argument "bar".'183 ]184185186def test_formatting_usage_error_no_help(runner):187 @click.command(add_help_option=False)188 @click.argument('arg')189 def cmd(arg):190 click.echo('arg:' + arg)191192 result = runner.invoke(cmd, [])193 assert result.exit_code == 2194 assert result.output.splitlines() == [195 'Usage: cmd [OPTIONS] ARG',196 '',197 'Error: Missing argument "arg".'198 ]199200201def test_formatting_usage_custom_help(runner):202 @click.command(context_settings=dict(help_option_names=['--man']))203 @click.argument('arg')204 def cmd(arg):205 click.echo('arg:' + arg)206207 result = runner.invoke(cmd, [])208 assert result.exit_code == 2209 assert result.output.splitlines() == [210 'Usage: cmd [OPTIONS] ARG',211 'Try "cmd --man" for help.',212 '',213 'Error: Missing argument "arg".'214 ]