Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 1 | # test_getopt.py |
| 2 | # David Goodger <dgoodger@bigfoot.com> 2000-08-19 |
| 3 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test.support import verbose, run_doctest, run_unittest |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5 | import unittest |
| 6 | |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 7 | import getopt |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 8 | import os |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 10 | sentinel = object() |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 12 | class GetoptTests(unittest.TestCase): |
| 13 | def setUp(self): |
| 14 | self.old_posixly_correct = os.environ.get("POSIXLY_CORRECT", sentinel) |
| 15 | if self.old_posixly_correct is not sentinel: |
| 16 | del os.environ["POSIXLY_CORRECT"] |
Fred Drake | 4354ba3 | 2004-08-03 15:54:45 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 18 | def tearDown(self): |
| 19 | if self.old_posixly_correct is sentinel: |
| 20 | os.environ.pop("POSIXLY_CORRECT", None) |
| 21 | else: |
| 22 | os.environ["POSIXLY_CORRECT"] = self.old_posixly_correct |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 24 | def assertError(self, *args, **kwargs): |
| 25 | self.assertRaises(getopt.GetoptError, *args, **kwargs) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 27 | def test_short_has_arg(self): |
| 28 | self.failUnless(getopt.short_has_arg('a', 'a:')) |
| 29 | self.failIf(getopt.short_has_arg('a', 'a')) |
| 30 | self.assertError(getopt.short_has_arg, 'a', 'b') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 32 | def test_long_has_args(self): |
| 33 | has_arg, option = getopt.long_has_args('abc', ['abc=']) |
| 34 | self.failUnless(has_arg) |
| 35 | self.assertEqual(option, 'abc') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 37 | has_arg, option = getopt.long_has_args('abc', ['abc']) |
| 38 | self.failIf(has_arg) |
| 39 | self.assertEqual(option, 'abc') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 41 | has_arg, option = getopt.long_has_args('abc', ['abcd']) |
| 42 | self.failIf(has_arg) |
| 43 | self.assertEqual(option, 'abcd') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 45 | self.assertError(getopt.long_has_args, 'abc', ['def']) |
| 46 | self.assertError(getopt.long_has_args, 'abc', []) |
| 47 | self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde']) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 49 | def test_do_shorts(self): |
| 50 | opts, args = getopt.do_shorts([], 'a', 'a', []) |
| 51 | self.assertEqual(opts, [('-a', '')]) |
| 52 | self.assertEqual(args, []) |
| 53 | |
| 54 | opts, args = getopt.do_shorts([], 'a1', 'a:', []) |
| 55 | self.assertEqual(opts, [('-a', '1')]) |
| 56 | self.assertEqual(args, []) |
| 57 | |
| 58 | #opts, args = getopt.do_shorts([], 'a=1', 'a:', []) |
| 59 | #self.assertEqual(opts, [('-a', '1')]) |
| 60 | #self.assertEqual(args, []) |
| 61 | |
| 62 | opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) |
| 63 | self.assertEqual(opts, [('-a', '1')]) |
| 64 | self.assertEqual(args, []) |
| 65 | |
| 66 | opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) |
| 67 | self.assertEqual(opts, [('-a', '1')]) |
| 68 | self.assertEqual(args, ['2']) |
| 69 | |
| 70 | self.assertError(getopt.do_shorts, [], 'a1', 'a', []) |
| 71 | self.assertError(getopt.do_shorts, [], 'a', 'a:', []) |
| 72 | |
| 73 | def test_do_longs(self): |
| 74 | opts, args = getopt.do_longs([], 'abc', ['abc'], []) |
| 75 | self.assertEqual(opts, [('--abc', '')]) |
| 76 | self.assertEqual(args, []) |
| 77 | |
| 78 | opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) |
| 79 | self.assertEqual(opts, [('--abc', '1')]) |
| 80 | self.assertEqual(args, []) |
| 81 | |
| 82 | opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) |
| 83 | self.assertEqual(opts, [('--abcd', '1')]) |
| 84 | self.assertEqual(args, []) |
| 85 | |
| 86 | opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) |
| 87 | self.assertEqual(opts, [('--abc', '')]) |
| 88 | self.assertEqual(args, []) |
| 89 | |
| 90 | # Much like the preceding, except with a non-alpha character ("-") in |
| 91 | # option name that precedes "="; failed in |
| 92 | # http://python.org/sf/126863 |
| 93 | opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], []) |
| 94 | self.assertEqual(opts, [('--foo', '42')]) |
| 95 | self.assertEqual(args, []) |
| 96 | |
| 97 | self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], []) |
| 98 | self.assertError(getopt.do_longs, [], 'abc', ['abc='], []) |
| 99 | |
| 100 | def test_getopt(self): |
| 101 | # note: the empty string between '-a' and '--beta' is significant: |
| 102 | # it simulates an empty string option argument ('-a ""') on the |
| 103 | # command line. |
| 104 | cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', |
| 105 | '', '--beta', 'arg1', 'arg2'] |
| 106 | |
| 107 | opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta']) |
| 108 | self.assertEqual(opts, [('-a', '1'), ('-b', ''), |
| 109 | ('--alpha', '2'), ('--beta', ''), |
| 110 | ('-a', '3'), ('-a', ''), ('--beta', '')]) |
| 111 | # Note ambiguity of ('-b', '') and ('-a', '') above. This must be |
| 112 | # accounted for in the code that calls getopt(). |
| 113 | self.assertEqual(args, ['arg1', 'arg2']) |
| 114 | |
| 115 | self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta']) |
| 116 | |
| 117 | def test_gnu_getopt(self): |
| 118 | # Test handling of GNU style scanning mode. |
| 119 | cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2'] |
| 120 | |
| 121 | # GNU style |
| 122 | opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) |
| 123 | self.assertEqual(args, ['arg1']) |
| 124 | self.assertEqual(opts, [('-a', ''), ('-b', '1'), |
| 125 | ('--alpha', ''), ('--beta', '2')]) |
| 126 | |
| 127 | # Posix style via + |
| 128 | opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta=']) |
| 129 | self.assertEqual(opts, [('-a', '')]) |
| 130 | self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2']) |
| 131 | |
| 132 | # Posix style via POSIXLY_CORRECT |
| 133 | os.environ["POSIXLY_CORRECT"] = "1" |
| 134 | opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) |
| 135 | self.assertEqual(opts, [('-a', '')]) |
| 136 | self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2']) |
| 137 | |
| 138 | def test_libref_examples(self): |
| 139 | s = """ |
| 140 | Examples from the Library Reference: Doc/lib/libgetopt.tex |
| 141 | |
| 142 | An example using only Unix style options: |
Fred Drake | 4354ba3 | 2004-08-03 15:54:45 +0000 | [diff] [blame] | 143 | |
| 144 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 145 | >>> import getopt |
| 146 | >>> args = '-a -b -cfoo -d bar a1 a2'.split() |
| 147 | >>> args |
| 148 | ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
| 149 | >>> optlist, args = getopt.getopt(args, 'abc:d:') |
| 150 | >>> optlist |
| 151 | [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
| 152 | >>> args |
| 153 | ['a1', 'a2'] |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 154 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 155 | Using long option names is equally easy: |
Raymond Hettinger | 1a4a9d0 | 2003-04-29 19:58:04 +0000 | [diff] [blame] | 156 | |
| 157 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 158 | >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' |
| 159 | >>> args = s.split() |
| 160 | >>> args |
| 161 | ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] |
| 162 | >>> optlist, args = getopt.getopt(args, 'x', [ |
| 163 | ... 'condition=', 'output-file=', 'testing']) |
| 164 | >>> optlist |
| 165 | [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')] |
| 166 | >>> args |
| 167 | ['a1', 'a2'] |
| 168 | """ |
Raymond Hettinger | 1a4a9d0 | 2003-04-29 19:58:04 +0000 | [diff] [blame] | 169 | |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 170 | import types |
| 171 | m = types.ModuleType("libreftest", s) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 172 | run_doctest(m, verbose) |
Raymond Hettinger | 1a4a9d0 | 2003-04-29 19:58:04 +0000 | [diff] [blame] | 173 | |
| 174 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 175 | def test_main(): |
| 176 | run_unittest(GetoptTests) |
Raymond Hettinger | 1a4a9d0 | 2003-04-29 19:58:04 +0000 | [diff] [blame] | 177 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 178 | if __name__ == "__main__": |
| 179 | test_main() |