【问题标题】:How can I create Git-style subcommands with oclif?如何使用 oclif 创建 Git 风格的子命令?
【发布时间】:2025-12-16 23:25:01
【问题描述】:

我正在使用 Heroku 的 CLI 框架 oclif 编写 CLI。它工作得很好,但我想要类似 Git 的子命令,比如:

$ mycli mycommand subcommand

$ mycli mycommand subcommand --flags="are awesome"

$ mycli mycommand another-subcommand --name="John Doe"

我浏览了文档,但找不到与命令结构、布局、层次结构等相关的任何信息。我​​可以将 mycommand 写为普通命令并打开 argv 的第一个参数,但我的子命令接受不同的标志,所以当有人运行 mycli help mycommand 时,我失去了 oclif 报告一些帮助的能力。

所以,我的问题是:使用 oclif 创建子命令的最佳方法是什么?

【问题讨论】:

    标签: node.js command-line-interface oclif


    【解决方案1】:

    您可以创建以下结构:

     - src
     --- commands // all your commands will be on this folder
     ----- subcommand1 // if you have more commands from after subcomand1 include them in this folder like the one bellow.
     ------ command.js // a simple command
     ----- subcommand2.js
    

    这将产生如下命令:

    cli subcommand1 command --some-flag --some-argument="xpto"
    cli subcommand2 --some-other-flag --some-other-argument="WAT"
    

    我注意到的一件事是,如果您想与其他命令共享一些标志或参数,您要么必须为此使用基类,要么在另一个文件中声明标志/开关/参数并将它们导入想要的命令

    【讨论】:

      最近更新 更多