1.10.21. fejezet, STDIN kezelése
Beküldte pzoli - 2023, augusztus 4 - 8:06du
Readline module
npm install --save-dev @types/node
Minta kód
import readline from 'readline'; const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) const q = (question: string) => { return new Promise((resolve, reject) => { rl.question(question, (answer) => { resolve(answer); }); }); } const main = async () => { const answer = await q('Enter answer: '); console.log(`Your answer is: ${answer}`); rl.close() } main()
(A következő modulok "npm run dev" futtatásával nem jól működnek, de npx "ts-node src/index.ts" futtatásával korrektek.)
Prompt modul (*)
npm i prompt npm i --save-dev @types/prompt
Minta kód
import prompt from 'prompt'; var schema = { properties: { name: { pattern: /^[a-zA-Z\s\-]+$/, message: 'Name must be only letters, spaces, or dashes', required: true }, password: { description: 'Enter your password', pattern: /^\w+$/, message: 'Password must be letters', hidden: true, replace: '*', default: 'lamepassword', required: true, } } }; prompt.start(); prompt.get(schema, function (err, result) { console.log('Command-line input received:'); console.log(' name: ' + result.name); console.log(' password: ' + result.password); });
Prompts modul (*)
npm install --save prompts npm i --save-dev @types/prompts
Minta kód
import prompts from 'prompts'; async function getPassword(message: string) { const response = await prompts({ type: 'password', name: 'value', message: message, }); console.log("password:", response.value); } getPassword("Enter your password:");
Inquirer modul (* friss)
npm i @inquirer/password
Minta kód
import password from '@inquirer/password'; async function getPassword(message: string) { const answer = await password({ message: message, mask: '*' }); return answer; } getPassword("Enter password:").then((result) => { console.log("Your password is:", result); })
- A hozzászóláshoz be kell jelentkezni