test_getopt.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # test_getopt.py
  2. # David Goodger <dgoodger@bigfoot.com> 2000-08-19
  3. from test.test_support import verbose, run_doctest, run_unittest, EnvironmentVarGuard
  4. import unittest
  5. import getopt
  6. sentinel = object()
  7. class GetoptTests(unittest.TestCase):
  8. def setUp(self):
  9. self.env = EnvironmentVarGuard()
  10. if "POSIXLY_CORRECT" in self.env:
  11. del self.env["POSIXLY_CORRECT"]
  12. def tearDown(self):
  13. self.env.__exit__()
  14. del self.env
  15. def assertError(self, *args, **kwargs):
  16. self.assertRaises(getopt.GetoptError, *args, **kwargs)
  17. def test_short_has_arg(self):
  18. self.assertTrue(getopt.short_has_arg('a', 'a:'))
  19. self.assertFalse(getopt.short_has_arg('a', 'a'))
  20. self.assertError(getopt.short_has_arg, 'a', 'b')
  21. def test_long_has_args(self):
  22. has_arg, option = getopt.long_has_args('abc', ['abc='])
  23. self.assertTrue(has_arg)
  24. self.assertEqual(option, 'abc')
  25. has_arg, option = getopt.long_has_args('abc', ['abc'])
  26. self.assertFalse(has_arg)
  27. self.assertEqual(option, 'abc')
  28. has_arg, option = getopt.long_has_args('abc', ['abcd'])
  29. self.assertFalse(has_arg)
  30. self.assertEqual(option, 'abcd')
  31. self.assertError(getopt.long_has_args, 'abc', ['def'])
  32. self.assertError(getopt.long_has_args, 'abc', [])
  33. self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde'])
  34. def test_do_shorts(self):
  35. opts, args = getopt.do_shorts([], 'a', 'a', [])
  36. self.assertEqual(opts, [('-a', '')])
  37. self.assertEqual(args, [])
  38. opts, args = getopt.do_shorts([], 'a1', 'a:', [])
  39. self.assertEqual(opts, [('-a', '1')])
  40. self.assertEqual(args, [])
  41. #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
  42. #self.assertEqual(opts, [('-a', '1')])
  43. #self.assertEqual(args, [])
  44. opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
  45. self.assertEqual(opts, [('-a', '1')])
  46. self.assertEqual(args, [])
  47. opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
  48. self.assertEqual(opts, [('-a', '1')])
  49. self.assertEqual(args, ['2'])
  50. self.assertError(getopt.do_shorts, [], 'a1', 'a', [])
  51. self.assertError(getopt.do_shorts, [], 'a', 'a:', [])
  52. def test_do_longs(self):
  53. opts, args = getopt.do_longs([], 'abc', ['abc'], [])
  54. self.assertEqual(opts, [('--abc', '')])
  55. self.assertEqual(args, [])
  56. opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
  57. self.assertEqual(opts, [('--abc', '1')])
  58. self.assertEqual(args, [])
  59. opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
  60. self.assertEqual(opts, [('--abcd', '1')])
  61. self.assertEqual(args, [])
  62. opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
  63. self.assertEqual(opts, [('--abc', '')])
  64. self.assertEqual(args, [])
  65. # Much like the preceding, except with a non-alpha character ("-") in
  66. # option name that precedes "="; failed in
  67. # http://python.org/sf/126863
  68. opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
  69. self.assertEqual(opts, [('--foo', '42')])
  70. self.assertEqual(args, [])
  71. self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], [])
  72. self.assertError(getopt.do_longs, [], 'abc', ['abc='], [])
  73. def test_getopt(self):
  74. # note: the empty string between '-a' and '--beta' is significant:
  75. # it simulates an empty string option argument ('-a ""') on the
  76. # command line.
  77. cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a',
  78. '', '--beta', 'arg1', 'arg2']
  79. opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
  80. self.assertEqual(opts, [('-a', '1'), ('-b', ''),
  81. ('--alpha', '2'), ('--beta', ''),
  82. ('-a', '3'), ('-a', ''), ('--beta', '')])
  83. # Note ambiguity of ('-b', '') and ('-a', '') above. This must be
  84. # accounted for in the code that calls getopt().
  85. self.assertEqual(args, ['arg1', 'arg2'])
  86. self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta'])
  87. def test_gnu_getopt(self):
  88. # Test handling of GNU style scanning mode.
  89. cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
  90. # GNU style
  91. opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
  92. self.assertEqual(args, ['arg1'])
  93. self.assertEqual(opts, [('-a', ''), ('-b', '1'),
  94. ('--alpha', ''), ('--beta', '2')])
  95. # recognize "-" as an argument
  96. opts, args = getopt.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', [])
  97. self.assertEqual(args, ['-'])
  98. self.assertEqual(opts, [('-a', ''), ('-b', '-')])
  99. # Posix style via +
  100. opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
  101. self.assertEqual(opts, [('-a', '')])
  102. self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
  103. # Posix style via POSIXLY_CORRECT
  104. self.env["POSIXLY_CORRECT"] = "1"
  105. opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
  106. self.assertEqual(opts, [('-a', '')])
  107. self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
  108. def test_libref_examples(self):
  109. s = """
  110. Examples from the Library Reference: Doc/lib/libgetopt.tex
  111. An example using only Unix style options:
  112. >>> import getopt
  113. >>> args = '-a -b -cfoo -d bar a1 a2'.split()
  114. >>> args
  115. ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
  116. >>> optlist, args = getopt.getopt(args, 'abc:d:')
  117. >>> optlist
  118. [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
  119. >>> args
  120. ['a1', 'a2']
  121. Using long option names is equally easy:
  122. >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
  123. >>> args = s.split()
  124. >>> args
  125. ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
  126. >>> optlist, args = getopt.getopt(args, 'x', [
  127. ... 'condition=', 'output-file=', 'testing'])
  128. >>> optlist
  129. [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
  130. >>> args
  131. ['a1', 'a2']
  132. """
  133. import types
  134. m = types.ModuleType("libreftest", s)
  135. run_doctest(m, verbose)
  136. def test_issue4629(self):
  137. longopts, shortopts = getopt.getopt(['--help='], '', ['help='])
  138. self.assertEqual(longopts, [('--help', '')])
  139. longopts, shortopts = getopt.getopt(['--help=x'], '', ['help='])
  140. self.assertEqual(longopts, [('--help', 'x')])
  141. self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help'])
  142. def test_main():
  143. run_unittest(GetoptTests)
  144. if __name__ == "__main__":
  145. test_main()