From 68213df9450fbe398a3b087be6a2b4f82557334b Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Wed, 18 Oct 2023 22:55:21 -0400 Subject: [PATCH] Create option-parsing helper stub --- src/lib/options.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/lib/options.ts diff --git a/src/lib/options.ts b/src/lib/options.ts new file mode 100644 index 0000000..3568f9d --- /dev/null +++ b/src/lib/options.ts @@ -0,0 +1,33 @@ +import { AsText, TextPiece } from '../words'; + +type SwitchPattern = Record & { + min?: number; + max?: number; +}; +type SwitchOutput

= { + [K in keyof P]: K extends "min" | "max" + ? never + : P[K] extends 0 + ? boolean + : string[]; +} & { + error?: string; +}; + +// TODO: tests and error handling +export function getOpt

( + argv: TextPiece[], + switches: P +): [SwitchOutput

, ...string[]] { + const [flags, textPieces] = getOptRaw(argv, switches); + return [flags, ...textPieces.map(AsText)]; +} + +export function getOptRaw

( + argv: TextPiece[], + switches: P +): [SwitchOutput

, TextPiece[]] { + const [, ...words] = argv; + // TODO: handle min/max + return [{} as SwitchOutput

, words]; +}