首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读行过滤答案

读行过滤答案
EN

Stack Overflow用户
提问于 2022-02-08 12:50:50
回答 1查看 68关注 0票数 0

我想在nodejs的内置模块中过滤答案,称为"readline“,它允许您提示用户并问一些问题,yk之类的东西,我已经为我的用户创建了一个js文件,以方便地理解在我的不和谐的机器人上该做什么。安装依赖关系,git拉,耶,它解释它。但我希望它只接受某些答案。这是你理解它的代码

代码语言:javascript
复制
const readline = require("readline")
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });

console.log("Hello! Welcome to the startup file of my bot dio! You can use this file to easily install,uninstall and update files!")
console.log('Type npm install if you want to install the needed packages. ')
console.log('Type git pull to update the whole folder but it needs a token and username so ye just enter it.')
console.log('Type node index.js of you want to start the bot')

rl.question('Now, what do you want to do? ', (answer) => {
    const { execSync } = require('child_process')
    const start = () => {
        execSync(`${answer}`, { stdio: "inherit" });
      }
start();

rl.close();
  }); 

我希望这段代码只接受我提供的答案(npm安装,git拉,节点index.js)

EN

回答 1

Stack Overflow用户

发布于 2022-02-09 03:07:35

我终于拿到了!我只是用了if数组lol。我完全忘记了。这是密码

代码语言:javascript
复制
const readline = require("readline")
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });
const answers = ['npm install', 'git pull', 'node index.js']

console.log("Hello! Welcome to the startup file of my bot dio! You can use this file to easily install,uninstall and update files!")
console.log('Type npm install if you want to install the needed packages. ')
console.log('Type git pull to update the whole folder but it needs a token and username so ye just enter it.')
console.log('Type node index.js of you want to start the bot')


rl.question('What do you want to do? ', (answer) => {
  const { execSync } = require('child_process');
    if(answer === answers[0]){
        console.log('Installing packages...')
        execSync('npm install', {stdio: 'inherit'})
        console.log('Installed!')
    }
    else if(answer === answers[1]){
        console.log('Updating...')
        execSync('git pull', {stdio: 'inherit'})
        console.log('Updated!')
    }
    else if(answer === answers[2]){
        console.log('Starting...')
        execSync('node index.js', {stdio: 'inherit'})
        console.log('Started!')
    }
    else{
        rl.setPrompt('Wrong input! Try again!')
        rl.prompt()
    }
    rl.close();
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71034223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档