Skip to content

Commit

Permalink
fix: types on custom flags (#463)
Browse files Browse the repository at this point in the history
* fix: types on custom flags

* chore: fix tests
  • Loading branch information
mdonnalley committed Aug 8, 2022
1 parent 821a16d commit 2728e23
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/parser/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {URL} from 'url'

import {Definition, OptionFlag, BooleanFlag} from '../interfaces'
import {Definition, OptionFlag, BooleanFlag, Default} from '../interfaces'
import * as fs from 'fs'

export function build<T>(
Expand All @@ -12,15 +12,15 @@ export function build(
defaults: Partial<OptionFlag<string>>,
): Definition<string>
export function build<T>(defaults: Partial<OptionFlag<T>>): Definition<T> {
return (options: any = {}): any => {
return (options: any = {}) => {
return {
parse: async (i: string, _: any) => i,
...defaults,
...options,
input: [] as string[],
multiple: Boolean(options.multiple === undefined ? defaults.multiple : options.multiple),
type: 'option',
} as any
}
}
}

Expand All @@ -35,8 +35,10 @@ export function boolean<T = boolean>(
} as BooleanFlag<T>
}

export const integer = (opts: { min?: number; max?: number} & Partial<OptionFlag<number>> = {}): OptionFlag<number | undefined> => {
return build<number>({
export function integer(opts: Partial<OptionFlag<number>> & {min?: number; max?: number } & ({required: true} | { default: Default<number> })): OptionFlag<number>
export function integer(opts?: Partial<OptionFlag<number>> & {min?: number; max?: number }): OptionFlag<number | undefined>
export function integer(opts: Partial<OptionFlag<number>> & {min?: number; max?: number } = {}): OptionFlag<number> | OptionFlag<number | undefined> {
return build({
...opts,
parse: async input => {
if (!/^-?\d+$/.test(input))
Expand All @@ -51,14 +53,18 @@ export const integer = (opts: { min?: number; max?: number} & Partial<OptionFlag
})()
}

export const directory = (opts: { exists?: boolean } & Partial<OptionFlag<string>> = {}): OptionFlag<string | undefined> => {
export function directory(opts: { exists?: boolean } & Partial<OptionFlag<string>> & ({required: true} | { default: Default<string> })): OptionFlag<string>
export function directory(opts?: { exists?: boolean } & Partial<OptionFlag<string>>): OptionFlag<string | undefined>
export function directory(opts: { exists?: boolean } & Partial<OptionFlag<string>> = {}): OptionFlag<string> | OptionFlag<string | undefined> {
return build<string>({
...opts,
parse: async (input: string) => opts.exists ? dirExists(input) : input,
})()
}

export const file = (opts: { exists?: boolean } & Partial<OptionFlag<string>> = {}): OptionFlag<string | undefined> => {
export function file(opts: { exists?: boolean } & Partial<OptionFlag<string>> & ({required: true} | { default: string })): OptionFlag<string>
export function file(opts?: { exists?: boolean } & Partial<OptionFlag<string>>): OptionFlag<string | undefined>
export function file(opts: { exists?: boolean } & Partial<OptionFlag<string>> = {}): OptionFlag<string> | OptionFlag<string | undefined> {
return build<string>({
...opts,
parse: async (input: string) => opts.exists ? fileExists(input) : input,
Expand Down

0 comments on commit 2728e23

Please sign in to comment.