dthree / vorpal

Node's framework for interactive CLIs
http://vorpal.js.org
MIT License
5.64k stars 280 forks source link

How to validate command arguments? #40

Closed scotthovestadt closed 8 years ago

scotthovestadt commented 8 years ago

If I have a command that looks like:

command-name [strategy]

If strategy can only be "insert", "update", or "upsert", for example, how do I use Vorpal to validate it?

dthree commented 8 years ago

I would just do a manual validation. Something like this:

const valids = ['insert', 'update', 'upsert'];
if (valids.indexOf(args.strategy) === -1) {
  this.log('Please enter a valid strategy');
  cb();
  return
}

This is actually a good question that will probably come up a lot. Would you mind posting this on Stack Overflow? There's a vorpal.js tag you can use.

Shakyamuni177te commented 8 years ago

I have posted a question on this:

http://stackoverflow.com/questions/33460454/validating-a-command-in-vorpal-js

dthree commented 8 years ago

Thanks.

scotthovestadt commented 8 years ago

The problem with this approach is it won't validate the arguments until the command is executed, which is a problem when you are piping commands together. Additionally, if you log an error it'll just go to the piped command.

I think we need to add a "validate" method to each command that can be called to validate arguments when the command is initialized but before it's executed?

dthree commented 8 years ago

Okay I'm totally down with this.

scotthovestadt commented 8 years ago

Re-open issue please!

dthree commented 8 years ago

:smiley:

scotthovestadt commented 8 years ago

@dthree Can you review https://github.com/dthree/vorpal/pull/57

dthree commented 8 years ago

57 fixed this, I belive.