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

Feature request for multiple prompt #49

Closed
wants to merge 1 commit into from
Closed

Feature request for multiple prompt #49

wants to merge 1 commit into from

Conversation

kuwabarahiroshi
Copy link

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.

.fill() method makes it easy to write multiple prompt at once.
'nickname': 'What is your nickname? '
, 'age': 'How old are your? '
, 'favorite-song': 'What is your favorite song? ' // option name can be camelCase as well
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants