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

Input prompt with command substitution #681

Open
kwkelly opened this issue May 25, 2018 · 5 comments
Open

Input prompt with command substitution #681

kwkelly opened this issue May 25, 2018 · 5 comments

Comments

@kwkelly
Copy link

kwkelly commented May 25, 2018

I'm trying to write a bash script using inquirer for input, but when using command substitution, inquirer does not show the prompt.

Test script test.js

var inquirer = require('inquirer');

inquirer.prompt([
 {
    type: 'input',
    name: 'comments',
    message: 'Any comments on your purchase experience?',
    default: 'Nope, all good!'
}
]).then(answers => {
    console.log(answers)
});

To see the problem, try assigning the output of the prompt to a variable.

In a bash shell (maybe others):
ans=$(node test.js)

And nothing shows up, event though simply
node test.js
works just fine.

However, the read bash builtin seems to work fine with command substitution. The following will show a prompt:
$(read -p "Enter input: " input)

@kwkelly
Copy link
Author

kwkelly commented May 30, 2018

Interestingly, the bash builtin read uses stderr to display the prompt text. Redirecting stderr to stdout causes it to be captured in the variable.

@kwkelly
Copy link
Author

kwkelly commented May 30, 2018

Looks like this is probably solved by #519

But is it possible to write to /dev/tty instead of stderr?

@SBoudrias
Copy link
Owner

I don't think you'll be able to write to /dev/tty directly. Node only supports stdout and stderr as far as I know.

@kwkelly
Copy link
Author

kwkelly commented May 31, 2018

/dev/tty is just a file, so it can be written to as you would a file:

var fs  = require('fs');
var tty = fs.createWriteStream('/dev/tty');

tty.write('testing\n');

However, this might not be a great idea since I have no idea what the equivalent would be on Windows.

@SBoudrias
Copy link
Owner

@kwkelly actually, you can pass any stream as output. So it should work fine with the result of fs.createWriteStream.

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

No branches or pull requests

2 participants