summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--click/parser.py5
-rw-r--r--tests/test_arguments.py23
2 files changed, 26 insertions, 2 deletions
diff --git a/click/parser.py b/click/parser.py
index c102917..c8c9172 100644
--- a/click/parser.py
+++ b/click/parser.py
@@ -180,15 +180,16 @@ class OptionParser(object):
def _process_args_for_options(self, state):
while state.rargs:
arg = state.rargs[0]
+ arglen = len(arg)
# Double dash es always handled explicitly regardless of what
# prefixes are valid.
if arg == '--':
del state.rargs[0]
return
- elif arg[0:2] in self._opt_prefixes:
+ elif arg[:2] in self._opt_prefixes and arglen > 2:
# process a single long option (possibly with value(s))
self._process_long_opt(state)
- elif arg[:1] in self._opt_prefixes and len(arg) > 1:
+ elif arg[:1] in self._opt_prefixes and arglen > 1:
# process a cluster of short options (possibly with
# value(s) for the last one only)
self._process_short_opts(state)
diff --git a/tests/test_arguments.py b/tests/test_arguments.py
index c8d33e1..6040621 100644
--- a/tests/test_arguments.py
+++ b/tests/test_arguments.py
@@ -47,3 +47,26 @@ def test_nargs_err(runner):
result = runner.invoke(copy, ['foo', 'bar'])
assert result.exit_code == 2
assert 'Got unexpected extra argument (bar)' in result.output
+
+
+def test_file_args(runner):
+ @click.command()
+ @click.argument('input', type=click.File('rb'))
+ @click.argument('output', type=click.File('wb'))
+ def inout(input, output):
+ while True:
+ chunk = input.read(1024)
+ if not chunk:
+ break
+ output.write(chunk)
+
+ with runner.isolated_filesystem():
+ result = runner.invoke(inout, ['-', 'hello.txt'], input='Hey!')
+ assert result.output == ''
+ assert result.exit_code == 0
+ with open('hello.txt', 'rb') as f:
+ assert f.read() == b'Hey!'
+
+ result = runner.invoke(inout, ['hello.txt', '-'])
+ assert result.output == 'Hey!'
+ assert result.exit_code == 0