Change a variable type to avoid signed overflow; replace repeated '19999' constant...
[python.git] / Lib / test / test_getopt.py
blob4729301c9293d89ead207325323159af9c7aac87
1 # test_getopt.py
2 # David Goodger <dgoodger@bigfoot.com> 2000-08-19
4 from test.test_support import verbose, run_doctest, run_unittest, EnvironmentVarGuard
5 import unittest
7 import getopt
8 import os
10 sentinel = object()
12 class GetoptTests(unittest.TestCase):
13 def setUp(self):
14 self.env = EnvironmentVarGuard()
15 if "POSIXLY_CORRECT" in self.env:
16 del self.env["POSIXLY_CORRECT"]
18 def tearDown(self):
19 self.env.__exit__()
20 del self.env
22 def assertError(self, *args, **kwargs):
23 self.assertRaises(getopt.GetoptError, *args, **kwargs)
25 def test_short_has_arg(self):
26 self.assertTrue(getopt.short_has_arg('a', 'a:'))
27 self.assertFalse(getopt.short_has_arg('a', 'a'))
28 self.assertError(getopt.short_has_arg, 'a', 'b')
30 def test_long_has_args(self):
31 has_arg, option = getopt.long_has_args('abc', ['abc='])
32 self.assertTrue(has_arg)
33 self.assertEqual(option, 'abc')
35 has_arg, option = getopt.long_has_args('abc', ['abc'])
36 self.assertFalse(has_arg)
37 self.assertEqual(option, 'abc')
39 has_arg, option = getopt.long_has_args('abc', ['abcd'])
40 self.assertFalse(has_arg)
41 self.assertEqual(option, 'abcd')
43 self.assertError(getopt.long_has_args, 'abc', ['def'])
44 self.assertError(getopt.long_has_args, 'abc', [])
45 self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde'])
47 def test_do_shorts(self):
48 opts, args = getopt.do_shorts([], 'a', 'a', [])
49 self.assertEqual(opts, [('-a', '')])
50 self.assertEqual(args, [])
52 opts, args = getopt.do_shorts([], 'a1', 'a:', [])
53 self.assertEqual(opts, [('-a', '1')])
54 self.assertEqual(args, [])
56 #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
57 #self.assertEqual(opts, [('-a', '1')])
58 #self.assertEqual(args, [])
60 opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
61 self.assertEqual(opts, [('-a', '1')])
62 self.assertEqual(args, [])
64 opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
65 self.assertEqual(opts, [('-a', '1')])
66 self.assertEqual(args, ['2'])
68 self.assertError(getopt.do_shorts, [], 'a1', 'a', [])
69 self.assertError(getopt.do_shorts, [], 'a', 'a:', [])
71 def test_do_longs(self):
72 opts, args = getopt.do_longs([], 'abc', ['abc'], [])
73 self.assertEqual(opts, [('--abc', '')])
74 self.assertEqual(args, [])
76 opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
77 self.assertEqual(opts, [('--abc', '1')])
78 self.assertEqual(args, [])
80 opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
81 self.assertEqual(opts, [('--abcd', '1')])
82 self.assertEqual(args, [])
84 opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
85 self.assertEqual(opts, [('--abc', '')])
86 self.assertEqual(args, [])
88 # Much like the preceding, except with a non-alpha character ("-") in
89 # option name that precedes "="; failed in
90 # http://python.org/sf/126863
91 opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
92 self.assertEqual(opts, [('--foo', '42')])
93 self.assertEqual(args, [])
95 self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], [])
96 self.assertError(getopt.do_longs, [], 'abc', ['abc='], [])
98 def test_getopt(self):
99 # note: the empty string between '-a' and '--beta' is significant:
100 # it simulates an empty string option argument ('-a ""') on the
101 # command line.
102 cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a',
103 '', '--beta', 'arg1', 'arg2']
105 opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
106 self.assertEqual(opts, [('-a', '1'), ('-b', ''),
107 ('--alpha', '2'), ('--beta', ''),
108 ('-a', '3'), ('-a', ''), ('--beta', '')])
109 # Note ambiguity of ('-b', '') and ('-a', '') above. This must be
110 # accounted for in the code that calls getopt().
111 self.assertEqual(args, ['arg1', 'arg2'])
113 self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta'])
115 def test_gnu_getopt(self):
116 # Test handling of GNU style scanning mode.
117 cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
119 # GNU style
120 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
121 self.assertEqual(args, ['arg1'])
122 self.assertEqual(opts, [('-a', ''), ('-b', '1'),
123 ('--alpha', ''), ('--beta', '2')])
125 # recognize "-" as an argument
126 opts, args = getopt.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', [])
127 self.assertEqual(args, ['-'])
128 self.assertEqual(opts, [('-a', ''), ('-b', '-')])
130 # Posix style via +
131 opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
132 self.assertEqual(opts, [('-a', '')])
133 self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
135 # Posix style via POSIXLY_CORRECT
136 self.env["POSIXLY_CORRECT"] = "1"
137 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
138 self.assertEqual(opts, [('-a', '')])
139 self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
141 def test_libref_examples(self):
142 s = """
143 Examples from the Library Reference: Doc/lib/libgetopt.tex
145 An example using only Unix style options:
148 >>> import getopt
149 >>> args = '-a -b -cfoo -d bar a1 a2'.split()
150 >>> args
151 ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
152 >>> optlist, args = getopt.getopt(args, 'abc:d:')
153 >>> optlist
154 [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
155 >>> args
156 ['a1', 'a2']
158 Using long option names is equally easy:
161 >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
162 >>> args = s.split()
163 >>> args
164 ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
165 >>> optlist, args = getopt.getopt(args, 'x', [
166 ... 'condition=', 'output-file=', 'testing'])
167 >>> optlist
168 [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
169 >>> args
170 ['a1', 'a2']
173 import types
174 m = types.ModuleType("libreftest", s)
175 run_doctest(m, verbose)
178 def test_main():
179 run_unittest(GetoptTests)
181 if __name__ == "__main__":
182 test_main()