0001 # test_getopt.py 0002 # David Goodger <dgoodger@bigfoot.com> 2000-08-19 0003 0004 import getopt 0005 from getopt import GetoptError 0006 from test.test_support import verify, verbose, run_doctest 0007 import os 0008 0009 def expectException(teststr, expected, failure=AssertionError): 0010 """Executes a statement passed in teststr, and raises an exception 0011 (failure) if the expected exception is *not* raised.""" 0012 try: 0013 exec teststr 0014 except expected: 0015 pass 0016 else: 0017 raise failure 0018 0019 old_posixly_correct = os.environ.get("POSIXLY_CORRECT") 0020 if old_posixly_correct is not None: 0021 del os.environ["POSIXLY_CORRECT"] 0022 0023 if verbose: 0024 print 'Running tests on getopt.short_has_arg' 0025 verify(getopt.short_has_arg('a', 'a:')) 0026 verify(not getopt.short_has_arg('a', 'a')) 0027 expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError) 0028 expectException("tmp = getopt.short_has_arg('a', '')", GetoptError) 0029 0030 if verbose: 0031 print 'Running tests on getopt.long_has_args' 0032 has_arg, option = getopt.long_has_args('abc', ['abc=']) 0033 verify(has_arg) 0034 verify(option == 'abc') 0035 has_arg, option = getopt.long_has_args('abc', ['abc']) 0036 verify(not has_arg) 0037 verify(option == 'abc') 0038 has_arg, option = getopt.long_has_args('abc', ['abcd']) 0039 verify(not has_arg) 0040 verify(option == 'abcd') 0041 expectException("has_arg, option = getopt.long_has_args('abc', ['def'])", 0042 GetoptError) 0043 expectException("has_arg, option = getopt.long_has_args('abc', [])", 0044 GetoptError) 0045 expectException("has_arg, option = " + \ 0046 "getopt.long_has_args('abc', ['abcd','abcde'])", 0047 GetoptError) 0048 0049 if verbose: 0050 print 'Running tests on getopt.do_shorts' 0051 opts, args = getopt.do_shorts([], 'a', 'a', []) 0052 verify(opts == [('-a', '')]) 0053 verify(args == []) 0054 opts, args = getopt.do_shorts([], 'a1', 'a:', []) 0055 verify(opts == [('-a', '1')]) 0056 verify(args == []) 0057 #opts, args = getopt.do_shorts([], 'a=1', 'a:', []) 0058 #verify(opts == [('-a', '1')]) 0059 #verify(args == []) 0060 opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) 0061 verify(opts == [('-a', '1')]) 0062 verify(args == []) 0063 opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) 0064 verify(opts == [('-a', '1')]) 0065 verify(args == ['2']) 0066 expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])", 0067 GetoptError) 0068 expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])", 0069 GetoptError) 0070 0071 if verbose: 0072 print 'Running tests on getopt.do_longs' 0073 opts, args = getopt.do_longs([], 'abc', ['abc'], []) 0074 verify(opts == [('--abc', '')]) 0075 verify(args == []) 0076 opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) 0077 verify(opts == [('--abc', '1')]) 0078 verify(args == []) 0079 opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) 0080 verify(opts == [('--abcd', '1')]) 0081 verify(args == []) 0082 opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) 0083 verify(opts == [('--abc', '')]) 0084 verify(args == []) 0085 # Much like the preceding, except with a non-alpha character ("-") in 0086 # option name that precedes "="; failed in 0087 # http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470 0088 opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], []) 0089 verify(opts == [('--foo', '42')]) 0090 verify(args == []) 0091 expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])", 0092 GetoptError) 0093 expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])", 0094 GetoptError) 0095 0096 # note: the empty string between '-a' and '--beta' is significant: 0097 # it simulates an empty string option argument ('-a ""') on the command line. 0098 cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '', 0099 '--beta', 'arg1', 'arg2'] 0100 0101 if verbose: 0102 print 'Running tests on getopt.getopt' 0103 opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta']) 0104 verify(opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''), 0105 ('-a', '3'), ('-a', ''), ('--beta', '')] ) 0106 # Note ambiguity of ('-b', '') and ('-a', '') above. This must be 0107 # accounted for in the code that calls getopt(). 0108 verify(args == ['arg1', 'arg2']) 0109 0110 expectException( 0111 "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])", 0112 GetoptError) 0113 0114 # Test handling of GNU style scanning mode. 0115 if verbose: 0116 print 'Running tests on getopt.gnu_getopt' 0117 cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2'] 0118 # GNU style 0119 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) 0120 verify(opts == [('-a', ''), ('-b', '1'), ('--alpha', ''), ('--beta', '2')]) 0121 verify(args == ['arg1']) 0122 # Posix style via + 0123 opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta=']) 0124 verify(opts == [('-a', '')]) 0125 verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2']) 0126 # Posix style via POSIXLY_CORRECT 0127 os.environ["POSIXLY_CORRECT"] = "1" 0128 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) 0129 verify(opts == [('-a', '')]) 0130 verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2']) 0131 0132 0133 if old_posixly_correct is None: 0134 del os.environ["POSIXLY_CORRECT"] 0135 else: 0136 os.environ["POSIXLY_CORRECT"] = old_posixly_correct 0137 0138 #------------------------------------------------------------------------------ 0139 0140 libreftest = """ 0141 Examples from the Library Reference: Doc/lib/libgetopt.tex 0142 0143 An example using only Unix style options: 0144 0145 0146 >>> import getopt 0147 >>> args = '-a -b -cfoo -d bar a1 a2'.split() 0148 >>> args 0149 ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] 0150 >>> optlist, args = getopt.getopt(args, 'abc:d:') 0151 >>> optlist 0152 [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] 0153 >>> args 0154 ['a1', 'a2'] 0155 0156 Using long option names is equally easy: 0157 0158 0159 >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' 0160 >>> args = s.split() 0161 >>> args 0162 ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] 0163 >>> optlist, args = getopt.getopt(args, 'x', [ 0164 ... 'condition=', 'output-file=', 'testing']) 0165 >>> optlist 0166 [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')] 0167 >>> args 0168 ['a1', 'a2'] 0169 0170 """ 0171 0172 __test__ = {'libreftest' : libreftest} 0173 0174 import sys 0175 run_doctest(sys.modules[__name__], verbose) 0176 0177 #------------------------------------------------------------------------------ 0178 0179 if verbose: 0180 print "Module getopt: tests completed successfully." 0181
Generated by PyXR 0.9.4