Merge pull request #1 from mitsuhiko/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
update
authored by Jayin Ton · committed by Jayin Ton · parents 308cc08, 6a03b34 · Browse files · 9 files·+124 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+124 added−17 removed
- tests:+74 added−0 removed
- other:+50 added−17 removed
Provenance
- Commit
fc01cc8- Tree
474efb7- Author
Jayin Ton <273942569@qq.com>- Committer
Jayin Ton <273942569@qq.com>- Parent 1
308cc08- Parent 2
6a03b34
9 files changed+124 added−17 removed
modified:click/core.py+4 added-1 removed
1507 unmodified lines1508 1509 help = self.help or ''1510 extra = []1511 if self.default is not None and self.show_default:1512- extra.append('default: %s' % self.default)1512+ extra.append('default: %s' % (1513+ ', '.join('%s' % d for d in self.default)1514+ if isinstance(self.default, (list, tuple))1515+ else self.default, ))1516 if self.required:1517 extra.append('required')1518 if extra:1519 help = '%s[%s]' % (help and help + ' ' or '', '; '.join(extra))113 unmodified linesmodified:click/decorators.py+17 added-9 removed
132 unmodified lines133 f.__click_params__.append(param)134 135 136 def argument(*param_decls, **attrs):137- """Attaches an option to the command. All positional arguments are137+ """Attaches an argument to the command. All positional arguments are138 passed as parameter declarations to :class:`Argument`; all keyword139- arguments are forwarded unchanged. This is equivalent to creating an140- :class:`Option` instance manually and attaching it to the141- :attr:`Command.params` list.139+ arguments are forwarded unchanged (except ``cls``).140+ This is equivalent to creating an :class:`Argument` instance manually141+ and attaching it to the :attr:`Command.params` list.142+143+ :param cls: the argument class to instantiate. This defaults to144+ :class:`Argument`.145 """146 def decorator(f):147+ ArgumentClass = attrs.pop('cls', Argument)144- _param_memo(f, Argument(param_decls, **attrs))148+ _param_memo(f, ArgumentClass(param_decls, **attrs))149 return f150 return decorator151 152 153 def option(*param_decls, **attrs):154 """Attaches an option to the command. All positional arguments are155 passed as parameter declarations to :class:`Option`; all keyword152- arguments are forwarded unchanged. This is equivalent to creating an153- :class:`Option` instance manually and attaching it to the154- :attr:`Command.params` list.156+ arguments are forwarded unchanged (except ``cls``).157+ This is equivalent to creating an :class:`Option` instance manually158+ and attaching it to the :attr:`Command.params` list.159+160+ :param cls: the option class to instantiate. This defaults to161+ :class:`Option`.162 """163 def decorator(f):164 if 'help' in attrs:165 attrs['help'] = inspect.cleandoc(attrs['help'])166+ OptionClass = attrs.pop('cls', Option)159- _param_memo(f, Option(param_decls, **attrs))167+ _param_memo(f, OptionClass(param_decls, **attrs))168 return f169 return decorator170 171 130 unmodified linesmodified:click/termui.py+1 added-1 removed
295 unmodified lines296 return297 # If we're on Windows and we don't have colorama available, then we298 # clear the screen by shelling out. Otherwise we can use an escape299 # sequence.300- if WIN and colorama is None:300+ if WIN:301 os.system('cls')302 else:303 sys.stdout.write('\033[2J\033[1;1H')304 214 unmodified linesmodified:click/testing.py+22 added-3 removed
133 unmodified lines134 rv.update(overrides)135 return rv136 137 @contextlib.contextmanager138- def isolation(self, input=None, env=None):138+ def isolation(self, input=None, env=None, color=False):139 """A context manager that sets up the isolation for invoking of a140 command line tool. This sets up stdin with the given input data141 and `os.environ` with the overrides from the given dictionary.142 This also rebinds some internals in Click to be mocked (like the143 prompt functionality).144 145 This is automatically done in the :meth:`invoke` method.146 147+ .. versionadded:: 4.0148+ The ``color`` parameter was added.149+150 :param input: the input stream to put into sys.stdin.151 :param env: the environment overrides as dictionary.152+ :param color: whether the output should contain color codes. The153+ application can still override this explicitly.154 """155 input = make_input_stream(input, self.charset)156 157 old_stdin = sys.stdin34 unmodified lines192 sys.stdout.write(char)193 sys.stdout.flush()194 return char195 196+ default_color = color197+ def should_strip_ansi(stream=None, color=None):198+ if color is None:199+ return not default_color200+ return not color201+202 old_visible_prompt_func = clickpkg.termui.visible_prompt_func203 old_hidden_prompt_func = clickpkg.termui.hidden_prompt_func204 old__getchar_func = clickpkg.termui._getchar205+ old_should_strip_ansi = clickpkg.utils.should_strip_ansi206 clickpkg.termui.visible_prompt_func = visible_input207 clickpkg.termui.hidden_prompt_func = hidden_input208 clickpkg.termui._getchar = _getchar209+ clickpkg.utils.should_strip_ansi = should_strip_ansi210 211 old_env = {}212 try:213 for key, value in iteritems(env):20 unmodified lines234 sys.stdin = old_stdin235 clickpkg.termui.visible_prompt_func = old_visible_prompt_func236 clickpkg.termui.hidden_prompt_func = old_hidden_prompt_func237 clickpkg.termui._getchar = old__getchar_func238+ clickpkg.utils.should_strip_ansi = old_should_strip_ansi239 240 def invoke(self, cli, args=None, input=None, env=None,227- catch_exceptions=True, **extra):241+ catch_exceptions=True, color=False, **extra):242 """Invokes a command in an isolated environment. The arguments are243 forwarded directly to the command line script, the `extra` keyword244 arguments are passed to the :meth:`~clickpkg.Command.main` function of245 the command.6 unmodified lines252 .. versionchanged:: 3.0253 The result object now has an `exc_info` attribute with the254 traceback if available.255 256+ .. versionadded:: 4.0257+ The ``color`` parameter was added.258+259 :param cli: the command to invoke260 :param args: the arguments to invoke261 :param input: the input data for `sys.stdin`.262 :param env: the environment overrides.263 :param catch_exceptions: Whether to catch any other exceptions than264 ``SystemExit``.265 :param extra: the keyword arguments to pass to :meth:`main`.266+ :param color: whether the output should contain color codes. The267+ application can still override this explicitly.268 """269 exc_info = None251- with self.isolation(input=input, env=env) as out:270+ with self.isolation(input=input, env=env, color=color) as out:271 exception = None272 exit_code = 0273 274 try:39 unmodified linesmodified:docs/commands.rst+2 added-2 removed
133 unmodified lines134 135 The :meth:`Context.invoke` command will automatically invoke the function136 in the correct way, so the function will either be called with ``f(ctx,137 obj)`` or ``f(obj)`` depending on whether or not it itself is decorated with138-:func:`with_context`.138+:func:`pass_context`.139 140-This is a very powerful context that can be used to build very complex140+This is a very powerful concept that can be used to build very complex141 nested applications; see :ref:`complex-guide` for more information.142 143 144 Group Invocation Without Command408 unmodified linesmodified:docs/python3.rst+1 added-1 removed
131 unmodified lines132 133 export LC_ALL=de_DE.utf-8134 export LANG=de_DE.utf-8135 136-If you are on a US machine, ``en_EN.utf-8`` is the encoding of choice. On136+If you are on a US machine, ``en_US.utf-8`` is the encoding of choice. On137 some newer Linux systems, you could also try ``C.UTF-8`` as the locale::138 139 export LC_ALL=C.UTF-8140 export LANG=C.UTF-814 unmodified linesmodified:tests/test_options.py+47 added
113 unmodified lines114 assert not result.exception115 assert result.output == 'foo|bar\n'116 117 118+def test_multiple_default_help(runner):119+ @click.command()120+ @click.option("--arg1", multiple=True, default=('foo', 'bar'),121+ show_default=True)122+ @click.option("--arg2", multiple=True, default=(1, 2), type=int,123+ show_default=True)124+ def cmd(arg, arg2):125+ pass126+127+ result = runner.invoke(cmd, ['--help'])128+ assert not result.exception129+ assert "foo, bar" in result.output130+ assert "1, 2" in result.output131+132+133 def test_nargs_envvar(runner):134 @click.command()135 @click.option('--arg', nargs=2)136 def cmd(arg):94 unmodified lines231 out = result.output.splitlines()232 assert ' --foo TEXT hello' in out233 assert ' i am' in out234 assert ' multiline' in out235+236+237+def test_argument_custom_class(runner):238+ class CustomArgument(click.Argument):239+ def get_default(self, ctx):240+ '''a dumb override of a default value for testing'''241+ return 'I am a default'242+243+ @click.command()244+ @click.argument('testarg', cls=CustomArgument, default='you wont see me')245+ def cmd(testarg):246+ click.echo(testarg)247+248+ result = runner.invoke(cmd)249+ assert 'I am a default' in result.output250+ assert 'you wont see me' not in result.output251+252+253+def test_option_custom_class(runner):254+ class CustomOption(click.Option):255+ def get_help_record(self, ctx):256+ '''a dumb override of a help text for testing'''257+ return ('--help', 'I am a help text')258+259+ @click.command()260+ @click.option('--testoption', cls=CustomOption, help='you wont see me')261+ def cmd(testoption):262+ click.echo(testoption)263+264+ result = runner.invoke(cmd, ['--help'])265+ assert 'I am a help text' in result.output266+ assert 'you wont see me' not in result.outputmodified:tests/test_testing.py+27 added
111 unmodified lines112 CustomError = SystemExit113 114 result = runner.invoke(cli)115 assert result.exit_code == 1116+117+118+def test_with_color():119+ @click.command()120+ def cli():121+ click.secho('hello world', fg='blue')122+123+ runner = CliRunner()124+125+ result = runner.invoke(cli)126+ assert result.output == 'hello world\n'127+ assert not result.exception128+129+ result = runner.invoke(cli, color=True)130+ assert result.output == click.style('hello world', fg='blue') + '\n'131+ assert not result.exception132+133+134+def test_with_color_but_pause_not_blocking():135+ @click.command()136+ def cli():137+ click.pause()138+139+ runner = CliRunner()140+ result = runner.invoke(cli, color=True)141+ assert not result.exception142+ assert result.output == ''modified:CHANGES+3 added
20 unmodified lines21 - Added `max_content_width` parameter to the context which can be used to22 change the maximum width of help output. By default Click will not format23 content for more than 80 characters width.24 - Added support for writing prompts to stderr.25+- Fix a bug when showing the default for multiple arguments.26+- Added support for custom subclasses to `option` and `argument`.27+- Fix bug in ``clear()`` on Windows when colorama is installed.28 29 Version 3.330 -----------31 176 unmodified lines