summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Reiter <reiter.christoph@gmail.com>2018-04-21 08:56:33 +0200
committerChristoph Reiter <reiter.christoph@gmail.com>2018-04-21 09:20:50 +0200
commit8181d23ffe4ed05c9216269c64eebcaeda15ed16 (patch)
tree7378295742ee363b0893d16ee70e456d50e7fe15
parentea358359a34ad96bf395641911aeaf80182d3aad (diff)
downloadpygobject-optionparser-remain-args.tar.gz
OptionParser.parse_args: return leftover arguments. Fixes #200optionparser-remain-args
Adjust the tests accordingly. Not sure why it was that way and tested as well.
-rw-r--r--gi/_option.py2
-rw-r--r--tests/test_option.py53
2 files changed, 38 insertions, 17 deletions
diff --git a/gi/_option.py b/gi/_option.py
index 3791137b..9babb2ac 100644
--- a/gi/_option.py
+++ b/gi/_option.py
@@ -341,7 +341,6 @@ class OptionParser(optparse.OptionParser):
rargs[:] = context.parse([sys.argv[0]] + rargs)[1:]
def parse_args(self, args=None, values=None):
- old_args = args or []
try:
options, args = optparse.OptionParser.parse_args(
self, args, values)
@@ -362,7 +361,6 @@ class OptionParser(optparse.OptionParser):
for key, value in group.values.__dict__.items():
options.ensure_value(key, value)
- args = args[2:-len(old_args)]
return options, args
diff --git a/tests/test_option.py b/tests/test_option.py
index 2854508b..251eb3ad 100644
--- a/tests/test_option.py
+++ b/tests/test_option.py
@@ -57,29 +57,53 @@ class TestOption(unittest.TestCase):
self.parser.add_option_group(group)
return group
- def test_parse_args(self):
+ def test_integer(self):
+ self._create_group()
options, args = self.parser.parse_args(
- ["test_option.py"])
- self.assertFalse(args)
+ ["--test-integer", "42", "bla"])
+ assert options.test_integer == 42
+ assert args == ["bla"]
+
+ def test_file(self):
+ self._create_group()
options, args = self.parser.parse_args(
- ["test_option.py", "foo"])
- self.assertEqual(args, [])
+ ["--file", "fn", "bla"])
+ assert options.unit_file == "fn"
+ assert args == ["bla"]
+
+ def test_mixed(self):
+ self._create_group()
options, args = self.parser.parse_args(
- ["test_option.py", "foo", "bar"])
- self.assertEqual(args, [])
+ ["--file", "fn", "--test-integer", "12", "--test",
+ "--g-fatal-warnings", "nope"])
+
+ assert options.unit_file == "fn"
+ assert options.test_integer == 12
+ assert options.test is False
+ assert options.fatal_warnings is True
+ assert args == ["nope"]
+
+ def test_parse_args(self):
+ options, args = self.parser.parse_args([])
+ self.assertFalse(args)
+
+ options, args = self.parser.parse_args(["foo"])
+ self.assertEqual(args, ["foo"])
+
+ options, args = self.parser.parse_args(["foo", "bar"])
+ self.assertEqual(args, ["foo", "bar"])
def test_parse_args_double_dash(self):
- options, args = self.parser.parse_args(
- ["test_option.py", "--", "-xxx"])
- # self.assertEqual(args, ["-xxx"])
+ options, args = self.parser.parse_args(["--", "-xxx"])
+ self.assertEqual(args, ["--", "-xxx"])
def test_parse_args_group(self):
group = self._create_group()
options, args = self.parser.parse_args(
- ["test_option.py", "--test", "-f", "test"])
+ ["--test", "-f", "test"])
self.assertFalse(options.test)
self.assertEqual(options.unit_file, "test")
@@ -92,12 +116,12 @@ class TestOption(unittest.TestCase):
def test_option_value_error(self):
self._create_group()
self.assertRaises(GLib.option.OptionValueError, self.parser.parse_args,
- ["test_option.py", "--test-integer=text"])
+ ["--test-integer=text"])
def test_bad_option_error(self):
self.assertRaises(GLib.option.BadOptionError,
self.parser.parse_args,
- ["test_option.py", "--unknwon-option"])
+ ["--unknwon-option"])
def test_option_group_constructor(self):
self.assertRaises(TypeError, GLib.option.OptionGroup)
@@ -106,8 +130,7 @@ class TestOption(unittest.TestCase):
self._create_group()
with capture_exceptions() as exc:
- self.parser.parse_args(
- ["test_option.py", "--callback-failure-test"])
+ self.parser.parse_args(["--callback-failure-test"])
assert len(exc) == 1
assert exc[0].value.args[0] == "foo"