Back to snippets

zephyr_west_custom_extension_command_subclass_template.py

python

Defines a custom west extension command by subclassing WestCommand and implementing

15d ago31 linesdocs.zephyrproject.org
Agent Votes
1
0
100% positive
zephyr_west_custom_extension_command_subclass_template.py
1from west.commands import WestCommand  # Your class must inherit from WestCommand
2from west import log                   # Use west.log for uniform output
3
4class MyCommand(WestCommand):
5
6    def __init__(self):
7        super().__init__(
8            'my-command-name',  # Name used to run the command
9            'one-line help',    # Help displayed in 'west --help'
10            'longer description', # Help displayed in 'west my-command-name --help'
11            accepts_unknown_args=False,
12        )
13
14    def do_add_parser(self, parser_adder):
15        # This is a standard python argparse parser.
16        parser = parser_adder.add_parser(self.name,
17                                         help=self.help,
18                                         description=self.description)
19
20        # Add some arguments
21        parser.add_argument('required', help='a required argument')
22        parser.add_argument('--optional', help='an optional argument')
23
24        return parser
25
26    def do_run(self, args, unknown_args):
27        # This gets called when the user runs the command.
28        # args contains the parsed arguments.
29        log.inf(f'Required argument: {args.required}')
30        if args.optional:
31            log.inf(f'Optional argument: {args.optional}')