【发布时间】: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