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