1 # test_getopt.py 2 # David Goodger <dgoodger (at] bigfoot.com> 2000-08-19 3 4 from test.test_support import verbose, run_doctest, run_unittest, EnvironmentVarGuard 5 import unittest 6 7 import getopt 8 9 sentinel = object() 10 11 class GetoptTests(unittest.TestCase): 12 def setUp(self): 13 self.env = EnvironmentVarGuard() 14 if "POSIXLY_CORRECT" in self.env: 15 del self.env["POSIXLY_CORRECT"] 16 17 def tearDown(self): 18 self.env.__exit__() 19 del self.env 20 21 def assertError(self, *args, **kwargs): 22 self.assertRaises(getopt.GetoptError, *args, **kwargs) 23 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') 28 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') 33 34 has_arg, option = getopt.long_has_args('abc', ['abc']) 35 self.assertFalse(has_arg) 36 self.assertEqual(option, 'abc') 37 38 has_arg, option = getopt.long_has_args('abc', ['abcd']) 39 self.assertFalse(has_arg) 40 self.assertEqual(option, 'abcd') 41 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']) 45 46 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 124 # recognize "-" as an argument 125 opts, args = getopt.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', []) 126 self.assertEqual(args, ['-']) 127 self.assertEqual(opts, [('-a', ''), ('-b', '-')]) 128 129 # 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 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']) 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: 145 146 147 >>> 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'] 156 157 Using long option names is equally easy: 158 159 160 >>> 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 """ 171 172 import types 173 m = types.ModuleType("libreftest", s) 174 run_doctest(m, verbose) 175 176 def test_issue4629(self): 177 longopts, shortopts = getopt.getopt(['--help='], '', ['help=']) 178 self.assertEqual(longopts, [('--help', '')]) 179 longopts, shortopts = getopt.getopt(['--help=x'], '', ['help=']) 180 self.assertEqual(longopts, [('--help', 'x')]) 181 self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help']) 182 183 def test_main(): 184 run_unittest(GetoptTests) 185 186 if __name__ == "__main__": 187 test_main() 188