blob: fa5701f310bb53b6a325993dd21aa451b5bad0e9 [file] [log] [blame]
Tim Peters571bb8f2000-08-20 04:18:40 +00001# test_getopt.py
2# David Goodger <dgoodger@bigfoot.com> 2000-08-19
3
Walter Dörwald155374d2009-05-01 19:58:58 +00004from test.support import verbose, run_doctest, run_unittest, EnvironmentVarGuard
Guido van Rossumd8faa362007-04-27 19:54:29 +00005import unittest
6
Tim Peters571bb8f2000-08-20 04:18:40 +00007import getopt
Tim Peters571bb8f2000-08-20 04:18:40 +00008
Guido van Rossumd8faa362007-04-27 19:54:29 +00009sentinel = object()
Tim Peters571bb8f2000-08-20 04:18:40 +000010
Guido van Rossumd8faa362007-04-27 19:54:29 +000011class GetoptTests(unittest.TestCase):
12 def setUp(self):
Walter Dörwald155374d2009-05-01 19:58:58 +000013 self.env = EnvironmentVarGuard()
14 if "POSIXLY_CORRECT" in self.env:
15 del self.env["POSIXLY_CORRECT"]
Fred Drake4354ba32004-08-03 15:54:45 +000016
Guido van Rossumd8faa362007-04-27 19:54:29 +000017 def tearDown(self):
Walter Dörwald155374d2009-05-01 19:58:58 +000018 self.env.__exit__()
19 del self.env
Tim Peters571bb8f2000-08-20 04:18:40 +000020
Guido van Rossumd8faa362007-04-27 19:54:29 +000021 def assertError(self, *args, **kwargs):
22 self.assertRaises(getopt.GetoptError, *args, **kwargs)
Tim Peters571bb8f2000-08-20 04:18:40 +000023
Guido van Rossumd8faa362007-04-27 19:54:29 +000024 def test_short_has_arg(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000025 self.assertTrue(getopt.short_has_arg('a', 'a:'))
26 self.assertFalse(getopt.short_has_arg('a', 'a'))
Guido van Rossumd8faa362007-04-27 19:54:29 +000027 self.assertError(getopt.short_has_arg, 'a', 'b')
Tim Peters571bb8f2000-08-20 04:18:40 +000028
Guido van Rossumd8faa362007-04-27 19:54:29 +000029 def test_long_has_args(self):
30 has_arg, option = getopt.long_has_args('abc', ['abc='])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000031 self.assertTrue(has_arg)
Guido van Rossumd8faa362007-04-27 19:54:29 +000032 self.assertEqual(option, 'abc')
Tim Peters571bb8f2000-08-20 04:18:40 +000033
Guido van Rossumd8faa362007-04-27 19:54:29 +000034 has_arg, option = getopt.long_has_args('abc', ['abc'])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000035 self.assertFalse(has_arg)
Guido van Rossumd8faa362007-04-27 19:54:29 +000036 self.assertEqual(option, 'abc')
Tim Peters571bb8f2000-08-20 04:18:40 +000037
Guido van Rossumd8faa362007-04-27 19:54:29 +000038 has_arg, option = getopt.long_has_args('abc', ['abcd'])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000039 self.assertFalse(has_arg)
Guido van Rossumd8faa362007-04-27 19:54:29 +000040 self.assertEqual(option, 'abcd')
Tim Peters571bb8f2000-08-20 04:18:40 +000041
Guido van Rossumd8faa362007-04-27 19:54:29 +000042 self.assertError(getopt.long_has_args, 'abc', ['def'])
43 self.assertError(getopt.long_has_args, 'abc', [])
44 self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde'])
Tim Peters571bb8f2000-08-20 04:18:40 +000045
Guido van Rossumd8faa362007-04-27 19:54:29 +000046 def test_do_shorts(self):
47 opts, args = getopt.do_shorts([], 'a', 'a', [])
48 self.assertEqual(opts, [('-a', '')])
49 self.assertEqual(args, [])
50
51 opts, args = getopt.do_shorts([], 'a1', 'a:', [])
52 self.assertEqual(opts, [('-a', '1')])
53 self.assertEqual(args, [])
54
55 #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
56 #self.assertEqual(opts, [('-a', '1')])
57 #self.assertEqual(args, [])
58
59 opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
60 self.assertEqual(opts, [('-a', '1')])
61 self.assertEqual(args, [])
62
63 opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
64 self.assertEqual(opts, [('-a', '1')])
65 self.assertEqual(args, ['2'])
66
67 self.assertError(getopt.do_shorts, [], 'a1', 'a', [])
68 self.assertError(getopt.do_shorts, [], 'a', 'a:', [])
69
70 def test_do_longs(self):
71 opts, args = getopt.do_longs([], 'abc', ['abc'], [])
72 self.assertEqual(opts, [('--abc', '')])
73 self.assertEqual(args, [])
74
75 opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
76 self.assertEqual(opts, [('--abc', '1')])
77 self.assertEqual(args, [])
78
79 opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
80 self.assertEqual(opts, [('--abcd', '1')])
81 self.assertEqual(args, [])
82
83 opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
84 self.assertEqual(opts, [('--abc', '')])
85 self.assertEqual(args, [])
86
87 # Much like the preceding, except with a non-alpha character ("-") in
88 # option name that precedes "="; failed in
89 # http://python.org/sf/126863
90 opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
91 self.assertEqual(opts, [('--foo', '42')])
92 self.assertEqual(args, [])
93
94 self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], [])
95 self.assertError(getopt.do_longs, [], 'abc', ['abc='], [])
96
97 def test_getopt(self):
98 # note: the empty string between '-a' and '--beta' is significant:
99 # it simulates an empty string option argument ('-a ""') on the
100 # command line.
101 cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a',
102 '', '--beta', 'arg1', 'arg2']
103
104 opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
105 self.assertEqual(opts, [('-a', '1'), ('-b', ''),
106 ('--alpha', '2'), ('--beta', ''),
107 ('-a', '3'), ('-a', ''), ('--beta', '')])
108 # Note ambiguity of ('-b', '') and ('-a', '') above. This must be
109 # accounted for in the code that calls getopt().
110 self.assertEqual(args, ['arg1', 'arg2'])
111
112 self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta'])
113
114 def test_gnu_getopt(self):
115 # Test handling of GNU style scanning mode.
116 cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
117
118 # GNU style
119 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
120 self.assertEqual(args, ['arg1'])
121 self.assertEqual(opts, [('-a', ''), ('-b', '1'),
122 ('--alpha', ''), ('--beta', '2')])
123
Georg Brandleee31162008-12-07 15:15:22 +0000124 # recognize "-" as an argument
125 opts, args = getopt.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', [])
126 self.assertEqual(args, ['-'])
127 self.assertEqual(opts, [('-a', ''), ('-b', '-')])
128
Guido van Rossumd8faa362007-04-27 19:54:29 +0000129 # Posix style via +
130 opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
131 self.assertEqual(opts, [('-a', '')])
132 self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
133
134 # Posix style via POSIXLY_CORRECT
Walter Dörwald155374d2009-05-01 19:58:58 +0000135 self.env["POSIXLY_CORRECT"] = "1"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000136 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
137 self.assertEqual(opts, [('-a', '')])
138 self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
139
140 def test_libref_examples(self):
141 s = """
142 Examples from the Library Reference: Doc/lib/libgetopt.tex
143
144 An example using only Unix style options:
Fred Drake4354ba32004-08-03 15:54:45 +0000145
146
Guido van Rossumd8faa362007-04-27 19:54:29 +0000147 >>> import getopt
148 >>> args = '-a -b -cfoo -d bar a1 a2'.split()
149 >>> args
150 ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
151 >>> optlist, args = getopt.getopt(args, 'abc:d:')
152 >>> optlist
153 [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
154 >>> args
155 ['a1', 'a2']
Martin v. Löwis446a25f2002-06-06 10:58:36 +0000156
Guido van Rossumd8faa362007-04-27 19:54:29 +0000157 Using long option names is equally easy:
Raymond Hettinger1a4a9d02003-04-29 19:58:04 +0000158
159
Guido van Rossumd8faa362007-04-27 19:54:29 +0000160 >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
161 >>> args = s.split()
162 >>> args
163 ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
164 >>> optlist, args = getopt.getopt(args, 'x', [
165 ... 'condition=', 'output-file=', 'testing'])
166 >>> optlist
167 [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
168 >>> args
169 ['a1', 'a2']
170 """
Raymond Hettinger1a4a9d02003-04-29 19:58:04 +0000171
Christian Heimes45f9af32007-11-27 21:50:00 +0000172 import types
173 m = types.ModuleType("libreftest", s)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000174 run_doctest(m, verbose)
Raymond Hettinger1a4a9d02003-04-29 19:58:04 +0000175
Victor Stinnereccc5fa2010-07-24 00:49:20 +0000176 def test_issue4629(self):
177 longopts, shortopts = getopt.getopt(['--help='], '', ['help='])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000178 self.assertEqual(longopts, [('--help', '')])
Victor Stinnereccc5fa2010-07-24 00:49:20 +0000179 longopts, shortopts = getopt.getopt(['--help=x'], '', ['help='])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000180 self.assertEqual(longopts, [('--help', 'x')])
Victor Stinnereccc5fa2010-07-24 00:49:20 +0000181 self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help'])
Raymond Hettinger1a4a9d02003-04-29 19:58:04 +0000182
Guido van Rossumd8faa362007-04-27 19:54:29 +0000183def test_main():
184 run_unittest(GetoptTests)
Raymond Hettinger1a4a9d02003-04-29 19:58:04 +0000185
Guido van Rossumd8faa362007-04-27 19:54:29 +0000186if __name__ == "__main__":
187 test_main()