@@ -19,21 +19,34 @@ def assertError(self, *args, **kwargs):
19
19
self .assertRaises (getopt .GetoptError , * args , ** kwargs )
20
20
21
21
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::' ), '?' )
24
25
self .assertError (getopt .short_has_arg , 'a' , 'b' )
25
26
26
27
def test_long_has_args (self ):
27
28
has_arg , option = getopt .long_has_args ('abc' , ['abc=' ])
28
- self .assertTrue (has_arg )
29
+ self .assertIs (has_arg , True )
29
30
self .assertEqual (option , 'abc' )
30
31
31
32
has_arg , option = getopt .long_has_args ('abc' , ['abc' ])
32
- self .assertFalse (has_arg )
33
+ self .assertIs (has_arg , False )
33
34
self .assertEqual (option , 'abc' )
34
35
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
+
35
44
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 , '?' )
37
50
self .assertEqual (option , 'abcd' )
38
51
39
52
self .assertError (getopt .long_has_args , 'abc' , ['def' ])
@@ -49,9 +62,9 @@ def test_do_shorts(self):
49
62
self .assertEqual (opts , [('-a' , '1' )])
50
63
self .assertEqual (args , [])
51
64
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 , [])
55
68
56
69
opts , args = getopt .do_shorts ([], 'a' , 'a:' , ['1' ])
57
70
self .assertEqual (opts , [('-a' , '1' )])
@@ -61,6 +74,14 @@ def test_do_shorts(self):
61
74
self .assertEqual (opts , [('-a' , '1' )])
62
75
self .assertEqual (args , ['2' ])
63
76
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
+
64
85
self .assertError (getopt .do_shorts , [], 'a1' , 'a' , [])
65
86
self .assertError (getopt .do_shorts , [], 'a' , 'a:' , [])
66
87
@@ -77,6 +98,22 @@ def test_do_longs(self):
77
98
self .assertEqual (opts , [('--abcd' , '1' )])
78
99
self .assertEqual (args , [])
79
100
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
+
80
117
opts , args = getopt .do_longs ([], 'abc' , ['ab' , 'abc' , 'abcd' ], [])
81
118
self .assertEqual (opts , [('--abc' , '' )])
82
119
self .assertEqual (args , [])
@@ -95,7 +132,7 @@ def test_getopt(self):
95
132
# note: the empty string between '-a' and '--beta' is significant:
96
133
# it simulates an empty string option argument ('-a ""') on the
97
134
# command line.
98
- cmdline = ['-a' , '1 ' , '-b' , '--alpha=2' , '--beta' , '-a' , '3' , '-a' ,
135
+ cmdline = ['-a1 ' , '-b' , '--alpha=2' , '--beta' , '-a' , '3' , '-a' ,
99
136
'' , '--beta' , 'arg1' , 'arg2' ]
100
137
101
138
opts , args = getopt .getopt (cmdline , 'a:b' , ['alpha=' , 'beta' ])
@@ -106,17 +143,29 @@ def test_getopt(self):
106
143
# accounted for in the code that calls getopt().
107
144
self .assertEqual (args , ['arg1' , 'arg2' ])
108
145
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
+
109
152
self .assertError (getopt .getopt , cmdline , 'a:b' , ['alpha' , 'beta' ])
110
153
111
154
def test_gnu_getopt (self ):
112
155
# 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' ]
114
158
115
159
# GNU style
116
160
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' , '' )])
120
169
121
170
# recognize "-" as an argument
122
171
opts , args = getopt .gnu_getopt (['-a' , '-' , '-b' , '-' ], 'ab:' , [])
@@ -126,13 +175,15 @@ def test_gnu_getopt(self):
126
175
# Posix style via +
127
176
opts , args = getopt .gnu_getopt (cmdline , '+ab:' , ['alpha' , 'beta=' ])
128
177
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' ])
130
180
131
181
# Posix style via POSIXLY_CORRECT
132
182
self .env ["POSIXLY_CORRECT" ] = "1"
133
183
opts , args = getopt .gnu_getopt (cmdline , 'ab:' , ['alpha' , 'beta=' ])
134
184
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' ])
136
187
137
188
def test_issue4629 (self ):
138
189
longopts , shortopts = getopt .getopt (['--help=' ], '' , ['help=' ])
0 commit comments