[2.0 Proposition] Using a this less API
- Dominant language
- JavaScript
- Stars
- 5.6k
- Forks
- 278
- PR merge metrics
- No merged PRs in 30d
Description
## Proposition
We should take the opportunity of the [incoming v2](https://github.com/dthree/vorpal/pull/272) to **adopt a `this`less api**.
This will make action handler far more easy to test without having to mock a vorpal instance.
The function would be injected with a single arguments, an object holding the `args`, the `options`, the logger and the callback.
## Example
### Original Code
```js
vorpal
.command('foo [optionalArg]')
.option('-v, --verbose', 'Print foobar instead.')
.action(function(args, callback) {
if (args.options.verbose) {
this.log('foobar ' + args.requiredArg);
} else {
this.log('bar ' + (args.optionalArg || 'default')));
}
callback();
});
```
### V2 proposed code
```js
vorpal
.command('foo [optionalArg]')
.option('-v, --verbose', 'Print foobar instead.')
.action(function(cmd) {
if (cmd.options.verbose) {
cmd.log('foobar ' + cmd.args.requiredArg);
} else {
cmd.log('bar ' + (cmd.args.optionalArg || 'default')));
}
cmd.callback();
});
```
#### alternative usage with destructuring:
```js
vorpal
.command('foo [optionalArg]')
.option('-v, --verbose', 'Print foobar instead.')
.action(function({options, args, log, callback}) {
if (options.verbose) {
log('foobar ' + args.requiredArg);
} else {
log('bar ' + (args.optionalArg || 'default')));
}
callback();
});
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.