diff --git a/makeFlags.ts b/makeFlags.ts index 7da1418..6649963 100644 --- a/makeFlags.ts +++ b/makeFlags.ts @@ -1,93 +1,138 @@ -import { - dateStyles, - timeStyles, - DateStyle, - TimeStyle, - toDateStyleString, - toTimeStyleString, - dateParse, - hourCycles, -toHourCycleString, - } from "./lib/common.ts"; +import { + dateParse, + DateStyle, + dateStyles, + hourCycles, + TimeStyle, + timeStyles, + toDateStyleString, + toHourCycleString, + toTimeStyleString, +} from "./lib/common.ts"; +import { + describe, + flag, + flags, + Handler, + isBooleanAt, + isStringAt, + Rule, + Test, +} from "npm:@jondotsoy/flags@2.0.2"; -export function makeFlags(args: string[]) { - let hourCycle: (typeof hourCycles)[number] = 'none'; - let dateStyle: DateStyle = 'full'; - let timeStyle: TimeStyle = 'full'; - let insertFinalNewLine = true; - let local: string | undefined; - let timeZone: string | undefined; - let date = new Date(); - let outputAsEpoch = false; - let outputAsEpochMS = false; - let outputAsJSON = false; - let outputAsUTC = false; - let outputAsSheet = false; - let stdinReadable = false; - let showHelp = false; - let template: string | undefined; - let crontab: string | undefined; +const labelSymbol = Symbol("label"); - const transformOptions: Record string | undefined) => void> = { - '-': () => { stdinReadable = true; }, - '--zero': () => { insertFinalNewLine = false; }, - '--date-style': (nextArgument) => { dateStyle = toDateStyleString(nextArgument()); }, - '--time-style': (nextArgument) => { timeStyle = toTimeStyleString(nextArgument()); }, - '--hour-cycles': (nextArgument) => { hourCycle = toHourCycleString(nextArgument()) }, - '--time-zone': (nextArgument) => { timeZone = nextArgument(); }, - '--local': (nextArgument) => { local = nextArgument(); }, - '--template': (nextArgument) => { template = nextArgument(); }, - '--json': () => { outputAsJSON = true; }, - '--sheet': () => { outputAsSheet = true; }, - '--utc': () => { outputAsUTC = true; }, - '--epoch': () => { outputAsEpoch = true; }, - '--epoch-ms': () => { outputAsEpochMS = true; }, - '--date': (nextArgument) => { - const v = nextArgument(); - if(v) { - date = dateParse(v); - } - }, - '--help': () => { showHelp = true; }, - get "-j"() { return this['--json']; }, - get "-d"() { return this["--date"]; }, - get "-l"() { return this["--local"]; }, - get "-tz"() { return this["--time-zone"]; }, - get "-z"() { return this["--zero"]; }, - get "-h"() { return this["--help"]; }, - }; +const setLabel = (obj: object, label: string) => + Reflect.set(obj, labelSymbol, label); +const getLabel = (obj: object) => Reflect.get(obj, labelSymbol); + +const describeLabel = (test: Test, label: string): Test => { + setLabel(test, label); + return test; +}; + +interface Options { + hourCycle: (typeof hourCycles)[number]; + dateStyle: DateStyle; + timeStyle: TimeStyle; + insertFinalNewLine: boolean; + local: string; + timeZone: string; + date: Date; + outputAsEpoch: boolean; + outputAsEpochMS: boolean; + outputAsJSON: boolean; + outputAsUTC: boolean; + outputAsSheet: boolean; + stdinReadable: boolean; + showHelp: boolean; + template: string; + crontab: string; +} - const optionsLabels: Record = { - '--date-style': { label: `<${dateStyles.join('|')}>` }, - '--time-style': { label: `<${timeStyles.join('|')}>` }, - '--hour-cycles': { label: `<${hourCycles.join('|')}>` }, - '--local': { label: `` }, - '--date': { label: `` }, - '--template': { label: `