Require implementations for warnings.showwarning() support the 'line' argument.
[python.git] / Lib / test / test_getopt.py
blobaac720d5a63eb343e20118b17f6e51f4e4bedbaa
1 # test_getopt.py
2 # David Goodger <dgoodger@bigfoot.com> 2000-08-19
4 from test.test_support import verbose, run_doctest, run_unittest
5 import unittest
7 import getopt
8 import os
10 sentinel = object()
12 class GetoptTests(unittest.TestCase):
13 def setUp(self):
14 self.old_posixly_correct = os.environ.get("POSIXLY_CORRECT", sentinel)
15 if self.old_posixly_correct is not sentinel:
16 del os.environ["POSIXLY_CORRECT"]
18 def tearDown(self):
19 if self.old_posixly_correct is sentinel:
20 os.environ.pop("POSIXLY_CORRECT", None)
21 else:
22 os.environ["POSIXLY_CORRECT"] = self.old_posixly_correct
24 def assertError(self, *args, **kwargs):
25 self.assertRaises(getopt.GetoptError, *args, **kwargs)
27 def test_short_has_arg(self):
28 self.failUnless(getopt.short_has_arg('a', 'a:'))
29 self.failIf(getopt.short_has_arg('a', 'a'))
30 self.assertError(getopt.short_has_arg, 'a', 'b')
32 def test_long_has_args(self):
33 has_arg, option = getopt.long_has_args('abc', ['abc='])
34 self.failUnless(has_arg)
35 self.assertEqual(option, 'abc')
37 has_arg, option = getopt.long_has_args('abc', ['abc'])
38 self.failIf(has_arg)
39 self.assertEqual(option, 'abc')
41 has_arg, option = getopt.long_has_args('abc', ['abcd'])
42 self.failIf(has_arg)
43 self.assertEqual(option, 'abcd')
45 self.assertError(getopt.long_has_args, 'abc', ['def'])
46 self.assertError(getopt.long_has_args, 'abc', [])
47 self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde'])
49 def test_do_shorts(self):
50 opts, args = getopt.do_shorts([], 'a', 'a', [])
51 self.assertEqual(opts, [('-a', '')])
52 self.assertEqual(args, [])
54 opts, args = getopt.do_shorts([], 'a1', 'a:', [])
55 self.assertEqual(opts, [('-a', '1')])
56 self.assertEqual(args, [])
58 #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
59 #self.assertEqual(opts, [('-a', '1')])
60 #self.assertEqual(args, [])
62 opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
63 self.assertEqual(opts, [('-a', '1')])
64 self.assertEqual(args, [])
66 opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
67 self.assertEqual(opts, [('-a', '1')])
68 self.assertEqual(args, ['2'])
70 self.assertError(getopt.do_shorts, [], 'a1', 'a', [])
71 self.assertError(getopt.do_shorts, [], 'a', 'a:', [])
73 def test_do_longs(self):
74 opts, args = getopt.do_longs([], 'abc', ['abc'], [])
75 self.assertEqual(opts, [('--abc', '')])
76 self.assertEqual(args, [])
78 opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
79 self.assertEqual(opts, [('--abc', '1')])
80 self.assertEqual(args, [])
82 opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
83 self.assertEqual(opts, [('--abcd', '1')])
84 self.assertEqual(args, [])
86 opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
87 self.assertEqual(opts, [('--abc', '')])
88 self.assertEqual(args, [])
90 # Much like the preceding, except with a non-alpha character ("-") in
91 # option name that precedes "="; failed in
92 # http://python.org/sf/126863
93 opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
94 self.assertEqual(opts, [('--foo', '42')])
95 self.assertEqual(args, [])
97 self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], [])
98 self.assertError(getopt.do_longs, [], 'abc', ['abc='], [])
100 def test_getopt(self):
101 # note: the empty string between '-a' and '--beta' is significant:
102 # it simulates an empty string option argument ('-a ""') on the
103 # command line.
104 cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a',
105 '', '--beta', 'arg1', 'arg2']
107 opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
108 self.assertEqual(opts, [('-a', '1'), ('-b', ''),
109 ('--alpha', '2'), ('--beta', ''),
110 ('-a', '3'), ('-a', ''), ('--beta', '')])
111 # Note ambiguity of ('-b', '') and ('-a', '') above. This must be
112 # accounted for in the code that calls getopt().
113 self.assertEqual(args, ['arg1', 'arg2'])
115 self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta'])
117 def test_gnu_getopt(self):
118 # Test handling of GNU style scanning mode.
119 cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
121 # GNU style
122 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
123 self.assertEqual(args, ['arg1'])
124 self.assertEqual(opts, [('-a', ''), ('-b', '1'),
125 ('--alpha', ''), ('--beta', '2')])
127 # recognize "-" as an argument
128 opts, args = getopt.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', [])
129 self.assertEqual(args, ['-'])
130 self.assertEqual(opts, [('-a', ''), ('-b', '-')])
132 # Posix style via +
133 opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
134 self.assertEqual(opts, [('-a', '')])
135 self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
137 # Posix style via POSIXLY_CORRECT
138 os.environ["POSIXLY_CORRECT"] = "1"
139 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
140 self.assertEqual(opts, [('-a', '')])
141 self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
143 def test_libref_examples(self):
144 s = """
145 Examples from the Library Reference: Doc/lib/libgetopt.tex
147 An example using only Unix style options:
150 >>> import getopt
151 >>> args = '-a -b -cfoo -d bar a1 a2'.split()
152 >>> args
153 ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
154 >>> optlist, args = getopt.getopt(args, 'abc:d:')
155 >>> optlist
156 [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
157 >>> args
158 ['a1', 'a2']
160 Using long option names is equally easy:
163 >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
164 >>> args = s.split()
165 >>> args
166 ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
167 >>> optlist, args = getopt.getopt(args, 'x', [
168 ... 'condition=', 'output-file=', 'testing'])
169 >>> optlist
170 [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
171 >>> args
172 ['a1', 'a2']
175 import types
176 m = types.ModuleType("libreftest", s)
177 run_doctest(m, verbose)
180 def test_main():
181 run_unittest(GetoptTests)
183 if __name__ == "__main__":
184 test_main()