test_getopt.py 6.7 KB

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