From c3e9ae9c621ed63c37e2b64b4fa0e3d2c63d7a0d Mon Sep 17 00:00:00 2001 From: Willie Ruemmele Date: Tue, 27 Jun 2023 08:30:23 -0600 Subject: [PATCH] Revert "fix: improve flag validation" --- src/parser/parse.ts | 13 +++++----- test/parser/parse.test.ts | 54 --------------------------------------- 2 files changed, 6 insertions(+), 61 deletions(-) diff --git a/src/parser/parse.ts b/src/parser/parse.ts index dff54b7ca..ba6ceab35 100644 --- a/src/parser/parse.ts +++ b/src/parser/parse.ts @@ -86,7 +86,7 @@ export class Parser> { this._debugInput() - const findLongFlag = (arg: string):string | undefined => { + const findLongFlag = (arg: string) => { const name = arg.slice(2) if (this.input.flags[name]) { return name @@ -102,12 +102,12 @@ export class Parser { + const findShortFlag = ([_, char]: string) => { if (this.flagAliases[char]) { return this.flagAliases[char].name } - return Object.keys(this.input.flags).find(k => (this.input.flags[k].char === char && char !== undefined && this.input.flags[k].char !== undefined)) + return Object.keys(this.input.flags).find(k => this.input.flags[k].char === char) } const parseFlag = (arg: string): boolean => { @@ -133,13 +133,12 @@ export class Parser { expect(Boolean(out.flags.myflag2)).to.equal(true) }) - it('throws error when no value provided to required flag', async () => { - try { - await parse(['--myflag', '--second', 'value'], { - flags: {myflag: Flags.string({required: true}), second: Flags.string()}, - }) - assert.fail('should have thrown') - } catch (error) { - expect((error as CLIError).message).to.include('Flag --myflag expects a value') - } - }) - - it('throws error when no value provided to required flag before a short char flag', async () => { - try { - await parse(['--myflag', '-s', 'value'], { - flags: {myflag: Flags.string({required: true}), second: Flags.string({char: 's'})}, - }) - assert.fail('should have thrown') - } catch (error) { - expect((error as CLIError).message).to.include('Flag --myflag expects a value') - } - }) - - it('doesn\'t throw when boolean flag passed', async () => { - const out = await parse(['--myflag', '--second', 'value'], { - flags: {myflag: Flags.boolean(), second: Flags.string()}, - }) - expect(out.flags.myflag).to.be.true - expect(out.flags.second).to.equal('value') - }) - - it('doesn\'t throw when negative number passed', async () => { - const out = await parse(['--myflag', '-s', '-9'], { - flags: {myflag: Flags.boolean(), second: Flags.integer({char: 's'})}, - }) - expect(out.flags.myflag).to.be.true - expect(out.flags.second).to.equal(-9) - }) - - it('doesn\'t throw when boolean short char is passed', async () => { - const out = await parse(['--myflag', '-s', 'value'], { - flags: {myflag: Flags.boolean(), second: Flags.string({char: 's'})}, - }) - expect(out.flags.myflag).to.be.true - expect(out.flags.second).to.equal('value') - }) - - it('doesn\'t throw when short char is passed as a string value', async () => { - const out = await parse(['--myflag', '\'-s\'', '-s', 'value'], { - flags: {myflag: Flags.string(), second: Flags.string({char: 's'})}, - }) - expect(out.flags.myflag).to.equal('\'-s\'') - expect(out.flags.second).to.equal('value') - }) - it('parses short flags', async () => { const out = await parse(['-mf'], { flags: {