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

55 lines
2.0 KiB
JavaScript
Executable File

import * as Result from './Result';
import { string } from './types';
function fullPositional(config) {
var _a, _b, _c;
const displayName = (_b = (_a = config.displayName) !== null && _a !== void 0 ? _a : config.type.displayName) !== null && _b !== void 0 ? _b : 'arg';
return {
description: (_c = config.description) !== null && _c !== void 0 ? _c : config.type.description,
helpTopics() {
var _a, _b;
return [
{
category: 'arguments',
usage: `<${displayName}>`,
description: (_b = (_a = config.description) !== null && _a !== void 0 ? _a : config.type.description) !== null && _b !== void 0 ? _b : 'self explanatory',
defaults: [],
},
];
},
register(_opts) { },
async parse({ nodes, visitedNodes, }) {
const positionals = nodes.filter((node) => node.type === 'positionalArgument' && !visitedNodes.has(node));
const positional = positionals[0];
if (!positional) {
return Result.err({
errors: [
{
nodes: [],
message: `No value provided for ${displayName}`,
},
],
});
}
visitedNodes.add(positional);
const decoded = await Result.safeAsync(config.type.from(positional.raw));
if (Result.isErr(decoded)) {
return Result.err({
errors: [
{
nodes: [positional],
message: decoded.error.message,
},
],
});
}
return Result.ok(decoded.value);
},
};
}
export function positional(config) {
return fullPositional({
type: string,
...config,
});
}
//# sourceMappingURL=positional.js.map