commander-remaining-args
Version:
Get the remaining unknown arguments after parsing the CLI with commander.js
39 lines (35 loc) • 1.17 kB
JavaScript
module.exports = cli =>
cli.rawArgs
// Only retain elements starting from the first --option
.reduce(
(acc, item) => acc.length || item.startsWith('-') ? [...acc, item] : acc,
[]
)
// Filter out arguments already parsed by commander.js
.filter((rawArg, index, rawArgs) => {
// --option=B, --oB
const matches = rawArg.match(/^(--.+)=(.+)$/) || rawArg.match(/^(-[^-])(.+)$/);
if (matches) {
const [, option] = matches;
if (cli.optionFor(option)) {
return false;
} else {
return true;
}
}
// If the option is consumed by commander.js, then we skip it
if (cli.optionFor(rawArg)) {
return false;
}
// If it's an argument of an option consumed by commander.js, then we
// skip it too
const previousRawArg = rawArgs[index - 1];
const previousOption = cli.optionFor(previousRawArg);
if (previousOption) { // Option consumed by commander.js
const previousKey = previousOption.attributeName();
if (cli[previousKey] === rawArg) {
return false;
}
}
return true;
});