Skip to content

Commit

Permalink
app: support alias commands
Browse files Browse the repository at this point in the history
Replace west commands with alias set in configuration files

Signed-off-by: Pieter De Gendt <[email protected]>
  • Loading branch information
pdgendt committed Jul 17, 2024
1 parent 256a06d commit 2d3e6d0
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/west/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import os
from pathlib import Path, PurePath
import platform
import shlex
import shutil
import signal
import sys
Expand Down Expand Up @@ -175,6 +176,7 @@ def __init__(self):
self.mle = None # saved exception if load_manifest() fails
self.builtins = {} # command name -> WestCommand instance
self.extensions = {} # extension command name -> spec
self.aliases = {} # alias -> literal
self.builtin_groups = OrderedDict() # group name -> WestCommand list
self.extension_groups = OrderedDict() # project path -> ext spec list
self.west_parser = None # a WestArgumentParser
Expand Down Expand Up @@ -235,6 +237,7 @@ def run(self, argv):
# Set self.manifest and self.extensions.
self.load_manifest()
self.load_extension_specs()
self.load_aliases()

# Set up initial argument parsers. This requires knowing
# self.extensions, so it can't happen before now.
Expand Down Expand Up @@ -394,6 +397,15 @@ def load_extension_specs(self):

self.extension_groups[path] = filtered

def load_aliases(self):
if not self.config:
return

for item in self.config.items():
ids = item[0].split('.', 1)
if ids[0] == 'alias':
self.aliases[ids[1]] = item[1]

def handle_extension_command_error(self, ece):
if self.cmd is not None:
msg = f"extension command \"{self.cmd.name}\" couldn't be run"
Expand Down Expand Up @@ -479,6 +491,15 @@ def run_command(self, argv, early_args):
# If we're running an extension, instantiate it from its
# spec and re-parse arguments before running.

# Replace alias command(s) if set
while early_args.command_name in self.aliases:
new_args = shlex.split(self.aliases[early_args.command_name])
# Make sure we don't end up in an infinite loop
del self.aliases[early_args.command_name]

early_args = early_args._replace(command_name=new_args[0])
argv = new_args + argv[1:]

self.handle_early_arg_errors(early_args)
args, unknown = self.west_parser.parse_known_args(args=argv)

Expand Down

0 comments on commit 2d3e6d0

Please sign in to comment.