Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 145 #146

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ function parse (args, opts) {
} else {
next = args[i + 1]

if (next !== undefined && (!next.match(/^-/) ||
const leadingDashWithQuotes = /"(-.+)"$/
if (leadingDashWithQuotes.test(next)) {
setArg(key, leadingDashWithQuotes.exec(next)[1])
i++
} else if (next !== undefined && (!next.match(/^-/) ||
next.match(negative)) &&
!checkAllAliases(key, flags.bools) &&
!checkAllAliases(key, flags.counts)) {
Expand Down
17 changes: 17 additions & 0 deletions lib/tokenize-arg-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module.exports = function (argString) {
var c = null
var opening = null
var args = []
// Opening and closing quotes around values that start with a dash
var quote = null

for (var ii = 0; ii < argString.length; ii++) {
prevC = c
Expand All @@ -26,8 +28,23 @@ module.exports = function (argString) {
// opening or closing single and double quotes.
if (c === opening) {
opening = null
// If the previous element was a flag, and the quote is open, close the
// open quote with a '"'
if (args[i - 1].startsWith('--') && quote === 'open') {
quote = null
args[i] += '"'
}
continue
} else if ((c === "'" || c === '"') && !opening) {
// If the previous element was a flag, next character is a '-', prefix
// the current element with a '"'
// See: https://github.com/yargs/yargs-parser/issues/145
if (args[i - 1].startsWith('--')) {
if (argString.charAt(ii + 1) === '-') {
if (!args[i]) args[i] = '"'
quote = 'open'
}
}
opening = c
continue
}
Expand Down
14 changes: 14 additions & 0 deletions test/tokenize-arg-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,27 @@ describe('TokenizeArgString', function () {
args[2].should.equal('--bar=foo bar')
})

it('handles single quoted string with leading dash', function () {
var args = tokenizeArgString("--foo '-helloWorld' --bar='-fooBar'")
args[0].should.equal('--foo')
args[1].should.equal('"-helloWorld"')
args[2].should.equal('--bar=-fooBar')
})

it('handles double quoted string with spaces', function () {
var args = tokenizeArgString('--foo "hello world" --bar="foo bar"')
args[0].should.equal('--foo')
args[1].should.equal('hello world')
args[2].should.equal('--bar=foo bar')
})

it('handles double quoted string with leading dash', function () {
var args = tokenizeArgString('--foo "-helloWorld" --bar="-fooBar"')
args[0].should.equal('--foo')
args[1].should.equal('"-helloWorld"')
args[2].should.equal('--bar=-fooBar')
})

it('handles quoted string with embeded quotes', function () {
var args = tokenizeArgString('--foo "hello \'world\'" --bar=\'foo "bar"\'')
args[0].should.equal('--foo')
Expand Down
17 changes: 17 additions & 0 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2745,4 +2745,21 @@ describe('yargs-parser', function () {
argv.foo[3].bla.should.equal('banana')
})
})

// see: https://github.com/yargs/yargs-parser/issues/145
describe('parses leading dash in flag value', function () {
it('parses correctly when there is a leading dash in the value', function () {
var argv = parser(['--foo', '"-helloWorld"', '--bar=-fooBar'])

argv.foo.should.equal('-helloWorld')
argv.bar.should.equal('-fooBar')
})

it('allows dashes in argument values', function () {
var argv = parser(['foo', '--arg', '"--bar --baz"'])

// fails with AssertionError: expected true to equal '--bar --baz'
argv.should.have.property('arg').and.equal('--bar --baz')
})
})
})