Feature request for multiple prompt #49

Closed
wants to merge 1 commit into
from

Projects

None yet

2 participants

I wrote .fill() API, which aims to make it easy to write multiple prompt at once.
The usage is illustrated as below:

program                                                                                                                                                                                                     
     .option('-n, --nickname <name>')
     .option('-a, --age <age>')
     .option('-s, --sex <sex>')
     .parse(process.argv);

 // .fill() should be called after .parse()
 program.fill({
     'nickname': 'What is your nickname? '
     , 'age': 'How old are you? '
 });

 // if '--nickname' or '--age' aren't supplied with command line option,
 // those values will be filled with prompt.

 // if those arguments are provided on command line option, it never prompts.
 // '--sex' is still just an option because it isn't specified to be filled.

 // Filled arguments are available on 'filled' event.
 program.on('filled', function(program) {
     console.log('name: %s', program.nickname);
     console.log('age: %s', program.age);
     console.log('sex: %s', program.sex);
 });

With using this API, we can avoid multiple nested prompt like below:

program.prompt('please input foo: ', function(foo) {
    program.prompt('please input bar: ', function(bar) {
        program.prompt('please input baz :', function(baz) {
            console.log('foo: %s, bar: %s, baz: %s', foo, bar, baz);
        });
    });
});

I'd be glad if you also think this is useful.

@kuwabarahiroshi kuwabarahiroshi Added .fill() feature
.fill() method makes it easy to write multiple prompt at once.
65862f8
@tj tj commented on the diff Feb 16, 2012
examples/fill
+}
+
+program
+ .version('0.0.1')
+ .option('-n --nickname <name>', 'Your nickname.')
+ .option('-a --age <age>', 'Your age.')
+ .option('-s --sex <sex>', 'Your sex. [ male | female ]', normalizeSex, 'unknown')
+ .option('-f --favorite-song <song>', 'Your favorite <song>.')
+ .parse(process.argv);
+
+program
+ .fill({
+ 'nickname': 'What is your nickname? '
+ , 'age': 'How old are your? '
+ , 'favorite-song': 'What is your favorite song? ' // option name can be camelCase as well
+ })
tj
tj Feb 16, 2012 Owner

I like this but I wouldn't merge them into program, I would have it take a callback with the object it populates, camel-case only

kuwabarahiroshi
kuwabarahiroshi Feb 17, 2012

I see, that is similar to .prompt() behavior.
So we'll have choice, that is, it's up to us to merge or not to merge filled options into program in a callback.

As with camelCase, or hyphenated-words, I aimed to be in "generous on input, strict on output" way. But if you don't like it, I agree with you.

tj
tj Feb 17, 2012 Owner

yup, we could even just augment .prompt() to support it:

program.prompt({
  name: 'stuff...',
  age: 'stuff...'
}, function(obj){
  console.log(obj)
});
kuwabarahiroshi
kuwabarahiroshi Feb 17, 2012

Yah, I thought that multiple prompt can be implemented with writing an overload version of .prompt().

However, to tell my opinion, I expect .prompt() to prompt always. I would like to .fill() options if it's not supplied with command line option.

I mean:

program.prompt({
    foo: 'please input foo: ',
    bar: 'please input bar: ',
    baz: 'please input baz: '
}, function(obj) {
    // ...
});

// I expect above code to always prompt `foo`, `bar` and `baz`.
// On the other hand,

program.fill({
    foo: 'please input foo: ',
    bar: 'please input bar: ',
    baz: 'please input baz: '
}, function(obj) {
    // ...
});

// I would like to fill `obj.foo` if `--foo` option isn't given on command line option, so as well with `bar` and `baz`.
// In other words, if `--foo` option is supplied on command line, I don't want to prompt that 'please input foo: '.

Does it make sense?
I afraid that it might be enough to have multiple version of .prompt()..., but I often want .fill() feature.

@tj tj closed this in f2060ff Apr 11, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment