From 38570780e51dcf6b258b6c1d499d516d346a2689 Mon Sep 17 00:00:00 2001 From: chrisdugne Date: Tue, 4 Jan 2022 09:13:34 +0100 Subject: [PATCH 1/2] added an example using inquirer prompting --- docs/examples.md | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/docs/examples.md b/docs/examples.md index 72f3f207f..2521afc24 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -302,7 +302,7 @@ s.on('end', function () { ``` *** - $ node line_count.js + $ node line_count.js Usage: line_count.js [options] Commands: @@ -319,7 +319,7 @@ s.on('end', function () { copyright 2019 Missing required argument: f - + $ node line_count.js count line_count.js count @@ -337,3 +337,43 @@ s.on('end', function () { $ node line_count.js count -f line_count.js 25 + +Using inquirer for prompting +--------------------------- + +```js +const inquirer = require('inquirer'); +const yargs = require('yargs'); + +const askName = async () => { + const answers = await inquirer.prompt([ + { + message: 'What is your name?', + name: 'name', + type: 'string' + } + ]); + + console.log(`Hello, ${answers.name}!`); +}; + +const argv = yargs(process.argv.splice(2)) + .command('ask', 'use inquirer to prompt for your name') + .command('sing', 'a classic yargs command without prompting') + .demandCommand(1, 1, 'choose a command: ask or sing') + .help('h').argv; + +const command = argv._[0]; + +switch (command) { + case 'ask': + askName(); + break; + case 'sing': + console.log('🎵 Oy oy oy'); + break; + default: + console.error(`❌ unknown command ${command}\n`); + yargs.showHelp(); +} +``` From 921dcc6084d33f333537e04a761333b3cbf5f493 Mon Sep 17 00:00:00 2001 From: chrisdugne Date: Thu, 13 Jan 2022 14:04:27 +0100 Subject: [PATCH 2/2] using strict(), replaced the switch by yargs handlers --- docs/examples.md | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/docs/examples.md b/docs/examples.md index 2521afc24..ece17f2ed 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -342,8 +342,10 @@ Using inquirer for prompting --------------------------- ```js -const inquirer = require('inquirer'); const yargs = require('yargs'); +const inquirer = require('inquirer'); + +const sing = () => console.log('🎵 Oy oy oy'); const askName = async () => { const answers = await inquirer.prompt([ @@ -358,22 +360,9 @@ const askName = async () => { }; const argv = yargs(process.argv.splice(2)) - .command('ask', 'use inquirer to prompt for your name') - .command('sing', 'a classic yargs command without prompting') + .command('ask', 'use inquirer to prompt for your name', () => {}, askName) + .command('sing', 'a classic yargs command without prompting', () => {}, sing) .demandCommand(1, 1, 'choose a command: ask or sing') + .strict() .help('h').argv; - -const command = argv._[0]; - -switch (command) { - case 'ask': - askName(); - break; - case 'sing': - console.log('🎵 Oy oy oy'); - break; - default: - console.error(`❌ unknown command ${command}\n`); - yargs.showHelp(); -} ```