dthree / vorpal

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

access un-parsed args in command #210

Closed camacho closed 6 years ago

camacho commented 7 years ago

First, thank you so much for all the work on this! It has been a really awesome experience using this framework for my project.

I'm trying to figure out if there is a way to access the raw argv in a command action? For my case (which others might have), I am trying to take the args and pass them through to an exec:

// In ./lib/commands/test/index.js
const { fork } = require('child_process');
const path = require('path');

const jestBin = path.resolve(__dirname, '../../node_modules/.bin/jest');

function addTestCommand(program) {
  program
    .command('test', 'Run Jest tests')
    .allowUnknownOptions()
    .action((args, cb) => {
      program.hide();
      fork(jestBin, args, { stdio: 'inherit' })
        .on('close', (response) => {
          program.show();
          cb(response);
        });
    });
}

module.exports = addTestCommand;
// In ./index.js
const vorpal = require('vorpal')();
require('./lib/test')(vorpal);

vorpal
  .show()
  .parse(process.argv);

In this case, Jest is expecting the args to be passed in as a raw array (like process.argv) but the args given to the command action are already parsed.

Is there a way to access the unparsed arguments in the command action OR could we add an additional value to the args argument like _raw: [] that contains a copy of the unparsed args?

Happy to do the work in a PR if I could be pointed to the right place to do the work.

Thanks again for all the work on this project!

MatthieuLemoine commented 7 years ago

Can you provide an example of command you run to launch vorpal with arguments and what are the values you get in the args and the values you would expect ?

camacho commented 7 years ago

sure - for the use case, I am simply starting up the script by running Node

node ./

Then, once in the running application, I want to execute the test command with some arguments:

vorpal$ test --no-color --coverage

I want Jest to be called with those flags:

require('child_process').fork('<path-to-jest-bin>', ['--no-color', '--coverage'], { stdio: 'inherit' });

Right now, I only have access to the parsed args in the action:

program
    .command('test', 'Run Jest tests')
    .allowUnknownOptions()
    .action((args, cb) => {
      console.log(args);
      // { options: { color: false, coverage: true } }
    });

It would be great to also have access to the unparsed args (like we do with process.argv):

program
    .command('test', 'Run Jest tests')
    .allowUnknownOptions()
    .action((args, cb) => {
      console.log(args);
      // { options: { color: false, coverage: true }, _raw: ['--no-color', '--coverage'] }
    });
postatum commented 7 years ago

Hi @camacho.

You can get access to raw command and args string inside vorpal command's parse() method. Though you would have to think how to pass that data to your action().

camacho commented 7 years ago

@postatum is it possible to access the history from a command action?

postatum commented 7 years ago

@camacho not sure, I didn't try. There's this feature, maybe it will be helpful - vorpal.history