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="127.0.0.1:3306", help="Main user DB")
define("memcache_hosts", default="127.0.0.1:11011", 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
tornado.options.parse_config_file("/etc/server.conf")
tornado.options.parse_command_line()

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, group=None)[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

group is used to group the defined options in logical groups. By default, command line options are grouped by the defined file.

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.

tornado.options.parse_command_line(args=None)[source]

Parses all options given on the command line.

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

tornado.options.parse_config_file(path)[source]

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

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

Prints all the command line options to stdout.

exception tornado.options.Error[source]

Exception raised by errors in the options module.

tornado.options.enable_pretty_logging()[source]

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.process — Utilities for multiple processes

This Page