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 | |
| 4 | import getopt |
| 5 | from getopt import GetoptError |
Raymond Hettinger | 627728a | 2003-05-17 01:08:35 +0000 | [diff] [blame] | 6 | from test.test_support import verify, verbose, run_doctest |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 7 | import os |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 8 | |
| 9 | def expectException(teststr, expected, failure=AssertionError): |
| 10 | """Executes a statement passed in teststr, and raises an exception |
| 11 | (failure) if the expected exception is *not* raised.""" |
| 12 | try: |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame^] | 13 | exec(teststr) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 14 | except expected: |
| 15 | pass |
| 16 | else: |
| 17 | raise failure |
| 18 | |
Fred Drake | 4354ba3 | 2004-08-03 15:54:45 +0000 | [diff] [blame] | 19 | old_posixly_correct = os.environ.get("POSIXLY_CORRECT") |
| 20 | if old_posixly_correct is not None: |
| 21 | del os.environ["POSIXLY_CORRECT"] |
| 22 | |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 23 | if verbose: |
| 24 | print 'Running tests on getopt.short_has_arg' |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 25 | verify(getopt.short_has_arg('a', 'a:')) |
| 26 | verify(not getopt.short_has_arg('a', 'a')) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 27 | expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError) |
| 28 | expectException("tmp = getopt.short_has_arg('a', '')", GetoptError) |
| 29 | |
| 30 | if verbose: |
| 31 | print 'Running tests on getopt.long_has_args' |
| 32 | has_arg, option = getopt.long_has_args('abc', ['abc=']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 33 | verify(has_arg) |
| 34 | verify(option == 'abc') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 35 | has_arg, option = getopt.long_has_args('abc', ['abc']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 36 | verify(not has_arg) |
| 37 | verify(option == 'abc') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 38 | has_arg, option = getopt.long_has_args('abc', ['abcd']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 39 | verify(not has_arg) |
| 40 | verify(option == 'abcd') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 41 | expectException("has_arg, option = getopt.long_has_args('abc', ['def'])", |
| 42 | GetoptError) |
| 43 | expectException("has_arg, option = getopt.long_has_args('abc', [])", |
| 44 | GetoptError) |
| 45 | expectException("has_arg, option = " + \ |
| 46 | "getopt.long_has_args('abc', ['abcd','abcde'])", |
| 47 | GetoptError) |
| 48 | |
| 49 | if verbose: |
| 50 | print 'Running tests on getopt.do_shorts' |
| 51 | opts, args = getopt.do_shorts([], 'a', 'a', []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 52 | verify(opts == [('-a', '')]) |
| 53 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 54 | opts, args = getopt.do_shorts([], 'a1', 'a:', []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 55 | verify(opts == [('-a', '1')]) |
| 56 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 57 | #opts, args = getopt.do_shorts([], 'a=1', 'a:', []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 58 | #verify(opts == [('-a', '1')]) |
| 59 | #verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 60 | opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 61 | verify(opts == [('-a', '1')]) |
| 62 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 63 | opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 64 | verify(opts == [('-a', '1')]) |
| 65 | verify(args == ['2']) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 66 | expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])", |
| 67 | GetoptError) |
| 68 | expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])", |
| 69 | GetoptError) |
| 70 | |
| 71 | if verbose: |
| 72 | print 'Running tests on getopt.do_longs' |
| 73 | opts, args = getopt.do_longs([], 'abc', ['abc'], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 74 | verify(opts == [('--abc', '')]) |
| 75 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 76 | opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 77 | verify(opts == [('--abc', '1')]) |
| 78 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 79 | opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 80 | verify(opts == [('--abcd', '1')]) |
| 81 | verify(args == []) |
Tim Peters | da7bf4e | 2000-12-27 08:03:20 +0000 | [diff] [blame] | 82 | opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 83 | verify(opts == [('--abc', '')]) |
| 84 | verify(args == []) |
Tim Peters | da7bf4e | 2000-12-27 08:03:20 +0000 | [diff] [blame] | 85 | # Much like the preceding, except with a non-alpha character ("-") in |
| 86 | # option name that precedes "="; failed in |
| 87 | # http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470 |
| 88 | opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 89 | verify(opts == [('--foo', '42')]) |
| 90 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 91 | expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])", |
| 92 | GetoptError) |
| 93 | expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])", |
| 94 | GetoptError) |
| 95 | |
| 96 | # note: the empty string between '-a' and '--beta' is significant: |
| 97 | # it simulates an empty string option argument ('-a ""') on the command line. |
| 98 | cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '', |
| 99 | '--beta', 'arg1', 'arg2'] |
| 100 | |
| 101 | if verbose: |
| 102 | print 'Running tests on getopt.getopt' |
| 103 | opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 104 | verify(opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''), |
| 105 | ('-a', '3'), ('-a', ''), ('--beta', '')] ) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 106 | # Note ambiguity of ('-b', '') and ('-a', '') above. This must be |
| 107 | # accounted for in the code that calls getopt(). |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 108 | verify(args == ['arg1', 'arg2']) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 109 | |
| 110 | expectException( |
| 111 | "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])", |
| 112 | GetoptError) |
| 113 | |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 114 | # Test handling of GNU style scanning mode. |
| 115 | if verbose: |
| 116 | print 'Running tests on getopt.gnu_getopt' |
| 117 | cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2'] |
| 118 | # GNU style |
| 119 | opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) |
| 120 | verify(opts == [('-a', ''), ('-b', '1'), ('--alpha', ''), ('--beta', '2')]) |
| 121 | verify(args == ['arg1']) |
| 122 | # Posix style via + |
| 123 | opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta=']) |
| 124 | verify(opts == [('-a', '')]) |
| 125 | verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2']) |
| 126 | # Posix style via POSIXLY_CORRECT |
| 127 | os.environ["POSIXLY_CORRECT"] = "1" |
| 128 | opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) |
| 129 | verify(opts == [('-a', '')]) |
| 130 | verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2']) |
Fred Drake | 4354ba3 | 2004-08-03 15:54:45 +0000 | [diff] [blame] | 131 | |
| 132 | |
| 133 | if old_posixly_correct is None: |
| 134 | del os.environ["POSIXLY_CORRECT"] |
| 135 | else: |
| 136 | os.environ["POSIXLY_CORRECT"] = old_posixly_correct |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 137 | |
Raymond Hettinger | 1a4a9d0 | 2003-04-29 19:58:04 +0000 | [diff] [blame] | 138 | #------------------------------------------------------------------------------ |
| 139 | |
| 140 | libreftest = """ |
| 141 | Examples from the Library Reference: Doc/lib/libgetopt.tex |
| 142 | |
| 143 | An example using only Unix style options: |
| 144 | |
| 145 | |
| 146 | >>> import getopt |
| 147 | >>> args = '-a -b -cfoo -d bar a1 a2'.split() |
| 148 | >>> args |
| 149 | ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
| 150 | >>> optlist, args = getopt.getopt(args, 'abc:d:') |
| 151 | >>> optlist |
| 152 | [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
| 153 | >>> args |
| 154 | ['a1', 'a2'] |
| 155 | |
| 156 | Using long option names is equally easy: |
| 157 | |
| 158 | |
| 159 | >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' |
| 160 | >>> args = s.split() |
| 161 | >>> args |
| 162 | ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] |
| 163 | >>> optlist, args = getopt.getopt(args, 'x', [ |
| 164 | ... 'condition=', 'output-file=', 'testing']) |
| 165 | >>> optlist |
| 166 | [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')] |
| 167 | >>> args |
| 168 | ['a1', 'a2'] |
| 169 | |
| 170 | """ |
| 171 | |
| 172 | __test__ = {'libreftest' : libreftest} |
| 173 | |
Raymond Hettinger | 627728a | 2003-05-17 01:08:35 +0000 | [diff] [blame] | 174 | import sys |
| 175 | run_doctest(sys.modules[__name__], verbose) |
Raymond Hettinger | 1a4a9d0 | 2003-04-29 19:58:04 +0000 | [diff] [blame] | 176 | |
| 177 | #------------------------------------------------------------------------------ |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 178 | |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 179 | if verbose: |
| 180 | print "Module getopt: tests completed successfully." |