moving from optparse to argparse. Major difficulty is
that argparse does not have Option objects -> added class Argument
Needed explicit call of MyOptionParser.format_epilog as argparse
does not have that. The parse_arg epilog argument wraps the text,
which is not the same (could be handled with a special formatter).
- parser.parse() now returns single argument (with positional args in
.file_or_dir)
- "file_or_dir" made a class variable Config._file_or_dir and used in help and tests
- added code for argcomplete (because of which this all started!)
addoption:
- if option type is a string ('int' or 'string', this converted to
int resp. str
- if option type is 'count' this is changed to the type of choices[0]
testing:
- added tests for Argument
- test_mark.test_keyword_extra split as ['-k', '-mykeyword'] generates argparse
error test split in two and one marked as fail
- testing hints, multiline and more strickt (for if someone moves format_epilog
to epilog argument of parse_args without Formatter)
- test for destination derived from long option with internal dash
- renamed second test_parseopt.test_parse() to test_parse2 as it was
not tested at all (the first was tested.)
--HG--
branch : argparse
This commit is contained in:
@@ -7,8 +7,44 @@ class TestParser:
|
||||
parser = parseopt.Parser(usage="xyz")
|
||||
pytest.raises(SystemExit, 'parser.parse(["-h"])')
|
||||
out, err = capsys.readouterr()
|
||||
assert err.find("no such option") != -1
|
||||
assert err.find("error: unrecognized arguments") != -1
|
||||
|
||||
def test_argument(self):
|
||||
with pytest.raises(parseopt.ArgumentError):
|
||||
# need a short or long option
|
||||
argument = parseopt.Argument()
|
||||
argument = parseopt.Argument('-t')
|
||||
assert argument._short_opts == ['-t']
|
||||
assert argument._long_opts == []
|
||||
assert argument.dest == 't'
|
||||
argument = parseopt.Argument('-t', '--test')
|
||||
assert argument._short_opts == ['-t']
|
||||
assert argument._long_opts == ['--test']
|
||||
assert argument.dest == 'test'
|
||||
argument = parseopt.Argument('-t', '--test', dest='abc')
|
||||
assert argument.dest == 'abc'
|
||||
|
||||
def test_argument_type(self):
|
||||
argument = parseopt.Argument('-t', dest='abc', type='int')
|
||||
assert argument.type is int
|
||||
argument = parseopt.Argument('-t', dest='abc', type='string')
|
||||
assert argument.type is str
|
||||
argument = parseopt.Argument('-t', dest='abc', type=float)
|
||||
assert argument.type is float
|
||||
with pytest.raises(KeyError):
|
||||
argument = parseopt.Argument('-t', dest='abc', type='choice')
|
||||
argument = parseopt.Argument('-t', dest='abc', type='choice',
|
||||
choices=['red', 'blue'])
|
||||
assert argument.type is str
|
||||
|
||||
def test_argument_processopt(self):
|
||||
argument = parseopt.Argument('-t', type=int)
|
||||
argument.default = 42
|
||||
argument.dest = 'abc'
|
||||
res = argument.attrs()
|
||||
assert res['default'] == 42
|
||||
assert res['dest'] == 'abc'
|
||||
|
||||
def test_group_add_and_get(self):
|
||||
parser = parseopt.Parser()
|
||||
group = parser.getgroup("hello", description="desc")
|
||||
@@ -36,7 +72,7 @@ class TestParser:
|
||||
group = parseopt.OptionGroup("hello")
|
||||
group.addoption("--option1", action="store_true")
|
||||
assert len(group.options) == 1
|
||||
assert isinstance(group.options[0], py.std.optparse.Option)
|
||||
assert isinstance(group.options[0], parseopt.Argument)
|
||||
|
||||
def test_group_shortopt_lowercase(self):
|
||||
parser = parseopt.Parser()
|
||||
@@ -58,19 +94,19 @@ class TestParser:
|
||||
def test_parse(self):
|
||||
parser = parseopt.Parser()
|
||||
parser.addoption("--hello", dest="hello", action="store")
|
||||
option, args = parser.parse(['--hello', 'world'])
|
||||
assert option.hello == "world"
|
||||
assert not args
|
||||
args = parser.parse(['--hello', 'world'])
|
||||
assert args.hello == "world"
|
||||
assert not getattr(args, parseopt.Config._file_or_dir)
|
||||
|
||||
def test_parse(self):
|
||||
def test_parse2(self):
|
||||
parser = parseopt.Parser()
|
||||
option, args = parser.parse([py.path.local()])
|
||||
assert args[0] == py.path.local()
|
||||
args = parser.parse([py.path.local()])
|
||||
assert getattr(args, parseopt.Config._file_or_dir)[0] == py.path.local()
|
||||
|
||||
def test_parse_will_set_default(self):
|
||||
parser = parseopt.Parser()
|
||||
parser.addoption("--hello", dest="hello", default="x", action="store")
|
||||
option, args = parser.parse([])
|
||||
option = parser.parse([])
|
||||
assert option.hello == "x"
|
||||
del option.hello
|
||||
args = parser.parse_setoption([], option)
|
||||
@@ -87,28 +123,37 @@ class TestParser:
|
||||
assert option.world == 42
|
||||
assert not args
|
||||
|
||||
def test_parse_special_destination(self):
|
||||
parser = parseopt.Parser()
|
||||
x = parser.addoption("--ultimate-answer", type=int)
|
||||
args = parser.parse(['--ultimate-answer', '42'])
|
||||
assert args.ultimate_answer == 42
|
||||
|
||||
def test_parse_defaultgetter(self):
|
||||
def defaultget(option):
|
||||
if option.type == "int":
|
||||
if not hasattr(option, 'type'):
|
||||
return
|
||||
if option.type is int:
|
||||
option.default = 42
|
||||
elif option.type == "string":
|
||||
elif option.type is str:
|
||||
option.default = "world"
|
||||
parser = parseopt.Parser(processopt=defaultget)
|
||||
parser.addoption("--this", dest="this", type="int", action="store")
|
||||
parser.addoption("--hello", dest="hello", type="string", action="store")
|
||||
parser.addoption("--no", dest="no", action="store_true")
|
||||
option, args = parser.parse([])
|
||||
option = parser.parse([])
|
||||
assert option.hello == "world"
|
||||
assert option.this == 42
|
||||
|
||||
assert option.no is False
|
||||
|
||||
@pytest.mark.skipif("sys.version_info < (2,5)")
|
||||
def test_addoption_parser_epilog(testdir):
|
||||
testdir.makeconftest("""
|
||||
def pytest_addoption(parser):
|
||||
parser.hints.append("hello world")
|
||||
parser.hints.append("from me too")
|
||||
""")
|
||||
result = testdir.runpytest('--help')
|
||||
#assert result.ret != 0
|
||||
result.stdout.fnmatch_lines(["*hint: hello world*"])
|
||||
result.stdout.fnmatch_lines(["hint: hello world", "hint: from me too"])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user