Skip to content

Commit

Permalink
app: Add -q argument for quiet mode
Browse files Browse the repository at this point in the history
Add a west argument to set verbosity to the quiet mode

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
  • Loading branch information
pdgendt committed Jul 22, 2024
1 parent 155b242 commit 0fe68ea
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/west/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class EarlyArgs(NamedTuple):
version: bool # True if -V was given
zephyr_base: Optional[str] # -z argument value
verbosity: int # 0 if not given, otherwise counts
quiet: bool # True if -q was given
command_name: Optional[str]

# Other arguments are appended here.
Expand All @@ -73,6 +74,7 @@ def parse_early_args(argv: ListType[str]) -> EarlyArgs:
version = False
zephyr_base = None
verbosity = 0
quiet = False
command_name = None
unexpected_arguments = []

Expand All @@ -81,7 +83,7 @@ def parse_early_args(argv: ListType[str]) -> EarlyArgs:
def consume_more_args(rest):
# Handle the 'Vv' portion of 'west -hVv'.

nonlocal help, version, zephyr_base, verbosity, command_name
nonlocal help, version, zephyr_base, verbosity, quiet, command_name
nonlocal unexpected_arguments
nonlocal expecting_zephyr_base

Expand All @@ -97,6 +99,9 @@ def consume_more_args(rest):
elif rest.startswith('v'):
verbosity += 1
consume_more_args(rest[1:])
elif rest.startswith('q'):
quiet = True
consume_more_args(rest[1:])
elif rest.startswith('z'):
if not rest[1:]:
expecting_zephyr_base = True
Expand All @@ -121,6 +126,9 @@ def consume_more_args(rest):
elif arg.startswith('-v'):
verbosity += 1
consume_more_args(arg[2:])
elif arg.startswith('-q'):
quiet = True
consume_more_args(arg[2:])
elif arg == '--verbose':
verbosity += 1
elif arg.startswith('-z'):
Expand All @@ -136,7 +144,7 @@ def consume_more_args(rest):
command_name = arg
break

return EarlyArgs(help, version, zephyr_base, verbosity,
return EarlyArgs(help, version, zephyr_base, verbosity, quiet,
command_name, unexpected_arguments)

class LogFormatter(logging.Formatter):
Expand Down Expand Up @@ -210,7 +218,7 @@ def run(self, argv):
logging.getLogger('pykwalify').setLevel(logging.CRITICAL)

# Use verbosity to determine west API log levels
self.setup_west_logging(early_args.verbosity)
self.setup_west_logging(early_args.verbosity, early_args.quiet)

# Makes ANSI color escapes work on Windows, and strips them when
# stdout/stderr isn't a terminal
Expand Down Expand Up @@ -465,6 +473,9 @@ def make_parsers(self):
help='''Display verbose output. May be given
multiple times to increase verbosity.''')

parser.add_argument('-q', '--quiet', action='store_true',
help='quiet mode, display no output')

parser.add_argument('-V', '--version', action='version',
version=f'West version: v{__version__}',
help='print the program version and exit')
Expand Down Expand Up @@ -585,7 +596,7 @@ def print_usage_and_exit(self, message):
self.west_parser.print_usage(file=sys.stderr)
sys.exit(message)

def setup_west_logging(self, verbosity):
def setup_west_logging(self, verbosity, quiet):
logger = logging.getLogger('west.manifest')

if verbosity >= 2:
Expand All @@ -595,6 +606,8 @@ def setup_west_logging(self, verbosity):
else:
logger.setLevel(logging.WARNING)

logger.disabled = bool(quiet)

logger.addHandler(LogHandler())

def run_builtin(self, args, unknown):
Expand Down Expand Up @@ -1068,8 +1081,11 @@ def mie_msg(mie):
return ret

def adjust_command_verbosity(command, args):
command.verbosity = min(command.verbosity + args.verbose,
Verbosity.DBG_EXTREME)
if args.quiet:
command.verbosity = Verbosity.QUIET
else:
command.verbosity = min(command.verbosity + args.verbose,
Verbosity.DBG_EXTREME)

def dump_traceback():
# Save the current exception to a file and return its path.
Expand Down
9 changes: 9 additions & 0 deletions src/west/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ def inf(self, *args, colorize: bool = False, end: str = '\n'):
is undefined or true, and stdout is a terminal, then
the message is printed in green.
'''
if Verbosity.INF > self.verbosity:
return

if not self.color_ui:
colorize = False

Expand Down Expand Up @@ -460,6 +463,9 @@ def wrn(self, *args, end: str = '\n'):
:param args: sequence of arguments to print.'''

if Verbosity.WRN > self.verbosity:
return

if self.color_ui:
print(WRN_COLOR, end='', file=sys.stderr)

Expand All @@ -484,6 +490,9 @@ def err(self, *args, fatal: bool = False, end: str = '\n'):
"FATAL ERROR: "; otherwise, "ERROR: " is used.
'''

if Verbosity.ERR > self.verbosity:
return

if self.color_ui:
print(ERR_COLOR, end='', file=sys.stderr)

Expand Down

0 comments on commit 0fe68ea

Please sign in to comment.