Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0 Proposition] Using a this less API #326

Open
AdrieanKhisbe opened this issue Oct 7, 2018 · 0 comments
Open

[2.0 Proposition] Using a this less API #326

AdrieanKhisbe opened this issue Oct 7, 2018 · 0 comments

Comments

@AdrieanKhisbe
Copy link
Contributor

Proposition

We should take the opportunity of the incoming v2 to adopt a thisless 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

vorpal
  .command('foo <requiredArg> [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

vorpal
  .command('foo <requiredArg> [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:

vorpal
  .command('foo <requiredArg> [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();
  });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant