dthree / vorpal

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

how to achive variadic options? #221

Closed albacoretuna closed 7 years ago

albacoretuna commented 7 years ago

Hi thanks for the nice project :)

I'm making an http cli tool and I need to be able to parse commands like this:

post localhost:3000/user/all Authorization:'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6xjM0MjMsInVzZXJJZCI6Im9taWQiLCJzY29wZSI6ImFkbWluIiwiaWF0IjoxNDkwNzgwODQ3fQ.R5dho9wyV80L8e7c_98Ld2VaMbAvKGOwB9yirtS6uQk' AnotherHeader:'blahblahbla' someData='blahBlah'

How can I approach this with vorpal? I tried using variadic arguments, but it didn't help as the headers are also variadic. I also tried to use a variadic option, so that after --header I could add multiple items, but that didn't work either.

postatum commented 7 years ago

Hi @omidfi. Here's an example. Check out .command.allowUnknownOptions() docs for more details.

// index.js
var vorpal = require('vorpal')()

vorpal
  .command('post <url>')
  .description('post stuff to url')
  .allowUnknownOptions()
  .action(function(args){
    console.log(args)
  });

vorpal.exec("post localhost:3000/user/all --Authorization 'Bearer long token' --AnotherHeader 'blahblahbla' --someData 'blahBlah'")
$ node index.js 
{ options: 
   { Authorization: 'Bearer long token',
     AnotherHeader: 'blahblahbla',
     someData: 'blahBlah' },
  url: 'localhost:3000/user/all' }