Skip to content

Instantly share code, notes, and snippets.

@dominykas
Last active June 25, 2020 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominykas/396b3279eceb3ad5ffeddacf30fd35e7 to your computer and use it in GitHub Desktop.
Save dominykas/396b3279eceb3ad5ffeddacf30fd35e7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const args = process.argv.slice(2);
const [modfn, ...params] = args;
const [mod, fn] = modfn.split('.');
const Mod = require(mod);
const options = {};
for (let i = 0; i < params.length; ++i) {
let param = params[i];
let key, value;
if (param.startsWith('--')) {
param = param.slice(2);
key = param;
value = true;
}
if (param.includes(':')) {
const [k, v] = param.split(':');
key = k;
value = v;
}
if (param.includes('=')) {
const [k, v] = param.split('=');
key = k;
value = v;
}
if (key) {
if (value === 'true') {
value = true;
}
options[key] = value;
delete params[i];
}
}
console.log({ mod, fn, params, options });
Mod[fn](...params.filter(p => p), options, (err, result) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment