dthree / vorpal

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

Variadic and optional argument case #216

Closed vanrez-nez closed 7 years ago

vanrez-nez commented 7 years ago

Hi everyone!

Not sure if the following behavior is by design but:

vorpal
    .command('cmd <a...> [b]', 'Test case"')
    .action(function(args, callback) {
        console.log( args );
        callback();
    });

with the input cmd 1 2 will parse the args inside action without the 1:

{ options: {}, a: [ 2 ], b: 2 }

Is this a bug? or am I misusing the variadic and optional arguments?. Thank you!

albacoretuna commented 7 years ago

I guess variadic arguments can be used only as the last argument

milesj commented 7 years ago

@radixzz

Omidfi is correct, variadics must be last: https://github.com/dthree/vorpal/blob/master/lib/vorpal.js#L311 and https://github.com/dthree/vorpal/wiki/API-%7C-vorpal.command#multiple-arguments

They work in a similar vein to rest arguments: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters