• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# test_getopt.py
2# David Goodger <dgoodger@bigfoot.com> 2000-08-19
3
4from test.support import verbose, run_doctest
5from test.support.os_helper import EnvironmentVarGuard
6import unittest
7
8import getopt
9
10sentinel = object()
11
12class GetoptTests(unittest.TestCase):
13    def setUp(self):
14        self.env = EnvironmentVarGuard()
15        if "POSIXLY_CORRECT" in self.env:
16            del self.env["POSIXLY_CORRECT"]
17
18    def tearDown(self):
19        self.env.__exit__()
20        del self.env
21
22    def assertError(self, *args, **kwargs):
23        self.assertRaises(getopt.GetoptError, *args, **kwargs)
24
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')
29
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')
34
35        has_arg, option = getopt.long_has_args('abc', ['abc'])
36        self.assertFalse(has_arg)
37        self.assertEqual(option, 'abc')
38
39        has_arg, option = getopt.long_has_args('abc', ['abcd'])
40        self.assertFalse(has_arg)
41        self.assertEqual(option, 'abcd')
42
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'])
46
47    def test_do_shorts(self):
48        opts, args = getopt.do_shorts([], 'a', 'a', [])
49        self.assertEqual(opts, [('-a', '')])
50        self.assertEqual(args, [])
51
52        opts, args = getopt.do_shorts([], 'a1', 'a:', [])
53        self.assertEqual(opts, [('-a', '1')])
54        self.assertEqual(args, [])
55
56        #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
57        #self.assertEqual(opts, [('-a', '1')])
58        #self.assertEqual(args, [])
59
60        opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
61        self.assertEqual(opts, [('-a', '1')])
62        self.assertEqual(args, [])
63
64        opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
65        self.assertEqual(opts, [('-a', '1')])
66        self.assertEqual(args, ['2'])
67
68        self.assertError(getopt.do_shorts, [], 'a1', 'a', [])
69        self.assertError(getopt.do_shorts, [], 'a', 'a:', [])
70
71    def test_do_longs(self):
72        opts, args = getopt.do_longs([], 'abc', ['abc'], [])
73        self.assertEqual(opts, [('--abc', '')])
74        self.assertEqual(args, [])
75
76        opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
77        self.assertEqual(opts, [('--abc', '1')])
78        self.assertEqual(args, [])
79
80        opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
81        self.assertEqual(opts, [('--abcd', '1')])
82        self.assertEqual(args, [])
83
84        opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
85        self.assertEqual(opts, [('--abc', '')])
86        self.assertEqual(args, [])
87
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, [])
94
95        self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], [])
96        self.assertError(getopt.do_longs, [], 'abc', ['abc='], [])
97
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']
104
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'])
112
113        self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta'])
114
115    def test_gnu_getopt(self):
116        # Test handling of GNU style scanning mode.
117        cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
118
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')])
124
125        # recognize "-" as an argument
126        opts, args = getopt.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', [])
127        self.assertEqual(args, ['-'])
128        self.assertEqual(opts, [('-a', ''), ('-b', '-')])
129
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'])
134
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'])
140
141    def test_libref_examples(self):
142        s = """
143        Examples from the Library Reference:  Doc/lib/libgetopt.tex
144
145        An example using only Unix style options:
146
147
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']
157
158        Using long option names is equally easy:
159
160
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']
171        """
172
173        import types
174        m = types.ModuleType("libreftest", s)
175        run_doctest(m, verbose)
176
177    def test_issue4629(self):
178        longopts, shortopts = getopt.getopt(['--help='], '', ['help='])
179        self.assertEqual(longopts, [('--help', '')])
180        longopts, shortopts = getopt.getopt(['--help=x'], '', ['help='])
181        self.assertEqual(longopts, [('--help', 'x')])
182        self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help'])
183
184if __name__ == "__main__":
185    unittest.main()
186