From c0661643fc8b454f758e2e6a15f0b3edfd6437ef Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 13 Jan 2022 16:33:24 +0100 Subject: [PATCH] docs: an example using inquirer prompting (#2114) --- docs/examples.md | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/docs/examples.md b/docs/examples.md index 72f3f207f..ece17f2ed 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,32 @@ s.on('end', function () { $ node line_count.js count -f line_count.js 25 + +Using inquirer for prompting +--------------------------- + +```js +const yargs = require('yargs'); +const inquirer = require('inquirer'); + +const sing = () => console.log('🎵 Oy oy oy'); + +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', () => {}, askName) + .command('sing', 'a classic yargs command without prompting', () => {}, sing) + .demandCommand(1, 1, 'choose a command: ask or sing') + .strict() + .help('h').argv; +```