]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Infer option types from the default when possible.
authorBen Darnell <ben@bendarnell.com>
Mon, 25 Apr 2011 06:11:42 +0000 (23:11 -0700)
committerBen Darnell <ben@bendarnell.com>
Mon, 25 Apr 2011 06:11:42 +0000 (23:11 -0700)
Closes #248.

tornado/options.py

index 35c695783c532df1d7305473784f05c4acfc3d90..3ea88bb6caf97250ff5da7fbdf05839b4d5157e5 100644 (file)
@@ -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)