archieve-projects/微信机器人/node_modules/cmd-ts/dist/deno_cjs/newparser/findOption.ts

37 lines
797 B
TypeScript

import { AstNode, LongOption, ShortOption } from './parser.ts';
type Option = LongOption | ShortOption;
/**
* A utility function to find an option in the AST
*
* @param nodes AST node list
* @param opts Long and short names to look up
*/
export function findOption(
nodes: AstNode[],
opts: {
longNames: string[];
shortNames: string[];
}
): Option[] {
const result: Option[] = [];
for (const node of nodes) {
if (node.type === 'longOption' && opts.longNames.includes(node.key)) {
result.push(node);
continue;
}
if (node.type === 'shortOptions' && opts.shortNames.length) {
for (const option of node.options) {
if (opts.shortNames.includes(option.key)) {
result.push(option);
}
}
}
}
return result;
}