tornado.options — Command-line parsing¶

    Each module defines its own options which are added to the globaloption namespace, e.g.:

    The method of your application does not need to be aware of all ofthe options used throughout your program; they are all automatically loadedwhen the modules are loaded. However, all modules that define optionsmust have been imported before the command line is parsed.

    Your main() method can parse the command line or parse a config file witheither:

    1. # or
    2. tornado.options.parse_config_file("/etc/server.conf")

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

    tornado.options.options is a singleton instance of , andthe top-level functions in this module (define, , etc)simply call methods on it. You may create additional OptionParserinstances to define isolated sets of options, such as for subcommands.

    注解

    1. options.logging = None
    2. parse_command_line()

    在 4.3 版更改: Dashes and underscores are fully interchangeable in option names;options can be defined, set, and read with any mix of the two.Dashes are typical for command-line usage while config files requireunderscores.

    tornado.options.define(name, default=None, type=None, help=None, metavar=None, multiple=False, group=None, callback=None)

    Defines an option in the global namespace.

    See .
    tornado.options.options

    Global options object. All defined options are available as attributeson this object.
    tornado.options.parsecommand_line(_args=None, final=True)

    Parses global options from the command line.

    See .
    tornado.options.(_path, final=True)[源代码]

    Parses global options from a config file.

    See OptionParser.parse_config_file.
    tornado.options.printhelp(_file=sys.stderr)

    Prints all the command line options to stderr (or another file).

    See .
    tornado.options.addparse_callback(_callback)[源代码]

    Adds a parse callback, to be invoked when option parsing is done.

    See OptionParser.add_parse_callback
    exception tornado.options.Error

    Exception raised by errors in the options module.

    OptionParser class¶

    class tornado.options.OptionParser[源代码]

    A collection of options, a dictionary with object-like access.

    Normally accessed via static functions in the tornado.options module,which reference a global instance.
    addparse_callback(_callback)

    Adds a parse callback, to be invoked when option parsing is done.
    asdict()

    The names and values of all options.


    3.1 新版功能.

    define(_name, default=None, type=None, help=None, metavar=None, multiple=False, group=None, callback=None)

    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 linearguments based on the given type. If multiple is True, we acceptcomma-separated values, and the option value is always a list.

    For multi-value integers, we also accept the syntax x:y, whichturns into range(x, y) - very useful for long integer ranges.

    help and metavar are used to construct theautomatically generated command line help string. The helpmessage is formatted like:







    group is used to group the defined options in logicalgroups. By default, command line options are grouped by thefile in which they are defined.

    Command line option names must be unique globally. They can be parsedfrom the command line with or parsed from aconfig file with parse_config_file.

    If a is given, it will be run with the new value wheneverthe option is changed. This can be used to combine command-lineand file-based options:







      With this definition, options in the file specified by —config willoverride options set earlier on the command line, but can be overriddenby later flags.
      group_dict(_group)

      The names and values of options in a group.

      Useful for copying options into Application settings:








      3.1 新版功能.

      groups()

      The set of option-groups created by define.


      3.1 新版功能.

      items()

      A sequence of (name, value) pairs.


      3.1 新版功能.

      mockable()

      Returns a wrapper around self that is compatible with.

      The mock.patch function (included inthe standard library package since Python 3.3,or in the third-party mock package for older versions ofPython) is incompatible with objects like options thatoverride __getattr
      and __setattr
      . This functionreturns an object that can be used with mock.patch.object to modify option values:



      1. with mock.patch.object(options.mockable(), 'name', value):
        assert options.name == value



      parse_command_line(_args=None, final=True)

      Parses all options given on the command line (defaults to).

      Note that args[0] is ignored since it is the program namein sys.argv.

      We return a list of all arguments that are not parsed as options.

      If final is False, parse callbacks will not be run.This is useful for applications that wish to combine configurationsfrom multiple sources.
      parseconfig_file(_path, final=True)

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

      If final is False, parse callbacks will not be run.This is useful for applications that wish to combine configurationsfrom multiple sources.


      在 4.1 版更改: Config files are now always interpreted as utf-8 instead ofthe system default encoding.



      在 4.4 版更改: The special variable file is available inside configfiles, specifying the absolute path to the config file itself.

      printhelp(_file=None)

      Prints all the command line options to stderr (or another file).

      原文: