]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Rename _Options to OptionParser, and fix bugs with --help
authorBen Darnell <ben@bendarnell.com>
Sun, 30 Sep 2012 22:44:45 +0000 (15:44 -0700)
committerBen Darnell <ben@bendarnell.com>
Sun, 30 Sep 2012 22:44:45 +0000 (15:44 -0700)
tornado/options.py
tornado/test/options_test.py
website/sphinx/releases/next.rst

index 22d3837cb63bcac9c26920185fa10980f5fedaf9..3330d0aa0441cb324f86f62eb0425a38fe2dda07 100644 (file)
@@ -66,14 +66,14 @@ class Error(Exception):
     pass
 
 
-class _Options(dict):
+class OptionParser(dict):
     """A collection of options, a dictionary with object-like access.
 
     Normally accessed via static functions in the `tornado.options` module,
     which reference a global instance.
     """
     def __init__(self):
-        super(_Options, self).__init__()
+        super(OptionParser, self).__init__()
         self.__dict__['_parse_callbacks'] = []
         self.define("help", type=bool, help="show this help information",
                     callback=self._help_callback)
@@ -128,7 +128,7 @@ class _Options(dict):
             name, equals, value = arg.partition("=")
             name = name.replace('-', '_')
             if not name in self:
-                print_help()
+                self.print_help()
                 raise Error('Unrecognized command line option: %r' % name)
             option = self[name]
             if not equals:
@@ -321,7 +321,7 @@ class _Option(object):
         return _unicode(value)
 
 
-options = _Options()
+options = OptionParser()
 """Global options dictionary.
 
 Supports both attribute-style and dict-style access.
@@ -392,7 +392,7 @@ def parse_config_file(path, final=True):
     return options.parse_config_file(path, final=final)
 
 
-def print_help(file=sys.stdout):
+def print_help(file=None):
     """Prints all the command line options to stdout."""
     return options.print_help(file)
 
index acf898b06c17b9ea051401da6b80fa96473a7a2b..d29a813b5b42ef9523cad13b9fded5183e0f9a48 100644 (file)
@@ -2,7 +2,7 @@ from __future__ import absolute_import, division, with_statement
 
 import sys
 
-from tornado.options import _Options
+from tornado.options import OptionParser, Error
 from tornado.test.util import unittest
 
 try:
@@ -12,13 +12,13 @@ except ImportError:
 
 class OptionsTest(unittest.TestCase):
     def test_parse_command_line(self):
-        options = _Options()
+        options = OptionParser()
         options.define("port", default=80)
         options.parse_command_line(["main.py", "--port=443"])
         self.assertEqual(options.port, 443)
 
     def test_parse_callbacks(self):
-        options = _Options()
+        options = OptionParser()
         self.called = False
         def callback():
             self.called = True
@@ -39,7 +39,7 @@ class OptionsTest(unittest.TestCase):
         self.assertTrue(self.called)
 
     def test_help(self):
-        options = _Options()
+        options = OptionParser()
         try:
             orig_stderr = sys.stderr
             sys.stderr = StringIO()
@@ -49,3 +49,25 @@ class OptionsTest(unittest.TestCase):
         finally:
             sys.stderr = orig_stderr
         self.assertIn("Usage:", usage)
+
+    def test_subcommand(self):
+        base_options = OptionParser()
+        base_options.define("verbose", default=False)
+        sub_options = OptionParser()
+        sub_options.define("foo", type=str)
+        rest = base_options.parse_command_line(
+            ["main.py", "--verbose", "subcommand", "--foo=bar"])
+        self.assertEqual(rest, ["subcommand", "--foo=bar"])
+        self.assertTrue(base_options.verbose)
+        rest2 = sub_options.parse_command_line(rest)
+        self.assertEqual(rest2, [])
+        self.assertEqual(sub_options.foo, "bar")
+
+        # the two option sets are distinct
+        try:
+            orig_stderr = sys.stderr
+            sys.stderr = StringIO()
+            with self.assertRaises(Error):
+                sub_options.parse_command_line(["subcommand", "--verbose"])
+        finally:
+            sys.stderr = orig_stderr
index 6e5cd4f743c4c1faf394fa0f19c5d48e365b04c6..7f0affa43439b33c814d055645ded192db4ef9aa 100644 (file)
@@ -95,3 +95,6 @@ In progress
   from a config file.
 * `tornado.option.parse_command_line` ``--help`` output now goes to ``stderr``
   rather than ``stdout``.
+* The class underlying the functions in `tornado.options` is now public
+  (`tornado.options.OptionParser`).  This can be used to create multiple
+  independent option sets, such as for subcommands.