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

expose rawParse to use minimist/commander #5

Closed
Downchuck opened this issue Sep 1, 2015 · 9 comments
Closed

expose rawParse to use minimist/commander #5

Downchuck opened this issue Sep 1, 2015 · 9 comments

Comments

@Downchuck
Copy link

Vorpal could expose rawParse(process.argv, { command: true|false}) to run a string through minimist or commander. This may be a special case.

I have an existing app, it's just using minimist for cli arguments in the usual form (app.js --arg items), and I'm working on the interactive CLI via vorpal. Trying to reduce redundancy when I can.

I am still working out how best to switch between command line (single commands) and interactive command line. But that's a different issue.

@dthree
Copy link
Owner

dthree commented Sep 1, 2015

You mean in addition to vorpal.parse? I suppose I could literally map it to minimist if that helps.

@Downchuck
Copy link
Author

That's the idea, you'd map to minimist by default, with an optional second argument that'd run commander.js parse -- which would use your vorpal command list to return an object. Both are meant to be maps over to minimist/commander just in case it's needed.

Related to using this: on vorpal.parse -- how do I detect whether or not a command was found? Is there a default callback, like parse(argv, notFoundFn)?

@dthree
Copy link
Owner

dthree commented Sep 1, 2015

It is meant to do exactly what you app does if you run it first. So it would technically display the help command.

Check out the .catch command (it's new, maybe I didn't document it yet): here's an example I pulled out of wat, which im working on:

  vorpal
    .catch('[commands...]')
    .option('-d, --detail', 'View detailed markdown on item.')
    .option('-i, --install', 'View installation instructions.')
    .autocompletion(function (text, iteration, cb) {
      const self = this;
      const index = parent.clerk.indexer.index();
      let result = util.autocomplete(text, iteration, index, function (word, options) {
        const res = self.match(word, options);
        return res;
      });
      if (_.isArray(result)) {
        result.sort();
      }
      cb(undefined, result);
    })
    .action(function (args, cb) {
      const self = this;

      args = args || {};
      args.options = args.options || {};

      // Handle humans.
      if (String(args.commands[0]).toLowerCase() === 'wat') {
        args.commands.shift();
      }

       // ...

      cb();
    });

Catch basically is called if you don't match on any other command, so it overrides help as the default function if no command is met. By adding in a [variadicArg...], you can pretty much do anything you want.

@dthree
Copy link
Owner

dthree commented Sep 1, 2015

On the parse, I think im gonna do this:

var args = vorpal.parse(process.argv, { use: 'minimist' });
var args = vorpal.parse(process.argv, { use: 'commander' });

@Downchuck
Copy link
Author

I like it.

@dthree
Copy link
Owner

dthree commented Sep 1, 2015

:)

Does the catch thing make sense?

@Downchuck
Copy link
Author

Yes, it makes sense, does just what I want.

@dthree
Copy link
Owner

dthree commented Sep 1, 2015

Perfect.

@dthree
Copy link
Owner

dthree commented Sep 22, 2015

@Downchuck Added this. Coming out in a release today.

@dthree dthree closed this as completed Sep 22, 2015
postatum pushed a commit to postatum/vorpal that referenced this issue Sep 25, 2018
…d-stupidity

fix(package): Fixed repository.url to point at mulesoft fork
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants