Skip to content

Commit cd76d9e

Browse files
committed
Improve getopt and unix_getopt test coverage
1 parent 31c81e6 commit cd76d9e

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lib/test/test_getopt.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ def test_getopt(self):
149149
('-a', ''), ('--alpha', '')])
150150
self.assertEqual(args, ['arg1', 'arg2'])
151151

152+
opts, args = getopt.getopt(cmdline, 'a::', 'alpha=?')
153+
self.assertEqual(opts, [('-a', '1'), ('--alpha', '2'), ('--alpha', ''),
154+
('-a', ''), ('--alpha', '')])
155+
self.assertEqual(args, ['arg1', 'arg2'])
156+
157+
cmdline = ['-a1', '--alpha=2', '--', '-b', '--beta=5']
158+
opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
159+
self.assertEqual(opts, [('-a', '1'), ('--alpha', '2')])
160+
self.assertEqual(args, ['-b', '--beta=5'])
161+
152162
self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta'])
153163

154164
def test_gnu_getopt(self):
@@ -191,6 +201,25 @@ def test_gnu_getopt(self):
191201
self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2',
192202
'--beta', '3', 'arg2'])
193203

204+
# Allow string for single long argument
205+
opts, args = getopt.gnu_getopt(cmdline, 'ab:', 'alpha')
206+
self.assertEqual(opts, [('-a', '')])
207+
self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2',
208+
'--beta', '3', 'arg2'])
209+
210+
# Pass everything after -- as args
211+
cmdline = ['-a1', '--alpha=2', '--', '-b', '--beta=5']
212+
opts, args = getopt.gnu_getopt(cmdline, 'a:b', ['alpha=', 'beta'])
213+
self.assertEqual(opts, [('-a', '1'), ('--alpha', '2')])
214+
self.assertEqual(args, ['-b', '--beta=5'])
215+
216+
# In order arguments
217+
cmdline = ["gamma", "--alpha=3"]
218+
opts, args = getopt.gnu_getopt(cmdline, '-', ["alpha="])
219+
self.assertEqual(opts, [(None, ['gamma']), ('--alpha', '3')])
220+
self.assertEqual(args, [])
221+
222+
194223
def test_issue4629(self):
195224
longopts, shortopts = getopt.getopt(['--help='], '', ['help='])
196225
self.assertEqual(longopts, [('--help', '')])

0 commit comments

Comments
 (0)