blob: e2ae9fed418f55cc81915b5ccfae8d891875a1f4 [file] [log] [blame]
Tim Peters571bb8f2000-08-20 04:18:40 +00001# test_getopt.py
2# David Goodger <dgoodger@bigfoot.com> 2000-08-19
3
4import getopt
5from getopt import GetoptError
Marc-André Lemburg36619082001-01-17 19:11:13 +00006from test_support import verify, verbose
Martin v. Löwis446a25f2002-06-06 10:58:36 +00007import os
Tim Peters571bb8f2000-08-20 04:18:40 +00008
9def 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
19if verbose:
20 print 'Running tests on getopt.short_has_arg'
Marc-André Lemburg36619082001-01-17 19:11:13 +000021verify(getopt.short_has_arg('a', 'a:'))
22verify(not getopt.short_has_arg('a', 'a'))
Tim Peters571bb8f2000-08-20 04:18:40 +000023expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError)
24expectException("tmp = getopt.short_has_arg('a', '')", GetoptError)
25
26if verbose:
27 print 'Running tests on getopt.long_has_args'
28has_arg, option = getopt.long_has_args('abc', ['abc='])
Marc-André Lemburg36619082001-01-17 19:11:13 +000029verify(has_arg)
30verify(option == 'abc')
Tim Peters571bb8f2000-08-20 04:18:40 +000031has_arg, option = getopt.long_has_args('abc', ['abc'])
Marc-André Lemburg36619082001-01-17 19:11:13 +000032verify(not has_arg)
33verify(option == 'abc')
Tim Peters571bb8f2000-08-20 04:18:40 +000034has_arg, option = getopt.long_has_args('abc', ['abcd'])
Marc-André Lemburg36619082001-01-17 19:11:13 +000035verify(not has_arg)
36verify(option == 'abcd')
Tim Peters571bb8f2000-08-20 04:18:40 +000037expectException("has_arg, option = getopt.long_has_args('abc', ['def'])",
38 GetoptError)
39expectException("has_arg, option = getopt.long_has_args('abc', [])",
40 GetoptError)
41expectException("has_arg, option = " + \
42 "getopt.long_has_args('abc', ['abcd','abcde'])",
43 GetoptError)
44
45if verbose:
46 print 'Running tests on getopt.do_shorts'
47opts, args = getopt.do_shorts([], 'a', 'a', [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000048verify(opts == [('-a', '')])
49verify(args == [])
Tim Peters571bb8f2000-08-20 04:18:40 +000050opts, args = getopt.do_shorts([], 'a1', 'a:', [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000051verify(opts == [('-a', '1')])
52verify(args == [])
Tim Peters571bb8f2000-08-20 04:18:40 +000053#opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000054#verify(opts == [('-a', '1')])
55#verify(args == [])
Tim Peters571bb8f2000-08-20 04:18:40 +000056opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
Marc-André Lemburg36619082001-01-17 19:11:13 +000057verify(opts == [('-a', '1')])
58verify(args == [])
Tim Peters571bb8f2000-08-20 04:18:40 +000059opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
Marc-André Lemburg36619082001-01-17 19:11:13 +000060verify(opts == [('-a', '1')])
61verify(args == ['2'])
Tim Peters571bb8f2000-08-20 04:18:40 +000062expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])",
63 GetoptError)
64expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])",
65 GetoptError)
66
67if verbose:
68 print 'Running tests on getopt.do_longs'
69opts, args = getopt.do_longs([], 'abc', ['abc'], [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000070verify(opts == [('--abc', '')])
71verify(args == [])
Tim Peters571bb8f2000-08-20 04:18:40 +000072opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000073verify(opts == [('--abc', '1')])
74verify(args == [])
Tim Peters571bb8f2000-08-20 04:18:40 +000075opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000076verify(opts == [('--abcd', '1')])
77verify(args == [])
Tim Petersda7bf4e2000-12-27 08:03:20 +000078opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000079verify(opts == [('--abc', '')])
80verify(args == [])
Tim Petersda7bf4e2000-12-27 08:03:20 +000081# Much like the preceding, except with a non-alpha character ("-") in
82# option name that precedes "="; failed in
83# http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470
84opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000085verify(opts == [('--foo', '42')])
86verify(args == [])
Tim Peters571bb8f2000-08-20 04:18:40 +000087expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])",
88 GetoptError)
89expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])",
90 GetoptError)
91
92# note: the empty string between '-a' and '--beta' is significant:
93# it simulates an empty string option argument ('-a ""') on the command line.
94cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '',
95 '--beta', 'arg1', 'arg2']
96
97if verbose:
98 print 'Running tests on getopt.getopt'
99opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
Marc-André Lemburg36619082001-01-17 19:11:13 +0000100verify(opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''),
101 ('-a', '3'), ('-a', ''), ('--beta', '')] )
Tim Peters571bb8f2000-08-20 04:18:40 +0000102# Note ambiguity of ('-b', '') and ('-a', '') above. This must be
103# accounted for in the code that calls getopt().
Marc-André Lemburg36619082001-01-17 19:11:13 +0000104verify(args == ['arg1', 'arg2'])
Tim Peters571bb8f2000-08-20 04:18:40 +0000105
106expectException(
107 "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])",
108 GetoptError)
109
Martin v. Löwis446a25f2002-06-06 10:58:36 +0000110# Test handling of GNU style scanning mode.
111if verbose:
112 print 'Running tests on getopt.gnu_getopt'
113cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
114# GNU style
115opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
116verify(opts == [('-a', ''), ('-b', '1'), ('--alpha', ''), ('--beta', '2')])
117verify(args == ['arg1'])
118# Posix style via +
119opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
120verify(opts == [('-a', '')])
121verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2'])
122# Posix style via POSIXLY_CORRECT
123os.environ["POSIXLY_CORRECT"] = "1"
124opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
125verify(opts == [('-a', '')])
126verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2'])
127
128
Tim Peters571bb8f2000-08-20 04:18:40 +0000129if verbose:
130 print "Module getopt: tests completed successfully."