Permalink
Browse files

More tests for flag-counting.

  • Loading branch information...
1 parent 54da01e commit b041a4f598737b20d4c5c13d165c34478cf57479 @keleshev keleshev committed Jul 29, 2012
Showing with 62 additions and 6 deletions.
  1. +6 −6 docopt.py
  2. +55 −0 language_agnostic_test/language_agnostic_tester.py
  3. +1 −0 test_docopt.py
View
12 docopt.py
@@ -187,14 +187,14 @@ def match(self, left, collected=None):
left_.append(l)
if not match:
return False, left, collected
- if type(self.value) is not int:
- return True, left_, collected + match
same_name = [a for a in collected if a.name == self.name]
- if len(same_name):
- same_name[0].value += 1#match[0].value
+ if type(self.value) is int:
+ if not same_name:
+ match[0].value = 1
+ return True, left_, collected + match
+ same_name[0].value += 1
return True, left_, collected
- else:
- return True, left_, collected + match
+ return True, left_, collected + match
@property
def name(self):
View
55 language_agnostic_test/language_agnostic_tester.py
@@ -597,6 +597,61 @@
$ prog --a
"user-error" # not a unique prefix
+#
+# Counting number of flags
+#
+
+r"""Usage: prog -v
+
+"""
+$ prog -v
+{"-v": true}
+
+r"""Usage: prog [-v -v]
+
+"""
+$ prog
+{"-v": 0}
+
+$ prog -v
+{"-v": 1}
+
+$ prog -vv
+{"-v": 2}
+
+r"""Usage: prog -v ...
+
+"""
+$ prog
+"user-error"
+
+$ prog -v
+{"-v": 1}
+
+$ prog -vv
+{"-v": 2}
+
+$ prog -vvvvvv
+{"-v": 6}
+
+r"""Usage: prog [-v | -vv | -vvv]
+
+This one is probably most readable user-friednly variant.
+
+"""
+$ prog
+{"-v": 0}
+
+$ prog -v
+{"-v": 1}
+
+$ prog -vv
+{"-v": 2}
+
+$ prog -vvvv
+"user-error"
+
+
'''
import sys, json, re
from subprocess import Popen, PIPE, STDOUT
View
1 test_docopt.py
@@ -521,6 +521,7 @@ def test_count_multiple_flags():
assert docopt('usage: prog [-vv]', '-vv') == {'-v': 2}
with raises(DocoptExit):
assert docopt('usage: prog [-vv]', '-vvv')
+ assert docopt('usage: prog [-v | -vv | -vvv]', '-vvv') == {'-v': 3}
#assert docopt('usage: prog [go]', 'go') == {'go': True}
#assert docopt('usage: prog [go go]', '') == {'go': 0}

0 comments on commit b041a4f

Please sign in to comment.