Skip to content

Commit

Permalink
fix: handling quoted strings and options validation for comma-delimit…
Browse files Browse the repository at this point in the history
…ed multiple-flag (#614)

* fix: handling quoted strings and options validation for comma-delimited multiple-flag

* chore: comment typo

* refactor: string flags

* refactor: single-quotes work, too.

* test: interior quote preservation
  • Loading branch information
mshanemc authored Feb 7, 2023
1 parent 4407b69 commit c32ab9d
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 38 deletions.
10 changes: 8 additions & 2 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,21 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
flags[token.flag] = await this._parseFlag(flags[token.flag], flag, token)
} else {
const input = token.input
this._validateOptions(flag, input)

if (flag.delimiter && flag.multiple) {
// split, trim, and remove surrounding doubleQuotes (which would hav been needed if the elements contain spaces)
const values = await Promise.all(
input.split(flag.delimiter).map(async v => this._parseFlag(v.trim(), flag, token)),
input.split(flag.delimiter).map(async v => this._parseFlag(v.trim().replace(/^"(.*)"$/, '$1').replace(/^'(.*)'$/, '$1'), flag, token)),
)
// then parse that each element aligns with the `options` property
for (const v of values) {
this._validateOptions(flag, v)
}

flags[token.flag] = flags[token.flag] || []
flags[token.flag].push(...values)
} else {
this._validateOptions(flag, input)
const value = await this._parseFlag(input, flag, token)
if (flag.multiple) {
flags[token.flag] = flags[token.flag] || []
Expand Down
Loading

0 comments on commit c32ab9d

Please sign in to comment.