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
Provenance
- Commit
42e2821- Tree
e0c2cfc- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
2899795- Parent 2
ee780c3
3 files changed+75 added−2 removed
modified:click/exceptions.py+7 added-1 removed
42 unmodified lines43 44 def __init__(self, message, ctx=None):45 ClickException.__init__(self, message)46 self.ctx = ctx47+ self.cmd = self.ctx and self.ctx.command or None48 49 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)62 63 64 class BadParameter(UsageError):153 unmodified linesmodified:docs/quickstart.rst+1 added-1 removed
77 unmodified lines78 And if you want to go back to the real world, use the following command::79 80 $ deactivate81 82-After doing this, the prompt of your shell should be as familar as before.82+After doing this, the prompt of your shell should be as familiar as before.83 84 Now, let's move on. Enter the following command to get Click activated in your85 virtualenv::86 188 unmodified linesmodified:tests/test_formatting.py+67 added
143 unmodified lines144 '',145 'Options:',146 ' --help Show this message and exit.',147 ]148+149+150+def test_formatting_usage_error(runner):151+ @click.command()152+ @click.argument('arg')153+ def cmd(arg):154+ click.echo('arg:' + arg)155+156+ 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+ ]164+165+166+def test_formatting_usage_error_nested(runner):167+ @click.group()168+ def cmd():169+ pass170+171+ @cmd.command()172+ @click.argument('bar')173+ def foo(bar):174+ click.echo('foo:' + bar)175+176+ 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+ ]184+185+186+def 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)191+192+ 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+ ]199+200+201+def 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)206+207+ 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+ ]