2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# test_getopt.py
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# David Goodger <dgoodger@bigfoot.com> 2000-08-19
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import getopt
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								from getopt import GetoptError
							 | 
						
					
						
							
								
									
										
										
										
											2002-07-23 19:04:11 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								from test.test_support import verify, verbose
							 | 
						
					
						
							
								
									
										
										
										
											2002-06-06 10:58:36 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import os
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								def expectException(teststr, expected, failure=AssertionError):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    """Executes a statement passed in teststr, and raises an exception
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								       (failure) if the expected exception is *not* raised."""
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    try:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        exec teststr
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    except expected:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        pass
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        raise failure
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								if verbose:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    print 'Running tests on getopt.short_has_arg'
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(getopt.short_has_arg('a', 'a:'))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(not getopt.short_has_arg('a', 'a'))
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								expectException("tmp = getopt.short_has_arg('a', '')", GetoptError)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								if verbose:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    print 'Running tests on getopt.long_has_args'
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								has_arg, option = getopt.long_has_args('abc', ['abc='])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(has_arg)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(option == 'abc')
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								has_arg, option = getopt.long_has_args('abc', ['abc'])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(not has_arg)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(option == 'abc')
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								has_arg, option = getopt.long_has_args('abc', ['abcd'])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(not has_arg)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(option == 'abcd')
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								expectException("has_arg, option = getopt.long_has_args('abc', ['def'])",
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                GetoptError)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								expectException("has_arg, option = getopt.long_has_args('abc', [])",
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                GetoptError)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								expectException("has_arg, option = " + \
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                     "getopt.long_has_args('abc', ['abcd','abcde'])",
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                GetoptError)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								if verbose:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    print 'Running tests on getopt.do_shorts'
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								opts, args = getopt.do_shorts([], 'a', 'a', [])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(opts == [('-a', '')])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(args == [])
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								opts, args = getopt.do_shorts([], 'a1', 'a:', [])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(opts == [('-a', '1')])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(args == [])
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#verify(opts == [('-a', '1')])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#verify(args == [])
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(opts == [('-a', '1')])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(args == [])
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(opts == [('-a', '1')])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(args == ['2'])
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])",
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                GetoptError)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])",
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                GetoptError)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								if verbose:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    print 'Running tests on getopt.do_longs'
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								opts, args = getopt.do_longs([], 'abc', ['abc'], [])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(opts == [('--abc', '')])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(args == [])
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(opts == [('--abc', '1')])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(args == [])
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(opts == [('--abcd', '1')])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(args == [])
							 | 
						
					
						
							
								
									
										
										
										
											2000-12-27 08:03:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(opts == [('--abc', '')])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(args == [])
							 | 
						
					
						
							
								
									
										
										
										
											2000-12-27 08:03:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								# Much like the preceding, except with a non-alpha character ("-") in
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# option name that precedes "="; failed in
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(opts == [('--foo', '42')])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(args == [])
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])",
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                GetoptError)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])",
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                GetoptError)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# note: the empty string between '-a' and '--beta' is significant:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# it simulates an empty string option argument ('-a ""') on the command line.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '',
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								           '--beta', 'arg1', 'arg2']
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								if verbose:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    print 'Running tests on getopt.getopt'
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''),
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                ('-a', '3'), ('-a', ''), ('--beta', '')] )
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# Note ambiguity of ('-b', '') and ('-a', '') above. This must be
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# accounted for in the code that calls getopt().
							 | 
						
					
						
							
								
									
										
										
										
											2001-01-17 19:11:13 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								verify(args == ['arg1', 'arg2'])
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								expectException(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])",
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    GetoptError)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2002-06-06 10:58:36 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								# Test handling of GNU style scanning mode.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								if verbose:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    print 'Running tests on getopt.gnu_getopt'
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# GNU style
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(opts == [('-a', ''), ('-b', '1'), ('--alpha', ''), ('--beta', '2')])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(args == ['arg1'])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# Posix style via +
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(opts == [('-a', '')])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2'])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# Posix style via POSIXLY_CORRECT
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								os.environ["POSIXLY_CORRECT"] = "1"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(opts == [('-a', '')])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2'])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2000-08-20 04:18:40 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								if verbose:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    print "Module getopt: tests completed successfully."
							 |