app: Add -q argument for quiet mode

Add a west argument to decrease the verbosity.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
This commit is contained in:
Pieter De Gendt 2024-07-22 11:16:35 +02:00 committed by Marc Herbert
parent d55f585af6
commit 0612a6ddae
3 changed files with 36 additions and 5 deletions

View File

@ -97,6 +97,9 @@ def parse_early_args(argv: ListType[str]) -> EarlyArgs:
elif rest.startswith('v'):
verbosity += 1
consume_more_args(rest[1:])
elif rest.startswith('q'):
verbosity -= 1
consume_more_args(rest[1:])
elif rest.startswith('z'):
if not rest[1:]:
expecting_zephyr_base = True
@ -121,8 +124,13 @@ def parse_early_args(argv: ListType[str]) -> EarlyArgs:
elif arg.startswith('-v'):
verbosity += 1
consume_more_args(arg[2:])
elif arg.startswith('-q'):
verbosity -= 1
consume_more_args(arg[2:])
elif arg == '--verbose':
verbosity += 1
elif arg == '--quiet':
verbosity -= 1
elif arg.startswith('-z'):
if arg == '-z':
expecting_zephyr_base = True
@ -465,6 +473,10 @@ class WestApp:
help='''Display verbose output. May be given
multiple times to increase verbosity.''')
parser.add_argument('-q', '--quiet', default=0, action='count',
help='''Display less verbose output. May be given
multiple times to decrease verbosity.''')
parser.add_argument('-V', '--version', action='version',
version=f'West version: v{__version__}',
help='print the program version and exit')
@ -485,7 +497,7 @@ class WestApp:
# Set up logging verbosity before running the command, for
# backwards compatibility. Remove this when we can part ways
# with the log module.
log.set_verbosity(args.verbose)
log.set_verbosity(args.verbose - args.quiet)
# If we were run as 'west -h ...' or 'west --help ...',
# monkeypatch the args namespace so we end up running Help. The
@ -594,8 +606,12 @@ class WestApp:
logger.setLevel(logging.DEBUG)
elif verbosity == 1:
logger.setLevel(logging.INFO)
else:
elif verbosity == 0:
logger.setLevel(logging.WARNING)
elif verbosity == -1:
logger.setLevel(logging.ERROR)
else:
logger.setLevel(logging.CRITICAL)
logger.addHandler(LogHandler())
@ -1070,8 +1086,10 @@ def mie_msg(mie):
return ret
def adjust_command_verbosity(command, args):
command.verbosity = min(command.verbosity + args.verbose,
Verbosity.DBG_EXTREME)
command.verbosity = max(
min(command.verbosity + args.verbose - args.quiet, Verbosity.DBG_EXTREME),
Verbosity.QUIET
)
def dump_traceback():
# Save the current exception to a file and return its path.

View File

@ -408,7 +408,7 @@ class WestCommand(ABC):
:param args: sequence of arguments to print
:param level: verbosity level of the message
'''
if level > self.verbosity:
if self.verbosity < level:
return
print(*args, end=end)
@ -422,6 +422,9 @@ class WestCommand(ABC):
is undefined or true, and stdout is a terminal, then
the message is printed in green.
'''
if self.verbosity < Verbosity.INF:
return
if not self.color_ui:
colorize = False
@ -460,6 +463,9 @@ class WestCommand(ABC):
:param args: sequence of arguments to print.'''
if self.verbosity < Verbosity.WRN:
return
if self.color_ui:
print(WRN_COLOR, end='', file=sys.stderr)
@ -484,6 +490,9 @@ class WestCommand(ABC):
"FATAL ERROR: "; otherwise, "ERROR: " is used.
'''
if self.verbosity < Verbosity.ERR:
return
if self.color_ui:
print(ERR_COLOR, end='', file=sys.stderr)

View File

@ -87,6 +87,8 @@ def inf(*args, colorize=False):
the message is printed in green.
'''
deprecated()
if VERBOSE < 0:
return
if not _use_colors():
colorize = False
@ -126,6 +128,8 @@ def wrn(*args):
If the configuration option ``color.ui`` is undefined or true and
stdout is a terminal, then the message is printed in yellow.'''
deprecated()
if VERBOSE < -1:
return
if _use_colors():
print(WRN_COLOR, end='', file=sys.stderr)