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 | |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame^] | 4 | from test.support import verbose, run_doctest, EnvironmentVarGuard |
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 |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 9 | sentinel = object() |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 11 | class GetoptTests(unittest.TestCase): |
| 12 | def setUp(self): |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 13 | self.env = EnvironmentVarGuard() |
| 14 | if "POSIXLY_CORRECT" in self.env: |
| 15 | del self.env["POSIXLY_CORRECT"] |
Fred Drake | 4354ba3 | 2004-08-03 15:54:45 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 17 | def tearDown(self): |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 18 | self.env.__exit__() |
| 19 | del self.env |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 21 | def assertError(self, *args, **kwargs): |
| 22 | self.assertRaises(getopt.GetoptError, *args, **kwargs) |
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 test_short_has_arg(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 25 | self.assertTrue(getopt.short_has_arg('a', 'a:')) |
| 26 | self.assertFalse(getopt.short_has_arg('a', 'a')) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 27 | self.assertError(getopt.short_has_arg, 'a', 'b') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 29 | def test_long_has_args(self): |
| 30 | has_arg, option = getopt.long_has_args('abc', ['abc=']) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 31 | self.assertTrue(has_arg) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 32 | self.assertEqual(option, 'abc') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 34 | has_arg, option = getopt.long_has_args('abc', ['abc']) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 35 | self.assertFalse(has_arg) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 36 | self.assertEqual(option, 'abc') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 38 | has_arg, option = getopt.long_has_args('abc', ['abcd']) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 39 | self.assertFalse(has_arg) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 40 | self.assertEqual(option, 'abcd') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 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']) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 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 | |
Georg Brandl | eee3116 | 2008-12-07 15:15:22 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 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 |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 135 | self.env["POSIXLY_CORRECT"] = "1" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 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: |
Fred Drake | 4354ba3 | 2004-08-03 15:54:45 +0000 | [diff] [blame] | 145 | |
| 146 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 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'] |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 157 | Using long option names is equally easy: |
Raymond Hettinger | 1a4a9d0 | 2003-04-29 19:58:04 +0000 | [diff] [blame] | 158 | |
| 159 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 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 | """ |
Raymond Hettinger | 1a4a9d0 | 2003-04-29 19:58:04 +0000 | [diff] [blame] | 171 | |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 172 | import types |
| 173 | m = types.ModuleType("libreftest", s) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 174 | run_doctest(m, verbose) |
Raymond Hettinger | 1a4a9d0 | 2003-04-29 19:58:04 +0000 | [diff] [blame] | 175 | |
Victor Stinner | eccc5fa | 2010-07-24 00:49:20 +0000 | [diff] [blame] | 176 | def test_issue4629(self): |
| 177 | longopts, shortopts = getopt.getopt(['--help='], '', ['help=']) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 178 | self.assertEqual(longopts, [('--help', '')]) |
Victor Stinner | eccc5fa | 2010-07-24 00:49:20 +0000 | [diff] [blame] | 179 | longopts, shortopts = getopt.getopt(['--help=x'], '', ['help=']) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 180 | self.assertEqual(longopts, [('--help', 'x')]) |
Victor Stinner | eccc5fa | 2010-07-24 00:49:20 +0000 | [diff] [blame] | 181 | self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help']) |
Raymond Hettinger | 1a4a9d0 | 2003-04-29 19:58:04 +0000 | [diff] [blame] | 182 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 183 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame^] | 184 | unittest.main() |