Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added an example using inquirer prompting #2114

Merged
merged 3 commits into from Jan 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 31 additions & 2 deletions docs/examples.md
Expand Up @@ -302,7 +302,7 @@ s.on('end', function () {
```

***
$ node line_count.js
$ node line_count.js
Usage: line_count.js <command> [options]

Commands:
Expand All @@ -319,7 +319,7 @@ s.on('end', function () {
copyright 2019

Missing required argument: f

$ node line_count.js count
line_count.js count

Expand All @@ -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;
```