-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Allow arguments to be specified for top-level command, not just sub-commands #257
Description
Currently, sub-commands can specify arguments to be parsed in their description string. With a little trickery, this facility can be used for the top-level command too:
program.parseExpectedArgs(['<foo>', '[bar]']);
program.parent = program;
var name = program._name;
program._name = '*';
program.action(function (foo, bar) { ... });
program._name = name;
The second line fixes the hooking up of the listener to the parent at the end of action, the temporary setting of program._name ensures the command will always be called, and the function passed to action contains the main code of the command.
The simplest API change I can think of to achieve this nicely would be to factor out most of command into a new method arguments, so you could write program.arguments('<foo> [bar]') and have it work as for a sub-command; then change action so that if parent is undefined when it is called, it will use '*' as the command name rather than this._name, and set the event on this rather than this.parent.
I have implemented this as pull request #258.