Skip to content

Commit 830cfc1

Browse files
committed
Fixed parsing regression - unrecognized quoted args beginning with a hyphen no longer strip the hyphen.
1 parent a1660c3 commit 830cfc1

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@author.io/arg",
3-
"version": "1.3.20",
3+
"version": "1.3.21",
44
"description": "An argument parser for CLI applications.",
55
"main": "./src/index.js",
66
"exports": {
@@ -55,7 +55,7 @@
5555
},
5656
"dev": {
5757
"alias": {
58-
"@author.io/arg": "/app/.dist/@author.io/arg/index.js"
58+
"@author.io/arg": "/app/.dist/arg/index.js"
5959
}
6060
},
6161
"standard": {

src/flag.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class Flag {
2424
}
2525

2626
this.#rawName = cfg.name
27-
this.#name = cfg.name.replace(/^-+/gi, '').trim()
27+
this.#name = cfg.name // .replace(/^-+/gi, '').trim()
2828

2929
if (cfg.hasOwnProperty('description')) { // eslint-disable-line no-prototype-builtins
3030
this.#description = cfg.description

src/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Parser {
1414
#aliases = new Set()
1515
#validFlags = null
1616
#length = 0
17+
#quotedFlags = new Set()
1718

1819
#cleanFlag = flag => {
1920
return flag.replace(/^-+/g, '').trim().toLowerCase()
@@ -181,17 +182,18 @@ class Parser {
181182

182183
// Normalize each flag/value pairing
183184
Array.from([...input.matchAll(PARSER)]).forEach(parsedArg => {
184-
let { flag, value, unquoted_value, quoted_arg, arg } = parsedArg.groups
185+
let { flag, value, unquoted_value, quoted_arg, arg } = parsedArg.groups // eslint-disable-line camelcase
185186

186187
// If the arg attribute is present, add the
187188
// value to the arguments placeholder instead
188189
// of the flags
189190
if (arg) {
190191
args.push(arg)
191-
} else if (quoted_arg) {
192+
} else if (quoted_arg) { // eslint-disable-line camelcase
192193
args.push(quoted_arg)
194+
this.#quotedFlags.add(this.#cleanFlag(quoted_arg))
193195
} else {
194-
value = unquoted_value || value
196+
value = unquoted_value || value // eslint-disable-line camelcase
195197
// Flags without values are considered boolean "true"
196198
value = value !== undefined ? value : true
197199

@@ -236,7 +238,7 @@ class Parser {
236238
}
237239
}
238240

239-
if (typeof flag.value !== flag.type) {
241+
if (typeof flag.value !== flag.type) { // eslint-disable-line valid-typeof
240242
if (flag.type === 'boolean') {
241243
if (flag.value === null) {
242244
flag.value = false
@@ -268,7 +270,8 @@ class Parser {
268270
addFlag (cfg) {
269271
cfg = typeof cfg === 'object' ? cfg : { name: cfg }
270272

271-
const clean = this.#cleanFlag(cfg.name)
273+
const preclean = this.#cleanFlag(cfg.name)
274+
const clean = this.#quotedFlags.has(preclean) ? cfg.name : preclean
272275

273276
if (this.#flags.has(clean)) {
274277
throw new Error(`"${cfg.name}" flag already exists.`)

tests/09-regression.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,12 @@ test('Boolean flags followed by unnamed string argument', t => {
148148

149149
t.end()
150150
})
151+
152+
test('Flags with hyphens should not strip hypen', t => {
153+
const input = `session account api_profiles remove "-d1pkFXOnEmvbi-xwDJ8H"`
154+
const { data } = new Parser(input)
155+
156+
t.expect(true, data['-d1pkFXOnEmvbi-xwDJ8H'], 'hyphenated argument recognized')
157+
158+
t.end()
159+
})

0 commit comments

Comments
 (0)