From: Ben Darnell Date: Mon, 25 Apr 2011 06:11:42 +0000 (-0700) Subject: Infer option types from the default when possible. X-Git-Tag: v2.0.0~99 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8a2c4172e1fc656594b4b552be8ab8f9bc9a8257;p=thirdparty%2Ftornado.git Infer option types from the default when possible. Closes #248. --- diff --git a/tornado/options.py b/tornado/options.py index 35c695783..3ea88bb6c 100644 --- a/tornado/options.py +++ b/tornado/options.py @@ -62,14 +62,14 @@ except: curses = None -def define(name, default=None, type=str, help=None, metavar=None, +def define(name, default=None, type=None, help=None, metavar=None, multiple=False): """Defines a new command line option. - If type is given (one of str, float, int, datetime, or timedelta), - we parse the command line arguments based on the given type. If - multiple is True, we accept comma-separated values, and the option - value is always a list. + If type is given (one of str, float, int, datetime, or timedelta) + or can be inferred from the default, we parse the command line + arguments based on the given type. If multiple is True, we accept + comma-separated values, and the option value is always a list. For multi-value integers, we also accept the syntax x:y, which turns into range(x, y) - very useful for long integer ranges. @@ -90,6 +90,11 @@ def define(name, default=None, type=str, help=None, metavar=None, options_file = frame.f_code.co_filename file_name = frame.f_back.f_code.co_filename if file_name == options_file: file_name = "" + if type is None: + if not multiple and default is not None: + type = default.__class__ + else: + type = str options[name] = _Option(name, file_name=file_name, default=default, type=type, help=help, metavar=metavar, multiple=multiple)