@@ -19,21 +19,34 @@ def assertError(self, *args, **kwargs):
1919 self .assertRaises (getopt .GetoptError , * args , ** kwargs )
2020
2121 def test_short_has_arg (self ):
22- self .assertTrue (getopt .short_has_arg ('a' , 'a:' ))
23- self .assertFalse (getopt .short_has_arg ('a' , 'a' ))
22+ self .assertIs (getopt .short_has_arg ('a' , 'a:' ), True )
23+ self .assertIs (getopt .short_has_arg ('a' , 'a' ), False )
24+ self .assertEqual (getopt .short_has_arg ('a' , 'a::' ), '?' )
2425 self .assertError (getopt .short_has_arg , 'a' , 'b' )
2526
2627 def test_long_has_args (self ):
2728 has_arg , option = getopt .long_has_args ('abc' , ['abc=' ])
28- self .assertTrue (has_arg )
29+ self .assertIs (has_arg , True )
2930 self .assertEqual (option , 'abc' )
3031
3132 has_arg , option = getopt .long_has_args ('abc' , ['abc' ])
32- self .assertFalse (has_arg )
33+ self .assertIs (has_arg , False )
3334 self .assertEqual (option , 'abc' )
3435
36+ has_arg , option = getopt .long_has_args ('abc' , ['abc=?' ])
37+ self .assertEqual (has_arg , '?' )
38+ self .assertEqual (option , 'abc' )
39+
40+ has_arg , option = getopt .long_has_args ('abc' , ['abcd=' ])
41+ self .assertIs (has_arg , True )
42+ self .assertEqual (option , 'abcd' )
43+
3544 has_arg , option = getopt .long_has_args ('abc' , ['abcd' ])
36- self .assertFalse (has_arg )
45+ self .assertIs (has_arg , False )
46+ self .assertEqual (option , 'abcd' )
47+
48+ has_arg , option = getopt .long_has_args ('abc' , ['abcd=?' ])
49+ self .assertEqual (has_arg , '?' )
3750 self .assertEqual (option , 'abcd' )
3851
3952 self .assertError (getopt .long_has_args , 'abc' , ['def' ])
@@ -49,9 +62,9 @@ def test_do_shorts(self):
4962 self .assertEqual (opts , [('-a' , '1' )])
5063 self .assertEqual (args , [])
5164
52- # opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
53- # self.assertEqual(opts, [('-a', '1')])
54- # self.assertEqual(args, [])
65+ opts , args = getopt .do_shorts ([], 'a=1' , 'a:' , [])
66+ self .assertEqual (opts , [('-a' , '= 1' )])
67+ self .assertEqual (args , [])
5568
5669 opts , args = getopt .do_shorts ([], 'a' , 'a:' , ['1' ])
5770 self .assertEqual (opts , [('-a' , '1' )])
@@ -61,6 +74,14 @@ def test_do_shorts(self):
6174 self .assertEqual (opts , [('-a' , '1' )])
6275 self .assertEqual (args , ['2' ])
6376
77+ opts , args = getopt .do_shorts ([], 'a' , 'a::' , ['1' ])
78+ self .assertEqual (opts , [('-a' , '' )])
79+ self .assertEqual (args , ['1' ])
80+
81+ opts , args = getopt .do_shorts ([], 'a1' , 'a::' , [])
82+ self .assertEqual (opts , [('-a' , '1' )])
83+ self .assertEqual (args , [])
84+
6485 self .assertError (getopt .do_shorts , [], 'a1' , 'a' , [])
6586 self .assertError (getopt .do_shorts , [], 'a' , 'a:' , [])
6687
@@ -77,6 +98,22 @@ def test_do_longs(self):
7798 self .assertEqual (opts , [('--abcd' , '1' )])
7899 self .assertEqual (args , [])
79100
101+ opts , args = getopt .do_longs ([], 'abc' , ['abc=?' ], ['1' ])
102+ self .assertEqual (opts , [('--abc' , '' )])
103+ self .assertEqual (args , ['1' ])
104+
105+ opts , args = getopt .do_longs ([], 'abc' , ['abcd=?' ], ['1' ])
106+ self .assertEqual (opts , [('--abcd' , '' )])
107+ self .assertEqual (args , ['1' ])
108+
109+ opts , args = getopt .do_longs ([], 'abc=1' , ['abc=?' ], [])
110+ self .assertEqual (opts , [('--abc' , '1' )])
111+ self .assertEqual (args , [])
112+
113+ opts , args = getopt .do_longs ([], 'abc=1' , ['abcd=?' ], [])
114+ self .assertEqual (opts , [('--abcd' , '1' )])
115+ self .assertEqual (args , [])
116+
80117 opts , args = getopt .do_longs ([], 'abc' , ['ab' , 'abc' , 'abcd' ], [])
81118 self .assertEqual (opts , [('--abc' , '' )])
82119 self .assertEqual (args , [])
@@ -95,7 +132,7 @@ def test_getopt(self):
95132 # note: the empty string between '-a' and '--beta' is significant:
96133 # it simulates an empty string option argument ('-a ""') on the
97134 # command line.
98- cmdline = ['-a' , '1 ' , '-b' , '--alpha=2' , '--beta' , '-a' , '3' , '-a' ,
135+ cmdline = ['-a1 ' , '-b' , '--alpha=2' , '--beta' , '-a' , '3' , '-a' ,
99136 '' , '--beta' , 'arg1' , 'arg2' ]
100137
101138 opts , args = getopt .getopt (cmdline , 'a:b' , ['alpha=' , 'beta' ])
@@ -106,17 +143,29 @@ def test_getopt(self):
106143 # accounted for in the code that calls getopt().
107144 self .assertEqual (args , ['arg1' , 'arg2' ])
108145
146+ cmdline = ['-a1' , '--alpha=2' , '--alpha=' , '-a' , '--alpha' , 'arg1' , 'arg2' ]
147+ opts , args = getopt .getopt (cmdline , 'a::' , ['alpha=?' ])
148+ self .assertEqual (opts , [('-a' , '1' ), ('--alpha' , '2' ), ('--alpha' , '' ),
149+ ('-a' , '' ), ('--alpha' , '' )])
150+ self .assertEqual (args , ['arg1' , 'arg2' ])
151+
109152 self .assertError (getopt .getopt , cmdline , 'a:b' , ['alpha' , 'beta' ])
110153
111154 def test_gnu_getopt (self ):
112155 # Test handling of GNU style scanning mode.
113- cmdline = ['-a' , 'arg1' , '-b' , '1' , '--alpha' , '--beta=2' ]
156+ cmdline = ['-a' , 'arg1' , '-b' , '1' , '--alpha' , '--beta=2' , '--beta' ,
157+ '3' , 'arg2' ]
114158
115159 # GNU style
116160 opts , args = getopt .gnu_getopt (cmdline , 'ab:' , ['alpha' , 'beta=' ])
117- self .assertEqual (args , ['arg1' ])
118- self .assertEqual (opts , [('-a' , '' ), ('-b' , '1' ),
119- ('--alpha' , '' ), ('--beta' , '2' )])
161+ self .assertEqual (args , ['arg1' , 'arg2' ])
162+ self .assertEqual (opts , [('-a' , '' ), ('-b' , '1' ), ('--alpha' , '' ),
163+ ('--beta' , '2' ), ('--beta' , '3' )])
164+
165+ opts , args = getopt .gnu_getopt (cmdline , 'ab::' , ['alpha' , 'beta=?' ])
166+ self .assertEqual (args , ['arg1' , '1' , '3' , 'arg2' ])
167+ self .assertEqual (opts , [('-a' , '' ), ('-b' , '' ), ('--alpha' , '' ),
168+ ('--beta' , '2' ), ('--beta' , '' )])
120169
121170 # recognize "-" as an argument
122171 opts , args = getopt .gnu_getopt (['-a' , '-' , '-b' , '-' ], 'ab:' , [])
@@ -126,13 +175,15 @@ def test_gnu_getopt(self):
126175 # Posix style via +
127176 opts , args = getopt .gnu_getopt (cmdline , '+ab:' , ['alpha' , 'beta=' ])
128177 self .assertEqual (opts , [('-a' , '' )])
129- self .assertEqual (args , ['arg1' , '-b' , '1' , '--alpha' , '--beta=2' ])
178+ self .assertEqual (args , ['arg1' , '-b' , '1' , '--alpha' , '--beta=2' ,
179+ '--beta' , '3' , 'arg2' ])
130180
131181 # Posix style via POSIXLY_CORRECT
132182 self .env ["POSIXLY_CORRECT" ] = "1"
133183 opts , args = getopt .gnu_getopt (cmdline , 'ab:' , ['alpha' , 'beta=' ])
134184 self .assertEqual (opts , [('-a' , '' )])
135- self .assertEqual (args , ['arg1' , '-b' , '1' , '--alpha' , '--beta=2' ])
185+ self .assertEqual (args , ['arg1' , '-b' , '1' , '--alpha' , '--beta=2' ,
186+ '--beta' , '3' , 'arg2' ])
136187
137188 def test_issue4629 (self ):
138189 longopts , shortopts = getopt .getopt (['--help=' ], '' , ['help=' ])
0 commit comments