V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
sigoden
V2EX  ›  分享创造

Clii - 编写命令行从未如此简单

  •  
  •   sigoden · 2022-02-21 09:32:43 +08:00 · 1901 次点击
    这是一个创建于 793 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Clii - 编写命令行从未如此简单

    安装包

    npm i clii
    yarn add clii
    

    写一个常规函数

    /**
     * A simple task
     * @param {Object} options
     * @param {number} options.num - Num variable
     * @param {("prod"|"dev"|"stage")} options.mode - Build mode
     */
    export async function task(options) {
      console.log(options);
    }
    

    引入 Clii

    import clii from "clii";
    
    clii(import.meta.url);
    

    一个命令行完成了

    clii task1 [options]
    
    A simple task
    
    Options:
          --version  Show version number                                   [boolean]
      -f, --file     Specific clii file                                     [string]
      -w, --workdir  Specific working directory                             [string]
          --num      Num variable                                           [number]
          --mode     Build mode           [string] [choices: "prod", "dev", "stage"]
      -h, --help     Show help                                             [boolean]
    

    你可以使用 clii 管理项目脚本。

    编写cliifile.mjs(相当于 js 版本的 Makefile)

    function lint() {}
    /**
     * Build
     * @param {Object} options
     * @param {boolean} prod
     */
    function build(options) {
      lint()
    }
    
    $ clii
    Usage: clii <cmd> [options]
    
    Commands:
      clii lint
      clii build [options]  Build
    
    Options:
          --version  Show version number                                   [boolean]
      -f, --file     Specific clii file                                     [string]
      -w, --workdir  Specific working directory                             [string]
      -h, --help     Show help                                             [boolean]
    
    $ clii lint
    $ clii build
    

    仓库: https://github.com/sigoden/clii

    2 条回复    2022-02-21 10:17:11 +08:00
    xujiahui
        1
    xujiahui  
       2022-02-21 10:08:28 +08:00
    可以看看 zx ( https://github.com/google/zx ),挺火的
    sigoden
        2
    sigoden  
    OP
       2022-02-21 10:17:11 +08:00
    @xujiahui clii 的着重提供命令界面。zx 是个异步 shell 执行器。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1247 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 105ms · UTC 18:00 · PVG 02:00 · LAX 11:00 · JFK 14:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.