From 04d53783f036589469b2f393fdea333608d45242 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Thu, 14 Nov 2024 14:54:40 +0100 Subject: [PATCH] app: config: Mutually exclusive group for local/global/system argument Use builtin mutually exclusive group support instead of a custom action. Signed-off-by: Pieter De Gendt --- src/west/app/config.py | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/src/west/app/config.py b/src/west/app/config.py index cfb5236b..a3729b13 100644 --- a/src/west/app/config.py +++ b/src/west/app/config.py @@ -71,23 +71,6 @@ GLOBAL = ConfigFile.GLOBAL LOCAL = ConfigFile.LOCAL -class Once(argparse.Action): - # For enforcing mutual exclusion of options by ensuring self.dest - # can only be set once. - # - # This sets the 'configfile' attribute depending on the option string, - # which must be --system, --global, or --local. - - def __call__(self, parser, namespace, ignored, option_string=None): - values = {'--system': SYSTEM, '--global': GLOBAL, '--local': LOCAL} - rev = {v: k for k, v in values.items()} - - if getattr(namespace, self.dest): - previous = rev[getattr(namespace, self.dest)] - parser.error(f"argument {option_string}: " - f"not allowed with argument {previous}") - - setattr(namespace, self.dest, values[option_string]) class Config(WestCommand): def __init__(self): @@ -113,12 +96,17 @@ def do_add_parser(self, parser_adder): help="delete an option everywhere it's set") group = parser.add_argument_group( - 'configuration file to use (give at most one)') - group.add_argument('--system', dest='configfile', nargs=0, action=Once, + "configuration file to use (give at most one)" + ).add_mutually_exclusive_group() + + group.add_argument('--system', dest='configfile', + action='store_const', const=SYSTEM, help='system-wide file') - group.add_argument('--global', dest='configfile', nargs=0, action=Once, + group.add_argument('--global', dest='configfile', + action='store_const', const=GLOBAL, help='global (user-wide) file') - group.add_argument('--local', dest='configfile', nargs=0, action=Once, + group.add_argument('--local', dest='configfile', + action='store_const', const=LOCAL, help="this workspace's file") parser.add_argument('name', nargs='?',