javascript - Commander JS - 验证参数

标签 javascript node.js command-line-interface

给定以下 cli 配置,我们可以通过简单地将命令式控件放入操作处理程序来验证传递的参数...

#!/usr/bin/env node

var program = require('commander');

program
  .command('greet <person>')
  .action((person, cmd) => {
    console.log(`Hello ${person}`);
  })

program.parse(process.argv)

但是,我想知道是否有实际的 api 可以这样做:

program.command('greet <Giuseppe|Mick|Bob>');

如果参数不正确,commanderjs 会自动抛出异常。

最佳答案

有可能,你可以这样做:

program
  .addArgument(new commander.Argument('<drink-size>', 'drink cup size').choices(['small', 'medium', 'large']))

请参阅 https://github.com/tj/commander.js/#more-configuration-1 的指挥官文档

关于javascript - Commander JS - 验证参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56421761/

相关文章:

javascript - 我收到错误 : Can't perform a React state update on an unmounted component even though i created cleanup

c# - 使用 .net core CLI 和 .net core 控制台应用程序有什么区别?

javascript - 根据窗口大小调整文本区域的大小

node.js - 带有 primeng 404 问题的 angular-cli@webpack

javascript - 将 Javascript 插入 ASP.Net 页面

node.js - Node : Wait the python script to write the file then upload it to s3

c# - 为什么 C# 不支持通过引用传递的运算符重载?

cordova - 无法在cordova项目中安装插件

javascript - 应用程序脚本电子表格浮点计算,谷歌应用程序脚本可以做 float 计算器吗?

JavaScript:按名称调用参数评估