Add command line arg parameters to Run

I was trying out a basic minimist example package using the following code from their examples in npm.

var argv = require('minimist')(process.argv.slice(2));
console.dir(argv);

Instead of a Run which will output an empty arg list, How do we test out something similar to this?

$ node example/parse.js -a beep -b boop

Returns:

{ _: , a: ‘beep’, b: ‘boop’ }

So, there are two options to get something working today (vs waiting for us to implement args/design UI around it/etc).

The first is to simply pre-fill the args yourself:

process.argv = process.argv.concat(["a","beep","b","boop"]);
//...
var argv = require("minimist")(process.argv.slice(2))

At the end of the day, this is essentially the same thing that would be happening under the hood if we were to implement a bunch of UI around command line args in Tonic.

The second is to spawn your own node process from Tonic. You could write some simple code to a file:

fs.writeFileSync("somefile.js", "require('minimist')(process.argv.slice(2))...");

Then spawn it off:

var spawned = require('child_process').spawn("node", ["somefile.js", "-a", "beep", "-b", "boop"]);
spawned.stdout.on('data', (data) => {
  console.log(`got: ${data}`);
});

Sorry for the inconvenience but hopefully this at least allows you to make some immediate progress!

2 Likes

very interesting, and glad there is a solution.

PS: I just published an npm module for command line called: commandlinerun
it might be useful to start different function with command line parameters, maybe it could be useful as well, maybe in combination with minimist