Skip to content

Commit 5715e74

Browse files
committed
bump version, merge branch 'devel'
2 parents 17b63a2 + aba5020 commit 5715e74

File tree

6 files changed

+49
-18
lines changed

6 files changed

+49
-18
lines changed

argopt/_argopt.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818

1919
RE_ARG_ONCE = re.compile(r"(?<!Optional\(|neOrMore\()"
20-
"Argument\('(<\S+?>)', (\S+?), (\S+?)\)")
20+
"Argument\('(\S+?)', (\S+?), (\S+?)\)")
2121
RE_ARG_STAR = re.compile(r"Optional\(OneOrMore\(Argument\("
22-
"'(<\S+?>)', (\S+?), (\S+?)\)\)\)")
22+
"'(\S+?)', (\S+?), (\S+?)\)\)\)")
2323
RE_ARG_PLUS = re.compile(r"(?<!Optional\()"
24-
"OneOrMore\(Argument\('(<\S+?>)', (\S+?), (\S+?)\)\)")
25-
RE_ARG_QEST = re.compile(r"Optional\(Argument\('(<\S+?>)', (\S+?), (\S+?)\)\)")
24+
"OneOrMore\(Argument\('(\S+?)', (\S+?), (\S+?)\)\)")
25+
RE_ARG_QEST = re.compile(r"Optional\(Argument\('(\S+?)', (\S+?), (\S+?)\)\)")
2626

2727

2828
def findall_args(re, pattern):
@@ -159,9 +159,8 @@ def argopt(doc='', argparser=ArgumentParser,
159159
pu = printable_usage(doc)
160160
log.log(logLevel, doc[:doc.find(pu)])
161161
args, opts = docopt_parser(doc,
162-
log=max(logLevel - 10, logging.NOTSET),
162+
logLevel=max(logLevel - 10, logging.NOTSET),
163163
**_kwargs)
164-
165164
_kwargs.setdefault("prog", pu.split()[1])
166165
_kwargs.setdefault("description", doc[:doc.find(pu)])
167166
# epilogue
@@ -188,10 +187,7 @@ def argopt(doc='', argparser=ArgumentParser,
188187
k['type'] = a.type
189188
if a.value is not None:
190189
k['default'] = a.value
191-
parser.add_argument(a.name[1:-1], # strip out encompassing '<>'
192-
nargs=a.nargs,
193-
help=a.desc,
194-
**k)
190+
parser.add_argument(a.name_stripped, nargs=a.nargs, help=a.desc, **k)
195191
for o in opts:
196192
log.log(logLevel, "o:%r" % o)
197193
if o.name in ('-h', '--help'):

argopt/_docopt.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ def parse(class_, argument_description):
170170
value = matched[0] if matched else None
171171
return class_(name, value, description)
172172

173+
@property
174+
def name_stripped(self):
175+
return self.name[1:-1] \
176+
if self.name[0] == '<' and self.name[-1] == '>' else self.name
177+
173178

174179
class Command(Argument):
175180
def __init__(self, name, value=False):
@@ -444,13 +449,36 @@ def parse_atom(tokens, options):
444449
return [Command(tokens.move())]
445450

446451

452+
def get_indent(doc, optargs=False):
453+
"""Returns indent level for options/arguments"""
454+
if optargs: # just Options and Arguments sections
455+
try:
456+
lines = printable_usage(doc, "arguments").split('\n')
457+
except DocoptLanguageError:
458+
lines = []
459+
try:
460+
lines += printable_usage(doc, "options").split('\n')
461+
except DocoptLanguageError:
462+
pass
463+
else:
464+
lines = doc.split('\n')
465+
indents = [len(l) - len(l.lstrip()) for l in lines]
466+
return ' ' * (min([i for i in indents if i]) if indents else 0)
467+
468+
447469
def parse_defaults(doc):
448470
# in python < 2.7 you can't pass flags=re.MULTILINE
449-
split = re.split('\n *(<\S+?>|-\S+?)', doc)[1:]
471+
ind = get_indent(doc, optargs=True)
472+
if not ind:
473+
return [], []
474+
#split = re.split('\n (<\S+?>|-\S+?)', doc)[1:]
475+
split = re.split('\n' + ind + '(<\S+?>|[A-Z0-9_]+|-\S+)', doc)[1:]
476+
#print ">>>>>", split, "<<<<<<"
477+
# strip next sections from descriptions
450478
split = [s1 + s2.split('\n\n')[0]
451479
for s1, s2 in zip(split[::2], split[1::2])]
452480
options = [Option.parse(s) for s in split if s.startswith('-')]
453-
arguments = [Argument.parse(s) for s in split if s.startswith('<')]
481+
arguments = [Argument.parse(s) for s in split if not s.startswith('-')]
454482
return options, arguments
455483

456484

argopt/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
__all__ = ["__version__"]
1313

1414
# major, minor, patch, -extra
15-
version_info = 0, 4, 0
15+
version_info = 0, 5, 0
1616

1717
# Nice string for the version
1818
__version__ = '.'.join(map(str, version_info))

argopt/tests/tests_argopt.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_basic():
1515
args = docopt(__doc__)
1616
1717
Usage:
18-
test.py [-h | options] <x> [<y>...]
18+
test.py [-h | options] <x> Y [<z>...]
1919
2020
Arguments:
2121
<x> A file.
@@ -38,7 +38,8 @@ def test_basic():
3838
args = docopt(__doc__)''' in res)
3939
assert (i in res for i in '''positional arguments:
4040
x A file.
41-
y
41+
Y
42+
z
4243
4344
optional arguments:
4445
-h, --help show this help message and exit
@@ -57,7 +58,8 @@ def test_basic():
5758
assert (args.x == 'such')
5859
except AssertionError as e:
5960
raise AssertionError("x:" + str(args.x) + str(e))
60-
assert (args.y == 'test much is'.split())
61+
assert (args.Y == 'test')
62+
assert (args.z == 'much is'.split())
6163
try:
6264
parser.parse_args(args=['-v'])
6365
except SystemExit as e:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def execute_makefile_commands(commands, alias, verbose=False):
180180
bugtrack_url='https://github.com/casperdcl/argopt/issues',
181181
platforms=['any'],
182182
packages=['argopt'],
183-
install_requires=['argparse'],
183+
install_requires=['argparse'] if sys.version_info[:2] < (2, 7) else [],
184184
package_data={'': ['LICENCE']},
185185
classifiers=[
186186
# Trove classifiers

tox.ini

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ deps = {[extra]deps}
3939
commands = {[extra]commands}
4040

4141
[testenv:py26]
42-
deps = {[coverage]deps}
42+
deps =
43+
{[core]deps}
44+
coverage
45+
coveralls==1.2.0
46+
pycparser==2.18
47+
idna==2.7
4348
commands = {[coverage]commands}
4449

4550
[testenv:flake8]

0 commit comments

Comments
 (0)