boostr
Version:
Build and deploy your Layr apps
39 lines • 1.72 kB
JavaScript
import { throwError } from './utilities.js';
export function getCommandOptions(parsedOptions, availableCommandOptions) {
const commandOptions = {};
for (let [parsedName, value] of Object.entries(parsedOptions)) {
let actualName;
const formattedName = formatCommandOptionName(parsedName);
for (const [name, { type, aliases = [] }] of Object.entries(availableCommandOptions)) {
if (parsedName === name || aliases.includes(parsedName)) {
if (type === 'boolean' && typeof value !== 'boolean') {
throwError(`A value should not be specified for the ${formattedName} flag`);
}
if (type === 'string' && typeof value !== 'string') {
throwError(`A string value should be specified for the ${formattedName} option`);
}
if (type === 'string[]') {
if (!Array.isArray(value)) {
value = [value];
}
for (const item of value) {
if (typeof item !== 'string') {
throwError(`A string value should be specified for the ${formattedName} option`);
}
}
}
actualName = name;
break;
}
}
if (actualName === undefined) {
throwError(`A specified option is unknown: ${formattedName}`);
}
commandOptions[actualName] = value;
}
return commandOptions;
}
export function formatCommandOptionName(name) {
return name.length === 1 ? `-${name}` : `--${name}`;
}
//# sourceMappingURL=command.js.map