Skip to content

Commit

Permalink
Add support to invoke args as string
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Menegazzo committed Oct 5, 2016
1 parent 702acf7 commit 0759c87
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Version 7.0
- `secho`'s first argument can now be `None`, like in `echo`.
- Usage errors now hint at the `--help` option.
- ``launch`` now works properly under Cygwin. See #650.
- `CliRunner.invoke` now may receive `args` as a string representing
a Unix shell command. See #664.

Version 6.7
-----------
Expand Down
11 changes: 9 additions & 2 deletions click/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import shutil
import tempfile
import contextlib
import shlex

from ._compat import iteritems, PY2
from ._compat import iteritems, PY2, string_types


# If someone wants to vendor click, we want to ensure the
Expand Down Expand Up @@ -260,7 +261,10 @@ def invoke(self, cli, args=None, input=None, env=None,
The ``color`` parameter was added.
:param cli: the command to invoke
:param args: the arguments to invoke
:param args: the arguments to invoke. It may be given as an iterable
or a string. When given as string it will be interpreted
as a Unix shell command. More details at
:func:`shlex.split`.
:param input: the input data for `sys.stdin`.
:param env: the environment overrides.
:param catch_exceptions: Whether to catch any other exceptions than
Expand All @@ -274,6 +278,9 @@ def invoke(self, cli, args=None, input=None, env=None,
exception = None
exit_code = 0

if isinstance(args, string_types):
args = shlex.split(args)

try:
cli.main(args=args or (),
prog_name=self.get_default_prog_name(cli), **extra)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,23 @@ def cli_env():
assert result.output == 'ENV=some_value\n'

assert os.environ == env_orig


@pytest.mark.parametrize('args, expected_output', [
(None, 'bar\n'),
([], 'bar\n'),
('', 'bar\n'),
(['--foo', 'one two'], 'one two\n'),
('--foo "one two"', 'one two\n'),
])
def test_args(args, expected_output):

@click.command()
@click.option('--foo', default='bar')
def cli_args(foo):
click.echo(foo)

runner = CliRunner()
result = runner.invoke(cli_args, args=args)
assert result.exit_code == 0
assert result.output == expected_output

0 comments on commit 0759c87

Please sign in to comment.