Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Schniz/cmd-ts. 💻 A type-driven command line argument parser cmd-ts.now.sh
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import { PrintHelp, Versioned } from './helpdoc';import { ParseContext, ParsingResult, Register } from './argparser';import { tokenize } from './newparser/tokenizer';import { parse } from './newparser/parser';import { errorBox } from './errorBox';import { isErr } from './Result';
export type Handling<Values, Result> = { handler: (values: Values) => Result };
export type Runner<HandlerArgs, HandlerResult> = PrintHelp & Partial<Versioned> & Register & Handling<HandlerArgs, HandlerResult> & { run(context: ParseContext): Promise<ParsingResult<HandlerResult>>; };
export type Into<R extends Runner<any, any>> = R extends Runner<any, infer X> ? X : never;
export async function run<R extends Runner<any, any>>( ap: R, strings: string[]): Promise<Into<R>> { const longOptionKeys = new Set<string>(); const shortOptionKeys = new Set<string>(); const hotPath: string[] = []; ap.register({ forceFlagShortNames: shortOptionKeys, forceFlagLongNames: longOptionKeys, });
const tokens = tokenize(strings); const nodes = parse(tokens, { longFlagKeys: longOptionKeys, shortFlagKeys: shortOptionKeys, }); const result = await ap.run({ nodes, visitedNodes: new Set(), hotPath });
if (isErr(result)) { console.error(errorBox(nodes, result.error.errors, hotPath)); process.exit(1); } else { return result.value; }}