import { findOption } from './newparser/findOption'; import chalk from 'chalk'; import * as Result from './Result'; import { string } from './types'; function fullOption(config) { var _a; return { description: (_a = config.description) !== null && _a !== void 0 ? _a : config.type.description, helpTopics() { var _a, _b, _c, _d, _e; const displayName = (_a = config.type.displayName) !== null && _a !== void 0 ? _a : 'value'; let usage = `--${config.long}`; if (config.short) { usage += `, -${config.short}`; } usage += ` <${displayName}>`; const defaults = []; if (config.env) { const env = process.env[config.env] === undefined ? '' : `=${chalk.italic(process.env[config.env])}`; defaults.push(`env: ${config.env}${env}`); } const defaultValueFn = (_b = config.defaultValue) !== null && _b !== void 0 ? _b : config.type.defaultValue; if (defaultValueFn) { try { const defaultValue = defaultValueFn(); if ((_c = config.defaultValueIsSerializable) !== null && _c !== void 0 ? _c : config.type.defaultValueIsSerializable) { defaults.push('default: ' + chalk.italic(defaultValue)); } else { defaults.push('optional'); } } catch (e) { } } return [ { category: 'options', usage, defaults, description: (_e = (_d = config.description) !== null && _d !== void 0 ? _d : config.type.description) !== null && _e !== void 0 ? _e : 'self explanatory', }, ]; }, register(opts) { opts.forceOptionLongNames.add(config.long); if (config.short) { opts.forceOptionShortNames.add(config.short); } }, async parse({ nodes, visitedNodes, }) { var _a, _b; const options = findOption(nodes, { longNames: [config.long], shortNames: config.short ? [config.short] : [], }).filter((x) => !visitedNodes.has(x)); options.forEach((opt) => visitedNodes.add(opt)); if (options.length > 1) { const error = { message: 'Too many times provided. Expected 1, got: ' + options.length, nodes: options, }; return Result.err({ errors: [error] }); } const valueFromEnv = config.env ? process.env[config.env] : undefined; const option = options[0]; let rawValue; let envPrefix = ''; const defaultValueFn = (_a = config.defaultValue) !== null && _a !== void 0 ? _a : config.type.defaultValue; if (option === null || option === void 0 ? void 0 : option.value) { rawValue = option.value.node.raw; } else if (valueFromEnv !== undefined) { rawValue = valueFromEnv; envPrefix = `env[${chalk.italic(config.env)}]: `; } else if (!option && typeof defaultValueFn === 'function') { try { return Result.ok(defaultValueFn()); } catch (e) { const message = `Default value not found for '--${config.long}': ${e.message}`; return Result.err({ errors: [ { nodes: [], message, }, ], }); } } else { const raw = (option === null || option === void 0 ? void 0 : option.type) === 'shortOption' ? `-${option === null || option === void 0 ? void 0 : option.key}` : `--${(_b = option === null || option === void 0 ? void 0 : option.key) !== null && _b !== void 0 ? _b : config.long}`; return Result.err({ errors: [ { nodes: options, message: `No value provided for ${raw}`, }, ], }); } const decoded = await Result.safeAsync(config.type.from(rawValue)); if (Result.isErr(decoded)) { return Result.err({ errors: [ { nodes: options, message: envPrefix + decoded.error.message }, ], }); } return Result.ok(decoded.value); }, }; } export function option(config) { return fullOption({ type: string, ...config, }); } //# sourceMappingURL=option.js.map