/Lib/test/test_getopt.py

http://unladen-swallow.googlecode.com/ · Python · 179 lines · 127 code · 35 blank · 17 comment · 4 complexity · da0ec9915e5207fcd7a4e3fe725a222e MD5 · raw file

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