【问题标题】:Asking the user to confirm before executing the code using Yargs在使用 Yargs 执行代码之前要求用户确认
【发布时间】:2020-10-21 14:09:06
【问题描述】:
CLI 的用户可以使用 yargs 指定他们想要使用参数调用哪个命令,然后 CLI 在运行代码之前回复以下消息:
您确定要使用以下设置在环境 X 上执行此代码: ....
是/否:
Y 将执行代码
N 什么都不做
更新:
刚刚找到了一个名为 yargs-interactive 的 npm 模块。我想这就是我的问题的答案。
【问题讨论】:
标签:
javascript
node.js
command-line-interface
yargs
【解决方案1】:
const yargsInteractive = require(‘yargs-interactive’);
const options = {
name: {
type: ‘input’,
default: ‘A robot’,
describe: ‘Enter your name’
},
likesPizza: {
type: ‘confirm’,
default: false,
describe: ‘Do you like pizza?’
},
};
yargsInteractive()
.usage(‘$0 <command> [args]’)
.interactive(options)
.then((result) => {
// Your business logic goes here.
// Get the arguments from the result
// (e.g. result.name)
console.log(result.name);
});