本文整理汇总了Python中tornado.options.OptionParser类的典型用法代码示例。如果您正苦于以下问题:Python OptionParser类的具体用法?Python OptionParser怎么用?Python OptionParser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OptionParser类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_error_redefine
def test_error_redefine(self):
options = OptionParser()
options.define('foo')
with self.assertRaises(Error) as cm:
options.define('foo')
self.assertRegexpMatches(str(cm.exception),
'Option.*foo.*already defined')
开发者ID:ArthurGarnier,项目名称:SickRage,代码行数:7,代码来源:options_test.py
示例2: test_setattr_type_check
def test_setattr_type_check(self):
# setattr requires that options be the right type and doesn't
# parse from string formats.
options = OptionParser()
options.define('foo', default=1, type=int)
with self.assertRaises(Error):
options.foo = '2'
开发者ID:ArthurGarnier,项目名称:SickRage,代码行数:7,代码来源:options_test.py
示例3: test_dash_underscore_file
def test_dash_underscore_file(self):
# No matter how an option was defined, it can be set with underscores
# in a config file.
for defined_name in ['foo-bar', 'foo_bar']:
options = OptionParser()
options.define(defined_name)
options.parse_config_file(os.path.join(os.path.dirname(__file__),
"options_test.cfg"))
self.assertEqual(options.foo_bar, 'a')
开发者ID:ArthurGarnier,项目名称:SickRage,代码行数:9,代码来源:options_test.py
示例4: test_help
def test_help(self):
options = OptionParser()
try:
orig_stderr = sys.stderr
sys.stderr = StringIO()
with self.assertRaises(SystemExit):
options.parse_command_line(["main.py", "--help"])
usage = sys.stderr.getvalue()
finally:
sys.stderr = orig_stderr
self.assertIn("Usage:", usage)
开发者ID:ArthurGarnier,项目名称:SickRage,代码行数:11,代码来源:options_test.py
示例5: test_dash_underscore_cli
def test_dash_underscore_cli(self):
# Dashes and underscores should be interchangeable.
for defined_name in ["foo-bar", "foo_bar"]:
for flag in ["--foo-bar=a", "--foo_bar=a"]:
options = OptionParser()
options.define(defined_name)
options.parse_command_line(["main.py", flag])
# Attr-style access always uses underscores.
self.assertEqual(options.foo_bar, "a")
# Dict-style access allows both.
self.assertEqual(options["foo-bar"], "a")
self.assertEqual(options["foo_bar"], "a")
开发者ID:bdarnell,项目名称:tornado,代码行数:12,代码来源:options_test.py
示例6: parse_logger_callback
def parse_logger_callback(self):
if options.disable_log:
options.logging = None
if options.log_file_prefix and options.log_port_prefix:
options.log_file_prefix += ".%s" % options.port
if options.log_patch:
logging.handlers.TimedRotatingFileHandler = ProcessLogTimedFileHandler
tornado_logger = logging.getLogger('tornado')
enable_pretty_logging(logger=tornado_logger)
logdir = options.logging_dir or settings.LOGGING_DIR
for log in settings.LOGGING:
opt = OptionParser()
define_logging_options(opt)
self.define(opt)
opt.log_rotate_when = log.get('when', 'midnight')
opt.log_to_stderr = log.get('log_to_stderr', False)
opt.logging = log.get('level', 'INFO')
opt.log_file_prefix = os.path.join(logdir, log['filename'])
if log.get('backups'):
opt.log_file_num_backups = log.get('backups')
if opt.log_port_prefix:
opt.log_file_prefix += ".%s" % options.port
opt.log_rotate_interval = log.get('interval', 1)
opt.log_rotate_mode = 'time'
logger = logging.getLogger(log['name'])
if not settings.DEBUG:
logger.propagate = 0
else:
if not opt.log_to_stderr:
logger.propagate = 0
else:
logger.propagate = 1
enable_pretty_logging(options=opt, logger=logger)
map(lambda h: h.setFormatter(LogFormatter(fmt=log.get("formatter", LogFormatter.DEFAULT_FORMAT),
color=settings.DEBUG)), logger.handlers)
开发者ID:wuchangqi,项目名称:torngas,代码行数:35,代码来源:webserver.py
示例7: test_parse_config_file
def test_parse_config_file(self):
options = OptionParser()
options.define("port", default=80)
options.define("username", default='foo')
options.define("my_path")
config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"options_test.cfg")
options.parse_config_file(config_path)
self.assertEqual(options.port, 443)
self.assertEqual(options.username, "李康")
self.assertEqual(options.my_path, config_path)
开发者ID:ArthurGarnier,项目名称:SickRage,代码行数:11,代码来源:options_test.py
示例8: test_error_redefine_underscore
def test_error_redefine_underscore(self):
# Ensure that the dash/underscore normalization doesn't
# interfere with the redefinition error.
tests = [
('foo-bar', 'foo-bar'),
('foo_bar', 'foo_bar'),
('foo-bar', 'foo_bar'),
('foo_bar', 'foo-bar'),
]
for a, b in tests:
with subTest(self, a=a, b=b):
options = OptionParser()
options.define(a)
with self.assertRaises(Error) as cm:
options.define(b)
self.assertRegexpMatches(str(cm.exception),
'Option.*foo.bar.*already defined')
开发者ID:alexdxy,项目名称:tornado,代码行数:17,代码来源:options_test.py
示例9: test_dash_underscore_introspection
def test_dash_underscore_introspection(self):
# Original names are preserved in introspection APIs.
options = OptionParser()
options.define('with-dash', group='g')
options.define('with_underscore', group='g')
all_options = ['help', 'with-dash', 'with_underscore']
self.assertEqual(sorted(options), all_options)
self.assertEqual(sorted(k for (k, v) in options.items()), all_options)
self.assertEqual(sorted(options.as_dict().keys()), all_options)
self.assertEqual(sorted(options.group_dict('g')),
['with-dash', 'with_underscore'])
# --help shows CLI-style names with dashes.
buf = StringIO()
options.print_help(buf)
self.assertIn('--with-dash', buf.getvalue())
self.assertIn('--with-underscore', buf.getvalue())
开发者ID:ArthurGarnier,项目名称:SickRage,代码行数:18,代码来源:options_test.py
示例10: test_dash_underscore_introspection
def test_dash_underscore_introspection(self):
# Original names are preserved in introspection APIs.
options = OptionParser()
options.define("with-dash", group="g")
options.define("with_underscore", group="g")
all_options = ["help", "with-dash", "with_underscore"]
self.assertEqual(sorted(options), all_options)
self.assertEqual(sorted(k for (k, v) in options.items()), all_options)
self.assertEqual(sorted(options.as_dict().keys()), all_options)
self.assertEqual(
sorted(options.group_dict("g")), ["with-dash", "with_underscore"]
)
# --help shows CLI-style names with dashes.
buf = StringIO()
options.print_help(buf)
self.assertIn("--with-dash", buf.getvalue())
self.assertIn("--with-underscore", buf.getvalue())
开发者ID:bdarnell,项目名称:tornado,代码行数:19,代码来源:options_test.py
示例11: test_group_dict
def test_group_dict(self):
options = OptionParser()
options.define("a", default=1)
options.define("b", group="b_group", default=2)
frame = sys._getframe(0)
this_file = frame.f_code.co_filename
self.assertEqual(set(["b_group", "", this_file]), options.groups())
b_group_dict = options.group_dict("b_group")
self.assertEqual({"b": 2}, b_group_dict)
self.assertEqual({}, options.group_dict("nonexistent"))
开发者ID:bdarnell,项目名称:tornado,代码行数:13,代码来源:options_test.py
示例12: test_group_dict
def test_group_dict(self):
options = OptionParser()
options.define('a', default=1)
options.define('b', group='b_group', default=2)
frame = sys._getframe(0)
this_file = frame.f_code.co_filename
self.assertEqual(set(['b_group', '', this_file]), options.groups())
b_group_dict = options.group_dict('b_group')
self.assertEqual({'b': 2}, b_group_dict)
self.assertEqual({}, options.group_dict('nonexistent'))
开发者ID:ArthurGarnier,项目名称:SickRage,代码行数:13,代码来源:options_test.py
示例13: test_subcommand
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
开发者ID:ArthurGarnier,项目名称:SickRage,代码行数:21,代码来源:options_test.py
示例14: test_mock_patch
def test_mock_patch(self):
# ensure that our setattr hooks don't interfere with mock.patch
options = OptionParser()
options.define('foo', default=1)
options.parse_command_line(['main.py', '--foo=2'])
self.assertEqual(options.foo, 2)
with mock.patch.object(options.mockable(), 'foo', 3):
self.assertEqual(options.foo, 3)
self.assertEqual(options.foo, 2)
# Try nested patches mixed with explicit sets
with mock.patch.object(options.mockable(), 'foo', 4):
self.assertEqual(options.foo, 4)
options.foo = 5
self.assertEqual(options.foo, 5)
with mock.patch.object(options.mockable(), 'foo', 6):
self.assertEqual(options.foo, 6)
self.assertEqual(options.foo, 5)
self.assertEqual(options.foo, 2)
开发者ID:ArthurGarnier,项目名称:SickRage,代码行数:20,代码来源:options_test.py
示例15: test_parse_callbacks
def test_parse_callbacks(self):
options = OptionParser()
self.called = False
def callback():
self.called = True
options.add_parse_callback(callback)
# non-final parse doesn't run callbacks
options.parse_command_line(["main.py"], final=False)
self.assertFalse(self.called)
# final parse does
options.parse_command_line(["main.py"])
self.assertTrue(self.called)
# callbacks can be run more than once on the same options
# object if there are multiple final parses
self.called = False
options.parse_command_line(["main.py"])
self.assertTrue(self.called)
开发者ID:CNCBASHER,项目名称:tornado,代码行数:20,代码来源:options_test.py
示例16: test_setattr_with_callback
def test_setattr_with_callback(self):
values = []
options = OptionParser()
options.define('foo', default=1, type=int, callback=values.append)
options.foo = 2
self.assertEqual(values, [2])
开发者ID:ArthurGarnier,项目名称:SickRage,代码行数:6,代码来源:options_test.py
示例17: test_parse_command_line
def test_parse_command_line(self):
options = OptionParser()
options.define("port", default=80)
options.parse_command_line(["main.py", "--port=443"])
self.assertEqual(options.port, 443)
开发者ID:ArthurGarnier,项目名称:SickRage,代码行数:5,代码来源:options_test.py
示例18: _define_options
def _define_options(self):
options = OptionParser()
options.define("str", type=str)
options.define("basestring", type=basestring_type)
options.define("int", type=int)
options.define("float", type=float)
options.define("datetime", type=datetime.datetime)
options.define("timedelta", type=datetime.timedelta)
options.define("email", type=Email)
options.define("list-of-int", type=int, multiple=True)
return options
开发者ID:bdarnell,项目名称:tornado,代码行数:11,代码来源:options_test.py
示例19: test_multiple_string
def test_multiple_string(self):
options = OptionParser()
options.define("foo", type=str, multiple=True)
options.parse_command_line(["main.py", "--foo=a,b,c"])
self.assertEqual(options.foo, ["a", "b", "c"])
开发者ID:bdarnell,项目名称:tornado,代码行数:5,代码来源:options_test.py
示例20: test_multiple_int
def test_multiple_int(self):
options = OptionParser()
options.define("foo", type=int, multiple=True)
options.parse_command_line(["main.py", "--foo=1,3,5:7"])
self.assertEqual(options.foo, [1, 3, 5, 6, 7])
开发者ID:bdarnell,项目名称:tornado,代码行数:5,代码来源:options_test.py
注:本文中的tornado.options.OptionParser类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论