From 127aea62b11858e78d0f0be268958301e8c14b23 Mon Sep 17 00:00:00 2001 From: "idan.d" Date: Thu, 31 May 2018 19:09:46 +0300 Subject: [PATCH 1/5] added tests for multiple flag input --- test/parse.test.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/parse.test.ts b/test/parse.test.ts index fe43694..ea20bb2 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -277,6 +277,33 @@ See more help with --help`) // }) }) + describe('flag with multiple inputs', () => { + it('flag multiple with flag in the middle', () => { + const out = parse(['--foo=bar', '--foo', '100', '--hello', 'world'], { + flags: {foo: flags.string({multiple: true}), hello: flags.string()}, + }) + expect(out.flags).to.deep.include({foo: ['bar', '100']}) + expect(out.flags).to.deep.include({hello: 'world'}) + }) + + it('flag multiple without flag in the middle', () => { + const out = parse(['--foo', './a.txt', './b.txt', './c.txt', '--hello', 'world'], { + flags: {foo: flags.string({multiple: true}), hello: flags.string()}, + }) + expect(out.flags).to.deep.include({foo: ['./a.txt', './b.txt', './c.txt']}) + expect(out.flags).to.deep.include({hello: 'world'}) + }) + + it('flag multiple with arguments', () => { + const out = parse(['--foo', './a.txt', './b.txt', './c.txt', '--', '15'], { + args: [{name: 'num'}], + flags: {foo: flags.string({multiple: true})}, + }) + expect(out.flags).to.deep.include({foo: ['./a.txt', './b.txt', './c.txt']}) + expect(out.args).to.deep.include({num: '15'}) + }) + }) + describe('defaults', () => { it('defaults', () => { const out = parse([], { From 554fe8cbf43544efa3fe8c0f97f2eaa6d3ffe113 Mon Sep 17 00:00:00 2001 From: "idan.d" Date: Thu, 31 May 2018 19:10:23 +0300 Subject: [PATCH 2/5] added multiple flag input handling withou the flag before each input --- src/parse.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/parse.ts b/src/parse.ts index 04e1cb3..a61953f 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -47,8 +47,10 @@ export class Parser } private readonly context: any + private _currentFlag: Flags.IOptionFlag | null constructor(private readonly input: T) { const {pickBy} = m.util + this._currentFlag = null this.context = input.context || {} this.argv = input.argv.slice(0) this._setNames() @@ -92,6 +94,7 @@ export class Parser Date: Thu, 31 May 2018 20:24:57 +0300 Subject: [PATCH 3/5] no underscore prefix --- src/parse.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/parse.ts b/src/parse.ts index a61953f..5a8f5f2 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -47,10 +47,10 @@ export class Parser } private readonly context: any - private _currentFlag: Flags.IOptionFlag | null + private currentFlag: Flags.IOptionFlag | null constructor(private readonly input: T) { const {pickBy} = m.util - this._currentFlag = null + this.currentFlag = null this.context = input.context || {} this.argv = input.argv.slice(0) this._setNames() @@ -94,7 +94,7 @@ export class Parser Date: Fri, 1 Jun 2018 10:59:09 +0300 Subject: [PATCH 4/5] use undefined instead of null --- src/parse.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/parse.ts b/src/parse.ts index 5a8f5f2..dae89a4 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -47,10 +47,10 @@ export class Parser } private readonly context: any - private currentFlag: Flags.IOptionFlag | null + private currentFlag: Flags.IOptionFlag | undefined constructor(private readonly input: T) { const {pickBy} = m.util - this.currentFlag = null + this.currentFlag = undefined this.context = input.context || {} this.argv = input.argv.slice(0) this._setNames() From 2b3c0947b56a345e1b7c4626dfc603c40b0bdf99 Mon Sep 17 00:00:00 2001 From: idan Date: Fri, 1 Jun 2018 11:45:41 +0300 Subject: [PATCH 5/5] current flag to be optional property --- src/parse.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/parse.ts b/src/parse.ts index dae89a4..e4e0be2 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -47,10 +47,9 @@ export class Parser } private readonly context: any - private currentFlag: Flags.IOptionFlag | undefined + private currentFlag?: Flags.IOptionFlag constructor(private readonly input: T) { const {pickBy} = m.util - this.currentFlag = undefined this.context = input.context || {} this.argv = input.argv.slice(0) this._setNames()