clef-parse
Version:
Simple, lightweight argv parser. Powers the cleffa and clefairy packages
30 lines (29 loc) • 1.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
const convert_case_1 = require("./convert-case");
const envVarNames = Object.keys(process.env);
const clefParseEnvVarNames = envVarNames.filter((name) => name.startsWith("CLEF_PARSE_"));
const clefParseEnvVars = Object.fromEntries(clefParseEnvVarNames.map((name) => [name, process.env[name]]));
const offset = Number(clefParseEnvVars.CLEF_PARSE_ARGV_OFFSET || "2");
const possibleHintValues = {
Boolean,
String,
Number,
Path: index_1.Path,
};
const hints = {};
for (const [name, value] of Object.entries(clefParseEnvVars)) {
if (!name.startsWith("CLEF_PARSE_HINT_")) {
continue;
}
const hintKey = (0, convert_case_1.convertToCamelCase)(name.replace(/^CLEF_PARSE_HINT_/, ""));
const hintValue = possibleHintValues[value || ""];
if (hintValue == null) {
throw new Error(`Invalid hint specified by env var '${name}'. Valid values are "Boolean", "String", "Number", or "Path", but received: ${value}.`);
}
hints[hintKey] = hintValue;
}
const result = (0, index_1.parseArgv)(process.argv.slice(offset), hints);
console.log(JSON.stringify(result, null, 2));