tornado.options — Command-line parsing

A command line parsing module that lets modules define their own options.

Each module defines its own options, e.g.:

from tornado.options import define, options

define("mysql_host", default="", help="Main user DB")
define("memcache_hosts", default="", multiple=True,
       help="Main user memcache servers")

def connect():
    db = database.Connection(options.mysql_host)

The main() method of your application does not need to be aware of all of the options used throughout your program; they are all automatically loaded when the modules are loaded. Your main() method can parse the command line or parse a config file with:

import tornado.options

Command line formats are what you would expect (“–myoption=myvalue”). Config files are just Python files. Global names become options, e.g.:

myoption = "myvalue"
myotheroption = "myothervalue"

We support datetimes, timedeltas, ints, and floats (just pass a ‘type’ kwarg to define). We also accept multi-value options. See the documentation for define() below.

tornado.options.define(name, default=None, type=None, help=None, metavar=None, multiple=False)[source]

Defines a new command line option.

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.

help and metavar are used to construct the automatically generated command line help string. The help message is formatted like:

--name=METAVAR      help string

Command line option names must be unique globally. They can be parsed from the command line with parse_command_line() or parsed from a config file with parse_config_file.


Parses all options given on the command line.

We return all command line arguments that are not options as a list.


Parses and loads the Python config file at the given path.

tornado.options.print_help(file=<open file '<stdout>', mode 'w' at 0x7f9e455a6150>)[source]

Prints all the command line options to stdout.

exception tornado.options.Error[source]

Exception raised by errors in the options module.

x.__init__(...) initializes x; see help(type(x)) for signature


Turns on formatted logging output as configured.

This is called automatically by parse_command_line.

Previous topic

tornado.httputil — Manipulate HTTP headers and URLs

Next topic

tornado.stack_context — Exception handling across asynchronous callbacks

This Page