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 1 commit
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
44 changes: 42 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,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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than using a switch statement, you could do this:

.command('ask', 'use inquirer to prompt for your name', () => {}, async () => {
   await askName();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, it's working without the async/await though

.command('ask', 'use inquirer to prompt for your name', () => {}, askName)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the advantage of async await is that you could await the top level yargs.argv and it shouln't return until you've filled in the prompted messages ... neat right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I've first tested with your suggestion, and it was not working as expected, not sure why;

.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`);
bcoe marked this conversation as resolved.
Show resolved Hide resolved
yargs.showHelp();
}
```