args-any
Version:
Utility lib for parsing command options
35 lines • 1.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.args = exports.parseArgs = void 0;
const lodash_1 = require("lodash");
const _1 = require(".");
const parseArgs = (args, settings) => {
const trimChars = '"';
const normalized = args.reduce((acc, curr) => {
acc.push(...(0, _1.expandSeparator)((0, lodash_1.trim)(curr, trimChars)));
return acc;
}, []);
let keys = normalized.filter(_1.option.isKey);
const keyPrefix = (settings && settings.keyPrefix) || false;
const useValueAsKey = (settings && settings.valueAsKey) || false;
if (keyPrefix) {
keys = keys.filter((x) => _1.option.hasPrefix(x, keyPrefix));
}
const map = new Map();
let current = [...normalized];
for (const key of keys) {
const itemValue = _1.option.parse(key, current, settings);
const itemKey = useValueAsKey ? itemValue.value : _1.prefixlessKey.parse(key, settings);
current = current.slice(current.indexOf(key) + 1);
const exclude = settings && settings.filter && !settings.filter(itemValue);
if (!exclude) {
map.set(itemKey, itemValue);
}
}
return map;
};
exports.parseArgs = parseArgs;
exports.args = {
parse: exports.parseArgs
};
//# sourceMappingURL=args.parser.js.map