west icon indicating copy to clipboard operation
west copied to clipboard

app: support alias commands

Open pdgendt opened this issue 1 year ago • 5 comments

This PR allows to add alias west commands to a configuration, similar to how git aliases work.

It can override existing commands to have default arguments, and iteratively replace alias commands.

Some examples:

# Add a global "west run" shortcut (will override pristine build)
$ west config --global alias.run "build --pristine=never -t run"

# Add a global "west menuconfig" shortcut
$ west config --global alias.menuconfig "build --pristine=never -t menuconfig"

# A shortcut to build my current project (prefix the path with $PWD/ to build anywhere from the workspace)
$ west config alias.my_proj "build -b native_sim samples/hello_world -- -DCONFIG_ASSERT=y"

# WARNING: overriding built-in or other commands is at your own risk
# build pristine by default locally
$ west config alias.build "build --pristine=always"

After applying you can run:

$ west run         #> west build --pristine=always --pristine=never -t run
$ west menuconfig  #> west build --pristine=always --pristine=never -t menuconfig
$ west my_proj     #> west build --pristine=always -b native_sim samples/hello_world -- -DCONFIG_ASSERT=y

pdgendt avatar Jul 17 '24 11:07 pdgendt

Updated the PR to have west help <alias> print out info about the alias.

pdgendt avatar Jul 18 '24 08:07 pdgendt

Calling west help now also lists alias commands.

pdgendt avatar Jul 19 '24 11:07 pdgendt

@marc-hb Thanks for the review! I will look into the difference between west help some_alias and west -h some_alias and the alias issue on windows.

pdgendt avatar Aug 27 '24 21:08 pdgendt

I will look into the difference between west help some_alias and west -h some_alias and the alias issue on windows.

I just did some interactive west config testing with both Powershell and CMD.EXE. You can nest single quotes inside double quotes in both but not the other way round. Don't ask me why. That's hopefully the only reason why https://github.com/zephyrproject-rtos/west/actions/runs/10585812818/job/29333273125?pr=716 failed.

If we really wanted to test quoting more then we should check some pre-canned config file in git and avoid command line peculiarities entirely. But for now I think restricting tests to single quotes nested in double quotes is portable and plenty good enough. We just want to make sure no one breaks the most basic whitespace usage and then trust shlex for the rest.

marc-hb avatar Aug 28 '24 04:08 marc-hb

@marc-hb I've handled all comments, I think.

pdgendt avatar Aug 28 '24 09:08 pdgendt

Just tried the latest https://github.com/zephyrproject-rtos/west/commit/93f9715c6410023986065da87709088cccea5b78 and infinite loops do not seem handled well. From reading the code I would expect this to print west: unknown command alpha but instead I get:

west help

aliases:
  alpha:                beta
  beta:                 alpha

west alpha

Traceback (most recent call last):
  File "~/.local/bin/west", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "src/west/app/main.py", line 1172, in main
    app.run(argv or sys.argv[1:])
  File "src/west/app/main.py", line 255, in run
    self.run_command(argv, early_args)
  File "src/west/app/main.py", line 556, in run_command
    self.run_extension(args.command, argv)
  File "src/west/app/main.py", line 682, in run_extension
    self.cmd = self.extensions[name].factory()
               ~~~~~~~~~~~~~~~^^^^^^
KeyError: 'alpha'

More tests needed maybe?

I warned you recursion (and overriding built ins) was fraught with peril! :-)

marc-hb avatar Sep 04 '24 07:09 marc-hb

I warned you recursion (and overriding built ins) was fraught with peril! :-)

Yes, the early argument handling does make it a bit more complicated, but I think I've managed to make it more robust now.

More tests needed maybe?

I added a test for the infinite recursion to contain "unknown command".

pdgendt avatar Sep 04 '24 08:09 pdgendt

Update: handle empty aliases and add a test

pdgendt avatar Sep 04 '24 10:09 pdgendt

Update: handle empty aliases

west help still broken with empty aliases and latest commit https://github.com/zephyrproject-rtos/west/commit/f8c61d1e9a11f46f5f9977e957b60c883ec72c46

west config alias.bar ""

west bar
usage: west [-h] [-z ZEPHYR_BASE] [-v] [-q] [-V] <command> ...
west: empty alias "bar"

west help

Traceback (most recent call last):
  File "/var/home/.local/bin/west", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "src/west/app/main.py", line 1175, in main
    app.run(argv or sys.argv[1:])
  File "src/west/app/main.py", line 255, in run
    self.run_command(argv, early_args)
  File "src/west/app/main.py", line 557, in run_command
    self.run_builtin(args, unknown)
  File "src/west/app/main.py", line 673, in run_builtin
    self.cmd.run(args, unknown, self.topdir,
  File "src/west/commands.py", line 194, in run
    self.do_run(args, unknown)
  File "src/west/app/main.py", line 862, in do_run
    app.west_parser.print_help(top_level=True)
  File "src/west/app/main.py", line 927, in print_help
    print(self.format_help(top_level=top_level), end='',
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "src/west/app/main.py", line 1014, in format_help
    self.format_command(append, alias, width)
  File "src/west/app/main.py", line 1039, in format_command
    self.format_thing_and_help(append, thing, command.help, width)
  File "src/west/app/main.py", line 1075, in format_thing_and_help
    help_lines[0] = thing + help_lines[0][thinglen:]
                            ~~~~~~~~~~^^^
IndexError: list index out of range

marc-hb avatar Sep 04 '24 18:09 marc-hb

west help still broken with empty aliases and latest commit f8c61d1

Ugh, sorry about that, and thanks for all the testing efforts! Empty help strings seem to get stripped. I now set an <empty> placeholder. Added it to the test.

pdgendt avatar Sep 04 '24 19:09 pdgendt

Fixed typo 🤦

pdgendt avatar Sep 04 '24 19:09 pdgendt