Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | # test_getopt.py |
2 | # David Goodger <dgoodger@bigfoot.com> 2000-08-19 | |
3 | ||
4 | import getopt | |
5 | from getopt import GetoptError | |
6 | from test.test_support import verify, verbose, run_doctest | |
7 | import os | |
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: | |
13 | exec teststr | |
14 | except expected: | |
15 | pass | |
16 | else: | |
17 | raise failure | |
18 | ||
19 | old_posixly_correct = os.environ.get("POSIXLY_CORRECT") | |
20 | if old_posixly_correct is not None: | |
21 | del os.environ["POSIXLY_CORRECT"] | |
22 | ||
23 | if verbose: | |
24 | print 'Running tests on getopt.short_has_arg' | |
25 | verify(getopt.short_has_arg('a', 'a:')) | |
26 | verify(not getopt.short_has_arg('a', 'a')) | |
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=']) | |
33 | verify(has_arg) | |
34 | verify(option == 'abc') | |
35 | has_arg, option = getopt.long_has_args('abc', ['abc']) | |
36 | verify(not has_arg) | |
37 | verify(option == 'abc') | |
38 | has_arg, option = getopt.long_has_args('abc', ['abcd']) | |
39 | verify(not has_arg) | |
40 | verify(option == 'abcd') | |
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', []) | |
52 | verify(opts == [('-a', '')]) | |
53 | verify(args == []) | |
54 | opts, args = getopt.do_shorts([], 'a1', 'a:', []) | |
55 | verify(opts == [('-a', '1')]) | |
56 | verify(args == []) | |
57 | #opts, args = getopt.do_shorts([], 'a=1', 'a:', []) | |
58 | #verify(opts == [('-a', '1')]) | |
59 | #verify(args == []) | |
60 | opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) | |
61 | verify(opts == [('-a', '1')]) | |
62 | verify(args == []) | |
63 | opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) | |
64 | verify(opts == [('-a', '1')]) | |
65 | verify(args == ['2']) | |
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'], []) | |
74 | verify(opts == [('--abc', '')]) | |
75 | verify(args == []) | |
76 | opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) | |
77 | verify(opts == [('--abc', '1')]) | |
78 | verify(args == []) | |
79 | opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) | |
80 | verify(opts == [('--abcd', '1')]) | |
81 | verify(args == []) | |
82 | opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) | |
83 | verify(opts == [('--abc', '')]) | |
84 | verify(args == []) | |
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=',], []) | |
89 | verify(opts == [('--foo', '42')]) | |
90 | verify(args == []) | |
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']) | |
104 | verify(opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''), | |
105 | ('-a', '3'), ('-a', ''), ('--beta', '')] ) | |
106 | # Note ambiguity of ('-b', '') and ('-a', '') above. This must be | |
107 | # accounted for in the code that calls getopt(). | |
108 | verify(args == ['arg1', 'arg2']) | |
109 | ||
110 | expectException( | |
111 | "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])", | |
112 | GetoptError) | |
113 | ||
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']) | |
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 | |
137 | ||
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 | ||
174 | import sys | |
175 | run_doctest(sys.modules[__name__], verbose) | |
176 | ||
177 | #------------------------------------------------------------------------------ | |
178 | ||
179 | if verbose: | |
180 | print "Module getopt: tests completed successfully." |