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

example using inquirer to prompt user #2385

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

nephh
Copy link

@nephh nephh commented Jan 8, 2024

Added a simple example of using Inquirer prompts to ensure the user inputs a required argument.

docs/advanced.md Outdated Show resolved Hide resolved
@shadowspawn
Copy link
Member

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...

@shadowspawn
Copy link
Member

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.

@nephh
Copy link
Author

nephh commented Jan 9, 2024

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.

@shadowspawn
Copy link
Member

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 await in example code as good practice when have an async handler.

await yargs(hideBin(process.argv))
...

@nephh
Copy link
Author

nephh commented Jan 10, 2024

Sounds good, thanks for all the help. Went ahead and pushed those changes

docs/advanced.md Outdated Show resolved Hide resolved
docs/advanced.md Outdated Show resolved Hide resolved
Copy link
Member

@shadowspawn shadowspawn left a 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants