Presets
duho.presets
Pre-configured argument classes for common patterns.
__all__ = ['ConfigArgs', 'LoggingArgs', 'OutputArgs']
module-attribute
ConfigArgs(**kwargs)
Bases: Args
A data mixin that adds --config FILE (-c) and reads that file.
The file's values become defaults under the command line, and its format is
chosen by its suffix (see duho.config). ConfigArgs sets
_config_field_, so list it BEFORE Cli, which declares that name as
None: class App(ConfigArgs, LoggingArgs, Cli).
Source code in src/duho/args/_argsclass.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
config = None
class-attribute
instance-attribute
__init_subclass__(**kwargs)
Source code in src/duho/presets.py
243 244 245 246 247 248 249 250 | |
LoggingArgs(**kwargs)
Bases: Args
Args subclass with built-in -v/-q and --loglevel support.
LoggingArgs is a data mixin (verbosity fields + _set_loglevels_
+ the _logger_ property); it defines no __call__ and is
NOT itself runnable. Since the Args/Cmd split, combine it
with Cmd to get a runnable command with logging::
class MyApp(LoggingArgs, Cmd):
_version_ = "1.2.3"
def __call__(self):
self._logger_.info("running")
return 0
Recommended base order: (LoggingArgs, Cmd) -- data mixin first,
executable base last (reads "add logging to a command"). Both orders
resolve correctly because LoggingArgs overrides no Cmd member;
_logger_/_set_loglevels_ come from LoggingArgs and
__call__ from Cmd regardless of order.
Set a class attr _version_ to opt into a --version flag; no
separate preset class is needed. --version prints
"%(prog)s 1.2.3" and exits 0. It is skipped if a version-dest
action already exists (e.g. supplied by a parent parser).
Source code in src/duho/args/_argsclass.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
loglevels = {}
class-attribute
instance-attribute
quiet = 0
class-attribute
instance-attribute
verbose = 0
class-attribute
instance-attribute
OutputArgs(**kwargs)
Bases: Args
A data mixin that adds --format {text,json} and --json, and _output_.
self._output_(value, text=None) prints value as the user asked: indented
JSON for --json or --format json, else text (a str, or a callable
taking value and returning one) or, with no text, duho's own rendering of
value. Nothing is printed implicitly. A class that declares a format or a
json field of its own, or inherits one, cannot use this mixin (TypeError).
Source code in src/duho/args/_argsclass.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
format = 'text'
class-attribute
instance-attribute
json = False
class-attribute
instance-attribute
__init_subclass__(**kwargs)
Source code in src/duho/presets.py
275 276 277 278 279 280 281 282 283 284 285 | |