Reputation: 13496
So I'm using inquirer to ask a question from my users via terminal:
var inquirer = require('inquirer');
var question = {
name: 'name',
message: '',
validation: function(){ ... }
filter: function(){ ... }
};
but at the same time let my users pass this field via --name
command line argument. (say arg.name
)
Question is: How can I tell inquirer to take this arg.name
value and use it as if user entered it for the answer of this question.
Note that I DO need the validation and filter to also kick-in for this value. (for example if value passed via --name
is invalid, the user is presented with the error message and asked to enter another value)
I'm hoping there is a way to solve this problem without having to hack-around manually calling my validation/filter methods.
Upvotes: 4
Views: 1519
Reputation: 497
You could set up a default value to a variable from arg.name:
let argument = args.name;
const question = {
name: 'name',
message: 'Name?',
default: argument
};
I tried testing this and it works. The user just has to hit enter to use the name passed in the arguments so maybe tell them that in the prompt. Also be aware that your validation will take place AFTER your filters. You just need to make sure whatever your filters are doing will pass validation.
Upvotes: 2