Skip to content

Commit

Permalink
app: config: Mutually exclusive group for local/global/system argument
Browse files Browse the repository at this point in the history
Use builtin mutually exclusive group support instead of a custom action.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
  • Loading branch information
pdgendt committed Nov 14, 2024
1 parent d6b34df commit 04d5378
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions src/west/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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='?',
Expand Down

0 comments on commit 04d5378

Please sign in to comment.