archieve-projects/微信机器人/node_modules/cmd-ts/dist/esm/multioption.js

70 lines
2.6 KiB
JavaScript
Executable File

import { findOption } from './newparser/findOption';
import * as Result from './Result';
/**
* Like `option`, but can accept multiple options, and expects a decoder from a list of strings.
* An error will highlight all option occurences.
*/
export function multioption(config) {
return {
helpTopics() {
var _a, _b;
const displayName = (_a = config.type.displayName) !== null && _a !== void 0 ? _a : 'value';
let usage = `--${config.long} <${displayName}>`;
if (config.short) {
usage += `, -${config.short}=<${displayName}>`;
}
return [
{
category: 'options',
usage,
defaults: [],
description: (_b = config.description) !== null && _b !== void 0 ? _b : 'self explanatory',
},
];
},
register(opts) {
opts.forceOptionLongNames.add(config.long);
if (config.short) {
opts.forceOptionShortNames.add(config.short);
}
},
async parse({ nodes, visitedNodes, }) {
var _a;
const options = findOption(nodes, {
longNames: [config.long],
shortNames: config.short ? [config.short] : [],
}).filter((x) => !visitedNodes.has(x));
for (const option of options) {
visitedNodes.add(option);
}
const optionValues = [];
const errors = [];
const flagNodes = [];
for (const option of options) {
const providedValue = (_a = option.value) === null || _a === void 0 ? void 0 : _a.node.raw;
if (providedValue === undefined) {
flagNodes.push(option);
continue;
}
optionValues.push(providedValue);
}
if (flagNodes.length > 0) {
errors.push({
nodes: flagNodes,
message: `Expected to get a value, found a flag`,
});
}
if (errors.length > 0) {
return Result.err({ errors });
}
const multiDecoded = await Result.safeAsync(config.type.from(optionValues));
if (Result.isErr(multiDecoded)) {
return Result.err({
errors: [{ nodes: options, message: multiDecoded.error.message }],
});
}
return multiDecoded;
},
};
}
//# sourceMappingURL=multioption.js.map