From 9189c22b2ef0216a09260a6cb55a525f95149b04 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Thu, 27 Jun 2019 00:30:21 -0300 Subject: [PATCH 1/3] fix: flag with default cannot be undefined --- src/flags.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flags.ts b/src/flags.ts index 77d9e0c..c4ab514 100644 --- a/src/flags.ts +++ b/src/flags.ts @@ -40,7 +40,7 @@ export type IOptionFlag = IFlagBase & { export type Definition = { (options: {multiple: true} & Partial>): IOptionFlag - (options: {required: true} & Partial>): IOptionFlag + (options: ({required: true} | {default: T}) & Partial>): IOptionFlag (options?: Partial>): IOptionFlag } From 41c6aecb9664cf0dda46287376a6080971adb7a8 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Thu, 27 Jun 2019 13:07:57 -0300 Subject: [PATCH 2/3] fix: type of flag with a default function --- src/flags.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/flags.ts b/src/flags.ts index c4ab514..4088431 100644 --- a/src/flags.ts +++ b/src/flags.ts @@ -4,6 +4,8 @@ import {AlphabetLowercase, AlphabetUppercase} from './alphabet' export type DefaultContext = { options: IOptionFlag; flags: { [k: string]: string } } +export type Default = T | ((context: DefaultContext) => T) + export type IFlagBase = { name: string char?: AlphabetLowercase | AlphabetUppercase @@ -26,13 +28,13 @@ export type IBooleanFlag = IFlagBase & { /** * specifying a default of false is the same not specifying a default */ - default?: boolean | ((context: DefaultContext) => boolean) + default?: Default } export type IOptionFlag = IFlagBase & { type: 'option' helpValue?: string - default?: T | ((context: DefaultContext) => T | undefined) + default?: Default multiple: boolean input: string[] options?: string[] @@ -40,7 +42,7 @@ export type IOptionFlag = IFlagBase & { export type Definition = { (options: {multiple: true} & Partial>): IOptionFlag - (options: ({required: true} | {default: T}) & Partial>): IOptionFlag + (options: ({required: true} | {default: Default}) & Partial>): IOptionFlag (options?: Partial>): IOptionFlag } From e7286fa06dc39ff997dbb8635ddc61504f71679c Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Wed, 10 Jul 2019 21:10:12 -0300 Subject: [PATCH 3/3] add missing type annotations --- test/parse.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/parse.test.ts b/test/parse.test.ts index 1e96f46..d642d5c 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -459,9 +459,10 @@ See more help with --help`) }) it('default has options', () => { + const def: flags.Default = ({options}) => options.description const out = parse([], { // args: [{ name: 'baz', default: () => 'BAZ' }], - flags: {foo: flags.string({description: 'bar', default: ({options}) => options.description})}, + flags: {foo: flags.string({description: 'bar', default: def})}, }) // expect(out.args).to.deep.include({ baz: 'BAZ' }) // expect(out.argv).to.deep.include(['BAZ']) @@ -469,12 +470,11 @@ See more help with --help`) }) it('can default to a different flag', () => { + const def: flags.Default = opts => opts.flags.foo const out = parse(['--foo=bar'], { flags: { bar: flags.string({ - default: opts => { - return opts.flags.foo - }, + default: def, }), foo: flags.string(), },