添加west扩展命令

使用west工具的帮助命令,west -h,不仅可以列出west工具的内置命令,也可以列举当前工程中实现的扩展命令,如build,flash等。

本文将介绍如何添加扩展命令。

west扩展命令的位置通过以下方式查找:

1. 首先找到工程中.west目录的config文件,打开config,查看以下两个值:

        1)path参数的值,默认为zephyr。

        2)file参数的值,默认为west.yml。

[manifest]
path = zephyr
file = west.yml

[zephyr]
base = zephyr

2)根据path参数和file参数的值,找到对应的文件,默认为zephyr/west.yml。

在west.yml文件中,可以看到以下配置,即west扩展命令在scripts/west-commands.yml文件中定义。

  self:
    path: zephyr
    west-commands: scripts/west-commands.yml
    import: submanifests

3)如下代码所示,west-commands.yml文件中列举了当前工程支持的扩展命令,以及每个扩展命令的名称和对应的python文件。

# Keep the help strings in sync with the values in the .py files!
west-commands:
  - file: scripts/west_commands/completion.py
    commands:
      - name: completion
        class: Completion
        help: output shell completion scripts
  - file: scripts/west_commands/boards.py
    commands:
      - name: boards
        class: Boards
        help: display information about supported boards
  - file: scripts/west_commands/build.py
    commands:
      - name: build
        class: Build
        help: compile a Zephyr application

接下来,可以按照以下步骤添加一个新的扩展命令。

1)在west-commands.yml文件中添加一个新的命令。命名为new_cmd,对应的python文件为new_cmd_file.py。

注:file,name,class,help可以根据需要添加任意名称和字符串。

# Keep the help strings in sync with the values in the .py files!
west-commands:
  - file: scripts/west_commands/new_cmd_file.py
    commands:
      - name: new_cmd
        class: new_cmd_class
        help: this is a new command
  - file: scripts/west_commands/completion.py
    commands:
      - name: completion
        class: Completion
        help: output shell completion scripts
  - file: scripts/west_commands/boards.py
    commands:
      - name: boards
        class: Boards
        help: display information about supported boards

2)在west_commands目录中添加west-commands.yml文件中定义的文件new_cmd_file.py。

其中,class应该与west-commands.yml文件中定义的class一致。此外,需要实现do_add_parser和do_run函数,这两个函数将在执行west扩展命令时自动被调用。

import argparse
from zephyr_ext_common import Forceable

USAGE = '''\
This is new command USAGE.
'''

DESCRIPTION = f'''\
This is new command DESCRIPTION.
'''

class new_cmd_class(Forceable):

    def __init__(self):
        super(new_cmd_class, self).__init__(
            'new_cmd',
            # Keep this in sync with the string in west-commands.yml.
            'this is a new command',
            DESCRIPTION,
            accepts_unknown_args=True)
        print("__init__ function of new command!")

    def do_add_parser(self, parser_adder):
        parser = parser_adder.add_parser(
            self.name,
            help=self.help,
            formatter_class=argparse.RawDescriptionHelpFormatter,
            description=self.description,
            usage=USAGE)

        print("do_add_parser function of new command!")
        return parser

    def do_run(self, args, remainder):
        print("do_run function of new command!")

通过west -h帮助命令查看以上扩展命令是否成功添加。可以看出,扩展命令中已经包含new_cmd命令。

执行扩展命令,west new_cmd,如下log所示,__init__函数,do_add_parser函数和do_run函数依次被调用。

通过以上步骤,新的扩展命令已经添加完成,接下来可以根据需求进一步开发,完善扩展命令。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值