|
| 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) |
0 commit comments