return self._options[name].set(value)
raise AttributeError("Unrecognized option %r" % name)
+ def __iter__(self):
+ return iter(self._options)
+
+ def __getitem__(self, item):
+ return self._options[item]
+
+ def items(self):
+ """A sequence of (name, value) pairs."""
+ return [(name, opt.value()) for name, opt in self._options.items()]
+
+ def as_dict(self):
+ """A dict of names and values.
+
+ Useful for copying options into Application settings::
+
+ from tornado import options
+ options.parse_command_line()
+ application = Application(handler, **options.options.as_dict())
+ """
+ return dict(self.items())
+
def define(self, name, default=None, type=None, help=None, metavar=None,
multiple=False, group=None, callback=None):
"""Defines a new command line option.
options.foo = 2
self.assertEqual(values, [2])
+ def _sample_options(self):
+ options = OptionParser()
+ options.define('a', default=1)
+ options.define('b', default=2)
+ return options
+
+ def test_iter(self):
+ options = self._sample_options()
+ # OptionParsers always define 'help'.
+ self.assertEqual(set(['a', 'b', 'help']), set(iter(options)))
+
+ def test_items(self):
+ options = self._sample_options()
+ # OptionParsers always define 'help'.
+ expected = [('a', 1), ('b', 2), ('help', options.help)]
+ actual = sorted(options.items())
+ self.assertEqual(expected, actual)
+
+ def test_as_dict(self):
+ options = self._sample_options()
+ options_dict = options.as_dict()
+ # OptionParsers always define 'help'.
+ expected = {'a': 1, 'b': 2, 'help': options.help}
+ self.assertEqual(expected, options_dict)
+
@unittest.skipIf(mock is None, 'mock package not present')
def test_mock_patch(self):
# ensure that our setattr hooks don't interfere with mock.patch