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

Trailing and leading quotes #222

Closed
wants to merge 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,16 @@ function parse (args, opts) {
}

function processValue (key, val) {
// strings may be quoted, clean this up as we assign values.
if (typeof val === 'string' &&
(val[0] === "'" || val[0] === '"') &&
val[val.length - 1] === val[0]
) {
val = val.substring(1, val.length - 1)
if (typeof val === 'string' && val.startsWith('\\-')) {
val = val.substring(1, val.length)
}
// strings may be quoted, clean this up as we assign values.
// if (typeof val === 'string' &&
// (val[0] === "'" || val[0] === '"') &&
// val[val.length - 1] === val[0]
// ) {
// val = val.substring(1, val.length - 1)
// }

// handle parsing boolean arguments --foo=true --bar false.
if (checkAllAliases(key, flags.bools) || checkAllAliases(key, flags.counts)) {
Expand Down
3 changes: 3 additions & 0 deletions lib/tokenize-arg-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ module.exports = function (argString) {
// don't split the string if we're in matching
// opening or closing single and double quotes.
if (c === opening) {
if (!args[i]) args[i] = ''
opening = null
continue
} else if ((c === "'" || c === '"') && !opening) {
opening = c
continue
}

if (!args[i]) args[i] = ''
Expand Down
25 changes: 13 additions & 12 deletions test/tokenize-arg-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,43 @@ describe('TokenizeArgString', function () {
it('handles quoted string with no spaces', function () {
var args = tokenizeArgString("--foo 'hello'")
args[0].should.equal('--foo')
args[1].should.equal("'hello'")
args[1].should.equal('hello')
})

it('handles single 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'")
args[1].should.equal('hello world')
args[2].should.equal('--bar=foo bar')
})

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"')
args[1].should.equal('hello world')
args[2].should.equal('--bar=foo bar')
})

it('handles single quoted empty string', function () {
var args = tokenizeArgString('--foo \'\' --bar=\'\'')
console.log(args)
args[0].should.equal('--foo')
args[1].should.equal("''")
args[2].should.equal("--bar=''")
args[1].should.equal('')
args[2].should.equal('--bar=')
})

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

it('handles quoted string with embedded quotes', 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"\'')
args[1].should.equal('hello \'world\'')
args[2].should.equal('--bar=foo "bar"')
})

// https://github.com/yargs/yargs-parser/pull/100
Expand All @@ -65,6 +66,6 @@ describe('TokenizeArgString', function () {
var args = tokenizeArgString(' foo bar "foo bar" ')
args[0].should.equal('foo')
expect(args[1]).equal('bar')
expect(args[2]).equal('"foo bar"')
expect(args[2]).equal('foo bar')
})
})
13 changes: 8 additions & 5 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3270,23 +3270,26 @@ describe('yargs-parser', function () {
args.foo.should.equal('hello world')
args.bar.should.equal('goodnight\'moon')
const args2 = parser(['--foo', '"hello world"', '--bar="goodnight\'moon"'])
args2.foo.should.equal('hello world')
args2.bar.should.equal('goodnight\'moon')
args2.foo.should.equal('"hello world"')
args2.bar.should.equal('"goodnight\'moon"')
})

it('handles single quoted strings', function () {
const args = parser("--foo 'hello world' --bar='goodnight\"moon'")
args.foo.should.equal('hello world')
args.bar.should.equal('goodnight"moon')
const args2 = parser(['--foo', "'hello world'", "--bar='goodnight\"moon'"])
args2.foo.should.equal('hello world')
args2.bar.should.equal('goodnight"moon')
args2.foo.should.equal("'hello world'")
args2.bar.should.equal("'goodnight\"moon'")
})

it('handles strings with dashes', function () {
const args = parser('--foo "-hello world" --bar="--goodnight moon"')
const args = parser('--foo "\\-hello world" --bar="--goodnight moon"')
args.foo.should.equal('-hello world')
args.bar.should.equal('--goodnight moon')
const args2 = parser(['--foo', '\\-hello world', '--bar=--goodnight moon'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love the \\ syntax, is there prior art this is based on?

args2.foo.should.equal('-hello world')
args2.bar.should.equal('--goodnight moon')
})
})

Expand Down