Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Lib/test/test_getopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ def test_getopt(self):
# accounted for in the code that calls getopt().
self.assertEqual(args, ['arg1', 'arg2'])

opts, args = getopt.getopt(cmdline, 'a::', ['alpha=?'])
self.assertEqual(opts, [('-a', '1'), ('--alpha', '2'), ('--alpha', ''),
('-a', ''), ('--alpha', '')])
self.assertEqual(args, ['arg1', 'arg2'])

# Allow string for single long argument
opts, args = getopt.getopt(cmdline, 'a::', 'alpha=?')
self.assertEqual(opts, [('-a', '1'), ('--alpha', '2'), ('--alpha', ''),
('-a', ''), ('--alpha', '')])
self.assertEqual(args, ['arg1', 'arg2'])

# Pass everything after -- as args
cmdline = ['-a1', '--alpha=2', '--', '-b', '--beta=5']
opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
self.assertEqual(opts, [('-a', '1'), ('--alpha', '2')])
self.assertEqual(args, ['-b', '--beta=5'])

cmdline = ['-a1', '--alpha=2', '--alpha=', '-a', '--alpha', 'arg1', 'arg2']
self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta'])

def test_gnu_getopt(self):
Expand Down Expand Up @@ -134,6 +152,25 @@ def test_gnu_getopt(self):
self.assertEqual(opts, [('-a', '')])
self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])

# Allow string for single long argument
opts, args = getopt.gnu_getopt(cmdline, 'ab:', 'alpha')
self.assertEqual(opts, [('-a', '')])
self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2',
'--beta', '3', 'arg2'])

# Pass everything after -- as args
cmdline = ['-a1', '--alpha=2', '--', '-b', '--beta=5']
opts, args = getopt.gnu_getopt(cmdline, 'a:b', ['alpha=', 'beta'])
self.assertEqual(opts, [('-a', '1'), ('--alpha', '2')])
self.assertEqual(args, ['-b', '--beta=5'])

# In order arguments
cmdline = ["gamma", "--alpha=3"]
opts, args = getopt.gnu_getopt(cmdline, '-', ["alpha="])
self.assertEqual(opts, [(None, ['gamma']), ('--alpha', '3')])
self.assertEqual(args, [])


def test_issue4629(self):
longopts, shortopts = getopt.getopt(['--help='], '', ['help='])
self.assertEqual(longopts, [('--help', '')])
Expand Down
Loading