2 # David Goodger <dgoodger@bigfoot.com> 2000-08-19
4 from test
.test_support
import verbose
, run_doctest
, run_unittest
, EnvironmentVarGuard
11 class GetoptTests(unittest
.TestCase
):
13 self
.env
= EnvironmentVarGuard()
14 if "POSIXLY_CORRECT" in self
.env
:
15 del self
.env
["POSIXLY_CORRECT"]
21 def assertError(self
, *args
, **kwargs
):
22 self
.assertRaises(getopt
.GetoptError
, *args
, **kwargs
)
24 def test_short_has_arg(self
):
25 self
.assertTrue(getopt
.short_has_arg('a', 'a:'))
26 self
.assertFalse(getopt
.short_has_arg('a', 'a'))
27 self
.assertError(getopt
.short_has_arg
, 'a', 'b')
29 def test_long_has_args(self
):
30 has_arg
, option
= getopt
.long_has_args('abc', ['abc='])
31 self
.assertTrue(has_arg
)
32 self
.assertEqual(option
, 'abc')
34 has_arg
, option
= getopt
.long_has_args('abc', ['abc'])
35 self
.assertFalse(has_arg
)
36 self
.assertEqual(option
, 'abc')
38 has_arg
, option
= getopt
.long_has_args('abc', ['abcd'])
39 self
.assertFalse(has_arg
)
40 self
.assertEqual(option
, 'abcd')
42 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'])
46 def test_do_shorts(self
):
47 opts
, args
= getopt
.do_shorts([], 'a', 'a', [])
48 self
.assertEqual(opts
, [('-a', '')])
49 self
.assertEqual(args
, [])
51 opts
, args
= getopt
.do_shorts([], 'a1', 'a:', [])
52 self
.assertEqual(opts
, [('-a', '1')])
53 self
.assertEqual(args
, [])
55 #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
56 #self.assertEqual(opts, [('-a', '1')])
57 #self.assertEqual(args, [])
59 opts
, args
= getopt
.do_shorts([], 'a', 'a:', ['1'])
60 self
.assertEqual(opts
, [('-a', '1')])
61 self
.assertEqual(args
, [])
63 opts
, args
= getopt
.do_shorts([], 'a', 'a:', ['1', '2'])
64 self
.assertEqual(opts
, [('-a', '1')])
65 self
.assertEqual(args
, ['2'])
67 self
.assertError(getopt
.do_shorts
, [], 'a1', 'a', [])
68 self
.assertError(getopt
.do_shorts
, [], 'a', 'a:', [])
70 def test_do_longs(self
):
71 opts
, args
= getopt
.do_longs([], 'abc', ['abc'], [])
72 self
.assertEqual(opts
, [('--abc', '')])
73 self
.assertEqual(args
, [])
75 opts
, args
= getopt
.do_longs([], 'abc=1', ['abc='], [])
76 self
.assertEqual(opts
, [('--abc', '1')])
77 self
.assertEqual(args
, [])
79 opts
, args
= getopt
.do_longs([], 'abc=1', ['abcd='], [])
80 self
.assertEqual(opts
, [('--abcd', '1')])
81 self
.assertEqual(args
, [])
83 opts
, args
= getopt
.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
84 self
.assertEqual(opts
, [('--abc', '')])
85 self
.assertEqual(args
, [])
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
, [])
94 self
.assertError(getopt
.do_longs
, [], 'abc=1', ['abc'], [])
95 self
.assertError(getopt
.do_longs
, [], 'abc', ['abc='], [])
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
101 cmdline
= ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a',
102 '', '--beta', 'arg1', 'arg2']
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'])
112 self
.assertError(getopt
.getopt
, cmdline
, 'a:b', ['alpha', 'beta'])
114 def test_gnu_getopt(self
):
115 # Test handling of GNU style scanning mode.
116 cmdline
= ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
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')])
124 # recognize "-" as an argument
125 opts
, args
= getopt
.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', [])
126 self
.assertEqual(args
, ['-'])
127 self
.assertEqual(opts
, [('-a', ''), ('-b', '-')])
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'])
134 # Posix style via POSIXLY_CORRECT
135 self
.env
["POSIXLY_CORRECT"] = "1"
136 opts
, args
= getopt
.gnu_getopt(cmdline
, 'ab:', ['alpha', 'beta='])
137 self
.assertEqual(opts
, [('-a', '')])
138 self
.assertEqual(args
, ['arg1', '-b', '1', '--alpha', '--beta=2'])
140 def test_libref_examples(self
):
142 Examples from the Library Reference: Doc/lib/libgetopt.tex
144 An example using only Unix style options:
148 >>> args = '-a -b -cfoo -d bar a1 a2'.split()
150 ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
151 >>> optlist, args = getopt.getopt(args, 'abc:d:')
153 [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
157 Using long option names is equally easy:
160 >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
163 ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
164 >>> optlist, args = getopt.getopt(args, 'x', [
165 ... 'condition=', 'output-file=', 'testing'])
167 [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
173 m
= types
.ModuleType("libreftest", s
)
174 run_doctest(m
, verbose
)
178 run_unittest(GetoptTests
)
180 if __name__
== "__main__":