From ba087237773e6f4b3649d03dc88f693a22681de9 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Fri, 25 Feb 2022 15:47:13 -0600 Subject: [PATCH 1/2] fix: parsing the default is wrong types --- src/parser/parse.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/parser/parse.ts b/src/parser/parse.ts index ad7a96994..c17259c78 100644 --- a/src/parser/parse.ts +++ b/src/parser/parse.ts @@ -208,12 +208,8 @@ export class Parser Date: Sat, 26 Feb 2022 20:31:14 -0600 Subject: [PATCH 2/2] test: parsing and defaults with classes --- test/parser/parse.test.ts | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/parser/parse.test.ts b/test/parser/parse.test.ts index 590213924..38d11da22 100644 --- a/test/parser/parse.test.ts +++ b/test/parser/parse.test.ts @@ -436,6 +436,55 @@ See more help with --help`) expect(out.argv).to.deep.equal([100]) }) + it('parse with a default does not parse default', async () => { + const out = await parse([], { + flags: {foo: flags.string({parse: async input => input.toUpperCase(), default: 'baz'})}, + }) + expect(out.flags).to.deep.include({foo: 'baz'}) + }) + + describe('parse with a default/value of another type (class)', async () => { + class TestClass { + public prop: string; + constructor(input: string) { + this.prop = input + } + } + it('uses default via value', async () => { + const out = await parse([], { + flags: { + foo: flags.build({ + parse: async input => new TestClass(input), + default: new TestClass('baz'), + })(), + }, + }) + expect(out.flags.foo?.prop).to.equal('baz') + }) + it('uses default via function', async () => { + const out = await parse([], { + flags: { + foo: flags.build({ + parse: async input => new TestClass(input), + default: async () => new TestClass('baz'), + })(), + }, + }) + expect(out.flags.foo?.prop).to.equal('baz') + }) + it('uses parser when value provided', async () => { + const out = await parse(['--foo=bar'], { + flags: { + foo: flags.build({ + parse: async input => new TestClass(input), + default: new TestClass('baz'), + })(), + }, + }) + expect(out.flags.foo?.prop).to.equal('bar') + }) + }) + // it('gets arg/flag in context', async () => { // const out = await parse({ // args: [{ name: 'num', parse: (_, ctx) => ctx.arg.name!.toUpperCase() }],