1 # test_getopt.py
2 # David Goodger <dgoodger@bigfoot.com> 2000-08-19
3
4 from test.support.os_helper import EnvironmentVarGuard
5 import doctest
6 import unittest
7
8 import getopt
9
10 sentinel = object()
11
12 class ESC[4;38;5;81mGetoptTests(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
13 def setUp(self):
14 self.env = self.enterContext(EnvironmentVarGuard())
15 if "POSIXLY_CORRECT" in self.env:
16 del self.env["POSIXLY_CORRECT"]
17
18 def assertError(self, *args, **kwargs):
19 self.assertRaises(getopt.GetoptError, *args, **kwargs)
20
21 def test_short_has_arg(self):
22 self.assertTrue(getopt.short_has_arg('a', 'a:'))
23 self.assertFalse(getopt.short_has_arg('a', 'a'))
24 self.assertError(getopt.short_has_arg, 'a', 'b')
25
26 def test_long_has_args(self):
27 has_arg, option = getopt.long_has_args('abc', ['abc='])
28 self.assertTrue(has_arg)
29 self.assertEqual(option, 'abc')
30
31 has_arg, option = getopt.long_has_args('abc', ['abc'])
32 self.assertFalse(has_arg)
33 self.assertEqual(option, 'abc')
34
35 has_arg, option = getopt.long_has_args('abc', ['abcd'])
36 self.assertFalse(has_arg)
37 self.assertEqual(option, 'abcd')
38
39 self.assertError(getopt.long_has_args, 'abc', ['def'])
40 self.assertError(getopt.long_has_args, 'abc', [])
41 self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde'])
42
43 def test_do_shorts(self):
44 opts, args = getopt.do_shorts([], 'a', 'a', [])
45 self.assertEqual(opts, [('-a', '')])
46 self.assertEqual(args, [])
47
48 opts, args = getopt.do_shorts([], 'a1', 'a:', [])
49 self.assertEqual(opts, [('-a', '1')])
50 self.assertEqual(args, [])
51
52 #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
53 #self.assertEqual(opts, [('-a', '1')])
54 #self.assertEqual(args, [])
55
56 opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
57 self.assertEqual(opts, [('-a', '1')])
58 self.assertEqual(args, [])
59
60 opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
61 self.assertEqual(opts, [('-a', '1')])
62 self.assertEqual(args, ['2'])
63
64 self.assertError(getopt.do_shorts, [], 'a1', 'a', [])
65 self.assertError(getopt.do_shorts, [], 'a', 'a:', [])
66
67 def test_do_longs(self):
68 opts, args = getopt.do_longs([], 'abc', ['abc'], [])
69 self.assertEqual(opts, [('--abc', '')])
70 self.assertEqual(args, [])
71
72 opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
73 self.assertEqual(opts, [('--abc', '1')])
74 self.assertEqual(args, [])
75
76 opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
77 self.assertEqual(opts, [('--abcd', '1')])
78 self.assertEqual(args, [])
79
80 opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
81 self.assertEqual(opts, [('--abc', '')])
82 self.assertEqual(args, [])
83
84 # Much like the preceding, except with a non-alpha character ("-") in
85 # option name that precedes "="; failed in
86 # http://python.org/sf/126863
87 opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
88 self.assertEqual(opts, [('--foo', '42')])
89 self.assertEqual(args, [])
90
91 self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], [])
92 self.assertError(getopt.do_longs, [], 'abc', ['abc='], [])
93
94 def test_getopt(self):
95 # note: the empty string between '-a' and '--beta' is significant:
96 # it simulates an empty string option argument ('-a ""') on the
97 # command line.
98 cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a',
99 '', '--beta', 'arg1', 'arg2']
100
101 opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
102 self.assertEqual(opts, [('-a', '1'), ('-b', ''),
103 ('--alpha', '2'), ('--beta', ''),
104 ('-a', '3'), ('-a', ''), ('--beta', '')])
105 # Note ambiguity of ('-b', '') and ('-a', '') above. This must be
106 # accounted for in the code that calls getopt().
107 self.assertEqual(args, ['arg1', 'arg2'])
108
109 self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta'])
110
111 def test_gnu_getopt(self):
112 # Test handling of GNU style scanning mode.
113 cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
114
115 # GNU style
116 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
117 self.assertEqual(args, ['arg1'])
118 self.assertEqual(opts, [('-a', ''), ('-b', '1'),
119 ('--alpha', ''), ('--beta', '2')])
120
121 # recognize "-" as an argument
122 opts, args = getopt.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', [])
123 self.assertEqual(args, ['-'])
124 self.assertEqual(opts, [('-a', ''), ('-b', '-')])
125
126 # Posix style via +
127 opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
128 self.assertEqual(opts, [('-a', '')])
129 self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
130
131 # Posix style via POSIXLY_CORRECT
132 self.env["POSIXLY_CORRECT"] = "1"
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'])
136
137 def test_issue4629(self):
138 longopts, shortopts = getopt.getopt(['--help='], '', ['help='])
139 self.assertEqual(longopts, [('--help', '')])
140 longopts, shortopts = getopt.getopt(['--help=x'], '', ['help='])
141 self.assertEqual(longopts, [('--help', 'x')])
142 self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help'])
143
144 def test_libref_examples():
145 """
146 Examples from the Library Reference: Doc/lib/libgetopt.tex
147
148 An example using only Unix style options:
149
150
151 >>> import getopt
152 >>> args = '-a -b -cfoo -d bar a1 a2'.split()
153 >>> args
154 ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
155 >>> optlist, args = getopt.getopt(args, 'abc:d:')
156 >>> optlist
157 [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
158 >>> args
159 ['a1', 'a2']
160
161 Using long option names is equally easy:
162
163
164 >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
165 >>> args = s.split()
166 >>> args
167 ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
168 >>> optlist, args = getopt.getopt(args, 'x', [
169 ... 'condition=', 'output-file=', 'testing'])
170 >>> optlist
171 [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
172 >>> args
173 ['a1', 'a2']
174 """
175
176 def load_tests(loader, tests, pattern):
177 tests.addTest(doctest.DocTestSuite())
178 return tests
179
180
181 if __name__ == "__main__":
182 unittest.main()