Skip to content

Commit 2175830

Browse files
committed
Handle -d/--debug and -q/--quiet as early as possible
Fixes #32 Signed-off-by: Pedro Algarvio <[email protected]>
1 parent 7eb45cf commit 2175830

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

changelog/32.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Handle `-d/--debug` and `-q/--quiet` as early as possible

src/ptscripts/parser.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ class Context:
8686
Context class passed to every command group function as the first argument.
8787
"""
8888

89-
def __init__(self, parser: Parser):
89+
def __init__(self, parser: Parser, debug: bool = False, quiet: bool = False):
9090
self.parser = parser
91+
self._quiet = quiet
92+
self._debug = debug
9193
self.repo_root = parser.repo_root
9294
theme = Theme(
9395
{
@@ -122,13 +124,15 @@ def debug(self, *args):
122124
"""
123125
Print debug message to stderr.
124126
"""
125-
self.console.log(*args, style="log-debug", _stack_offset=2)
127+
if self._debug:
128+
self.console.log(*args, style="log-debug", _stack_offset=2)
126129

127130
def info(self, *args):
128131
"""
129132
Print info message to stderr.
130133
"""
131-
self.console.log(*args, style="log-info", _stack_offset=2)
134+
if not self._quiet:
135+
self.console.log(*args, style="log-info", _stack_offset=2)
132136

133137
def warn(self, *args):
134138
"""
@@ -339,9 +343,21 @@ def __new__(cls):
339343
Method that instantiates a singleton class and returns it.
340344
"""
341345
if cls._instance is None:
346+
# Let's do a litle manual parsing so that we can set debug or quiet early
347+
debug = False
348+
quiet = False
349+
for arg in sys.argv[1:]:
350+
if not arg.startswith("-"):
351+
break
352+
if arg in ("-q", "--quiet"):
353+
quiet = True
354+
break
355+
if arg in ("-d", "--debug"):
356+
debug = True
357+
break
342358
instance = super().__new__(cls)
343359
instance.repo_root = pathlib.Path.cwd()
344-
instance.context = Context(instance)
360+
instance.context = Context(instance, debug=debug, quiet=quiet)
345361
instance.parser = argparse.ArgumentParser(
346362
prog="tools",
347363
description="Python Tools Scripts",
@@ -443,12 +459,18 @@ def parse_args(self):
443459
self._process_registered_tool_modules()
444460
options = self.parser.parse_args()
445461
if options.quiet:
462+
self.context._quiet = True
463+
self.context._debug = False
446464
logging.root.setLevel(logging.CRITICAL + 1)
447465
elif options.debug:
466+
self.context._quiet = False
467+
self.context._debug = True
448468
logging.root.setLevel(logging.DEBUG)
449469
self.context.console.log_path = True
450470
self.context.console_stdout.log_path = True
451471
else:
472+
self.context._quiet = False
473+
self.context._debug = False
452474
logging.root.setLevel(logging.INFO)
453475
if options.timestamps:
454476
for handler in logging.root.handlers:
@@ -466,6 +488,8 @@ def __getattr__(self, attr):
466488
"""
467489
Proxy unknown attributes to the parser instance.
468490
"""
491+
if attr == "options":
492+
return self.__getattribute__(attr)
469493
return getattr(self.parser, attr)
470494

471495

0 commit comments

Comments
 (0)