Skip to content

Commit

Permalink
fix: allow negative num args (#601)
Browse files Browse the repository at this point in the history
* fix: allow negative number arguments

* fix: parser error tests
  • Loading branch information
mdonnalley authored Jan 25, 2023
1 parent 013c48b commit 0540835
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@
"pretest": "yarn build --noEmit && tsc -p test --noEmit --skipLibCheck"
},
"types": "lib/index.d.ts"
}
}
12 changes: 9 additions & 3 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ const readStdin = async (): Promise<string | null> => {
})
}

function isNegativeNumber(input: string): boolean {
return /^-\d/g.test(input)
}

export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']>, BFlags extends OutputFlags<T['flags']>, TArgs extends OutputArgs<T['args']>> {
private readonly argv: string[]

Expand Down Expand Up @@ -169,7 +173,7 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
continue
}

if (this.input['--'] !== false) {
if (this.input['--'] !== false && !isNegativeNumber(input)) {
// At this point we have a value that begins with '-' or '--'
// but doesn't match up to a flag definition. So we assume that
// this is a misspelled flag or a non-existent flag,
Expand Down Expand Up @@ -230,7 +234,7 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
flags[token.flag] = flags[token.flag] || []
flags[token.flag].push(...values)
} else {
const value = flag.parse ? await flag.parse(input, this.context, flag) : input
const value = await this._parseFlag(input, flag)
if (flag.multiple) {
flags[token.flag] = flags[token.flag] || []
flags[token.flag].push(value)
Expand All @@ -250,7 +254,7 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
if (input) {
this._validateOptions(flag, input)

flags[k] = await flag.parse(input, this.context, flag)
flags[k] = await this._parseFlag(input, flag)
}
} else if (flag.type === 'boolean') {
// eslint-disable-next-line no-negated-condition
Expand All @@ -269,6 +273,8 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
}

private async _parseFlag(input: any, flag: BooleanFlag<any> | OptionFlag<any>) {
if (!flag.parse) return input

try {
if (flag.type === 'boolean') {
return await flag.parse(input, this.context, flag)
Expand Down
7 changes: 7 additions & 0 deletions test/parser/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ See more help with --help`)
}
})

it('parses negative number arg', async () => {
const out = await parse(['-119.1949853', '34.14986578'], {
args: {longitude: Args.string(), latitude: Args.string()},
})
expect(out.argv).to.deep.equal(['-119.1949853', '34.14986578'])
})

it('parses - as an arg', async () => {
const out = await parse(['-'], {
args: {myarg: Args.string()},
Expand Down

0 comments on commit 0540835

Please sign in to comment.