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.
| 131 | |
| 132 | |
| 133 | class OptionParser(object): |
| 134 | """A collection of options, a dictionary with object-like access. |
| 135 | |
| 136 | Normally accessed via static functions in the `tornado.options` module, |
| 137 | which reference a global instance. |
| 138 | """ |
| 139 | |
| 140 | def __init__(self) -> None: |
| 141 | # we have to use self.__dict__ because we override setattr. |
| 142 | self.__dict__["_options"] = {} |
| 143 | self.__dict__["_parse_callbacks"] = [] |
| 144 | self.define( |
| 145 | "help", |
| 146 | type=bool, |
| 147 | help="show this help information", |
| 148 | callback=self._help_callback, |
| 149 | ) |
| 150 | |
| 151 | def _normalize_name(self, name: str) -> str: |
| 152 | return name.replace("_", "-") |
| 153 | |
| 154 | def __getattr__(self, name: str) -> Any: |
| 155 | name = self._normalize_name(name) |
| 156 | if isinstance(self._options.get(name), _Option): |
| 157 | return self._options[name].value() |
| 158 | raise AttributeError("Unrecognized option %r" % name) |
| 159 | |
| 160 | def __setattr__(self, name: str, value: Any) -> None: |
| 161 | name = self._normalize_name(name) |
| 162 | if isinstance(self._options.get(name), _Option): |
| 163 | return self._options[name].set(value) |
| 164 | raise AttributeError("Unrecognized option %r" % name) |
| 165 | |
| 166 | def __iter__(self) -> Iterator: |
| 167 | return (opt.name for opt in self._options.values()) |
| 168 | |
| 169 | def __contains__(self, name: str) -> bool: |
| 170 | name = self._normalize_name(name) |
| 171 | return name in self._options |
| 172 | |
| 173 | def __getitem__(self, name: str) -> Any: |
| 174 | return self.__getattr__(name) |
| 175 | |
| 176 | def __setitem__(self, name: str, value: Any) -> None: |
| 177 | return self.__setattr__(name, value) |
| 178 | |
| 179 | def items(self) -> Iterable[Tuple[str, Any]]: |
| 180 | """An iterable of (name, value) pairs. |
| 181 | |
| 182 | .. versionadded:: 3.1 |
| 183 | """ |
| 184 | return [(opt.name, opt.value()) for name, opt in self._options.items()] |
| 185 | |
| 186 | def groups(self) -> Set[str]: |
| 187 | """The set of option-groups created by ``define``. |
| 188 | |
| 189 | .. versionadded:: 3.1 |
| 190 | """ |
no outgoing calls