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

Ensure empty string is added into argv._ #140

Merged
merged 2 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions lib/tokenize-arg-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function (argString) {
// split on spaces unless we're in quotes.
if (c === ' ' && !opening) {
if (!(prevC === ' ')) {
if (!args[i]) args[i] = ''
Copy link
Member

Choose a reason for hiding this comment

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

👋 for this fix, I think it would make sense to move the logic down to the check for the closing quote. So something like:

    if (c === opening) {
      if (!args[i]) args[i] = ''
      opening = null
      continue
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see how that would make sense - placing it at the point of detecting the closing quotes. Both ways pass the tests, so I'll commit the change.

The reason I put it where I did was to ensure that we can't possibly increment i before we've checked for a missing argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Committed :)

i++
}
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 @@ -32,6 +32,20 @@ describe('TokenizeArgString', function () {
args[2].should.equal('--bar=foo bar')
})

it('handles single quoted empty string', function () {
var args = tokenizeArgString('--foo \'\' --bar=\'\'')
args[0].should.equal('--foo')
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=')
})

it('handles quoted string with embeded quotes', function () {
var args = tokenizeArgString('--foo "hello \'world\'" --bar=\'foo "bar"\'')
args[0].should.equal('--foo')
Expand Down