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
example using inquirer to prompt user #2385
base: main
Are you sure you want to change the base?
Conversation
I wonder whether the inquirer prompts would work well in a middleware callback so "outside" the handler. But not sure makes any practical difference! I'll do a little digging... |
Putting the code in middleware does work fine but I didn't find an advantage other than the separation, so probably better keeping the code simpler. |
I did notice some things with async await if we wanted to say access username later on in a different function. We would just need to await the whole yargs function, something like this (in order to console.log out the username): const argv = await yargs(hideBin(process.argv))
.command({
command: "login [username]",
describe: "Prints out the username",
builder: (yargs) => {
yargs.positional("username", {
describe: "The username to be printed",
type: "string",
});
},
handler: async (argv) => {
if (!argv.username) {
argv.username = await input({ message: "Please enter your username" });
}
console.log(`Welcome back, ${argv.username}!`);
},
})
.help()
.parse();
console.log(argv.username); Would it be helpful to mention this in this inquirer section? There might be more specific documentation on async await so it might not be needed. |
Good question about async. I don't think need to explicitly refactor code or explain about async since main point of example is inquirer, but I do think should add
|
Sounds good, thanks for all the help. Went ahead and pushed those changes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Thanks @nephh
Added a simple example of using Inquirer prompts to ensure the user inputs a required argument.