From 8a2c4172e1fc656594b4b552be8ab8f9bc9a8257 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Sun, 24 Apr 2011 23:11:42 -0700 Subject: [PATCH] Infer option types from the default when possible. Closes #248. --- tornado/options.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) 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) -- 2.47.3