dthree / vorpal

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

Better argument parsing #49

Closed scotthovestadt closed 8 years ago

scotthovestadt commented 8 years ago

There are a few types of arguments that can't be parsed in the REPL. For example, it's impossible to pass a JSON object.

I would like the argument parser to accommodate the following arguments:

command-name not-quoted
command-name '{"hello": "world"}`
command-name `{"hello": "little quotes 'hi' in here"}`

I'm working on code to do this. I don't think it's actually that complicated. Do you agree with the spec?

scotthovestadt commented 8 years ago

Please merge in https://github.com/dthree/vorpal/pull/51

Thank you!

cayasso commented 8 years ago

Any update on this PR? I am a little stuck waiting for this fix :+1: thanks for this awesome project.

dthree commented 8 years ago

Just merged and published. And you're welcome!

cayasso commented 8 years ago

AWESOME, thanks!

rimiti commented 7 years ago

Hi,

I also have this problem. When i'm passing JSON as argument value it's interpreted like an object... The result is strange.. How can I do to pass JSON ?

code

vorpal
  .command('--test-sync <payload>')
  .description('Synchronize group.')
  .option('--payload <required>', 'Pass JSON payload.')
  .validate((args) => {
    console.log(args.payload)
    try {
      JSON.parse(args.payload)
    } catch (e) {
      return 'You only can pass JSON.'
    }
    return true
  })
  .action((args, callback) => {
    console.log('test');
    callback();
  });

Command:

$ node index.js --test-sync --payload '{"hello": "world"}'

Output:

hello

Regards,

waldekmastykarz commented 6 years ago

@rimiti try:

$ node index.js --test-sync --payload '`{"hello": "world"}`'

It seems that the outer single quotes are being removed before passing for processing by Vorpal which is why Vorpal is thinking hello and world are separate values. By wrapping the payload in an extra pair of backticks, you let Vorpal know that the whole string is a single argument which is then correctly processed.