【问题标题】:Getting values from stdin with Node.js & TypeScript使用 Node.js 和 TypeScript 从标准输入获取值
【发布时间】:2020-01-22 06:50:33
【问题描述】:

我有一些任务,基本上是一个需要 x 行的 CLI 脚本,并根据所述行进行一些计算。

例如:

输入:

3 // number of iterations to do something
ATCCGCTTAGAGGGATT // first string
GTCCGTTTAGAAGGTTT // second string
// 2nd iteration starts
abcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyza
// 3rd iteration starts
abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789

输出:

ATCCGCTTAGAGGGATT // first string
GTCCGTTTAGAAGGTTT // second string
*....*.....*..*.. // visual marker of differences between the two strings(*)

abcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyza
**************************

abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789
....................................

我尝试过的:

const rl = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout,
  prompt: ''
})

let numberOfTests = 0;
let firstText = '';
let secondText = '';
let differences = '';

rl.prompt();

rl.on('line', (line: any) => {
  let loop = 0;
  numberOfTests = line;
  firstText = line;
  secondText = line;
  calculateDifferences(firstText, secondText); // basic function that loops through the arrays and makes the visual marker.
  loop ++;
  if (loop === numberOfTests) {
    rl.close();
  }
}).on('close', () => {
  console.log('Have a great day!');
  process.exit(0);
});

我面临的主要问题是我无法将输入流彼此分开。 到我们在 secondText 时,每个变量都将等于我第三次输入的值。

如何将输入彼此分开?

Node documentation 对此没有那么详细。或者我看不透。

【问题讨论】:

    标签: node.js typescript input stdin readline


    【解决方案1】:

    设法找到区分 rl 实例的解决方案。

    Following this comment is 100% working in my use-case

    【讨论】:

      猜你喜欢
      • 2020-01-20
      • 1970-01-01
      • 2011-12-31
      • 2019-09-25
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多