Skip to content

Commit 41767af

Browse files
mbolivar-nordiccarlescufi
authored andcommitted
west: add example west command
Add a working example for how to implement a west command within the user's manifest repository. There is documentation for this, but we should have some working code in here just to make life easier for people. Signed-off-by: Martí Bolívar <[email protected]>
1 parent 2f6038d commit 41767af

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88

99
# build
1010
/build*
11+
12+
__pycache__/

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ applications. Some of the features demonstrated in this example are:
1111
- Custom [devicetree bindings][bindings]
1212
- Out-of-tree [drivers][drivers]
1313
- Example CI configuration (using Github Actions)
14+
- Custom [west extension][west_ext]
1415

1516
This repository is versioned together with the [Zephyr main tree][zephyr]. This
1617
means that every time that Zephyr is tagged, this repository is tagged as well
@@ -26,6 +27,7 @@ point to the development branch of Zephyr, also `main`.
2627
[bindings]: https://docs.zephyrproject.org/latest/guides/dts/bindings.html
2728
[drivers]: https://docs.zephyrproject.org/latest/reference/drivers/index.html
2829
[zephyr]: https://github.com/zephyrproject-rtos/zephyr
30+
[west_ext]: https://docs.zephyrproject.org/latest/develop/west/extensions.html
2931

3032
## Getting Started
3133

scripts/example_west_command.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright (c) 2019 Foundries.io
2+
# Copyright (c) 2022 Nordic Semiconductor ASA
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
'''example_west_command.py
6+
7+
Example of a west extension in the example-application repository.'''
8+
9+
from west.commands import WestCommand # your extension must subclass this
10+
from west import log # use this for user output
11+
12+
class ExampleWestCommand(WestCommand):
13+
14+
def __init__(self):
15+
super().__init__(
16+
'example-west-command', # gets stored as self.name
17+
'an example west extension command', # self.help
18+
# self.description:
19+
'''\
20+
A multi-line description of example-west-command.
21+
22+
You can split this up into multiple paragraphs and they'll get
23+
reflowed for you. You can also pass
24+
formatter_class=argparse.RawDescriptionHelpFormatter when calling
25+
parser_adder.add_parser() below if you want to keep your line
26+
endings.''')
27+
28+
def do_add_parser(self, parser_adder):
29+
# This is a bit of boilerplate, which allows you full control over the
30+
# type of argparse handling you want. The "parser_adder" argument is
31+
# the return value of an argparse.ArgumentParser.add_subparsers() call.
32+
parser = parser_adder.add_parser(self.name,
33+
help=self.help,
34+
description=self.description)
35+
36+
# Add some example options using the standard argparse module API.
37+
parser.add_argument('-o', '--optional', help='an optional argument')
38+
parser.add_argument('required', help='a required argument')
39+
40+
return parser # gets stored as self.parser
41+
42+
def do_run(self, args, unknown_args):
43+
# This gets called when the user runs the command, e.g.:
44+
#
45+
# $ west my-command-name -o FOO BAR
46+
# --optional is FOO
47+
# required is BAR
48+
log.inf('--optional is', args.optional)
49+
log.inf('required is', args.required)

scripts/west-commands.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
west-commands:
2+
- file: scripts/example_west_command.py
3+
commands:
4+
- name: example-west-command
5+
class: ExampleWestCommand
6+
help: an example west extension command

west.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
manifest:
55
self:
66
path: example-application
7+
west-commands: scripts/west-commands.yml
78

89
remotes:
910
- name: zephyrproject-rtos

0 commit comments

Comments
 (0)