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