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