From 9229d59c09664ae14690a8ebbf5c38cde9281785 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Sun, 30 Sep 2012 15:44:45 -0700 Subject: [PATCH] Rename _Options to OptionParser, and fix bugs with --help --- tornado/options.py | 10 +++++----- tornado/test/options_test.py | 30 ++++++++++++++++++++++++++---- website/sphinx/releases/next.rst | 3 +++ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/tornado/options.py b/tornado/options.py index 22d3837cb..3330d0aa0 100644 --- a/tornado/options.py +++ b/tornado/options.py @@ -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) diff --git a/tornado/test/options_test.py b/tornado/test/options_test.py index acf898b06..d29a813b5 100644 --- a/tornado/test/options_test.py +++ b/tornado/test/options_test.py @@ -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 diff --git a/website/sphinx/releases/next.rst b/website/sphinx/releases/next.rst index 6e5cd4f74..7f0affa43 100644 --- a/website/sphinx/releases/next.rst +++ b/website/sphinx/releases/next.rst @@ -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. -- 2.47.2