-
Notifications
You must be signed in to change notification settings - Fork 992
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #331 from bcoe/parse-string
allow yargs.parse() and yargs(foo).argv to accept a raw argument string
- Loading branch information
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// take an un-split argv string and tokenize it. | ||
module.exports = function (argString) { | ||
var i = 0 | ||
var c = null | ||
var opening = null | ||
var args = [] | ||
|
||
for (var ii = 0; ii < argString.length; ii++) { | ||
c = argString.charAt(ii) | ||
|
||
// split on spaces unless we're in quotes. | ||
if (c === ' ' && !opening) { | ||
i++ | ||
continue | ||
} | ||
|
||
// don't split the string if we're in matching | ||
// opening or closing single and double quotes. | ||
if (c === opening) { | ||
opening = null | ||
continue | ||
} else if ((c === "'" || c === '"') && !opening) { | ||
opening = c | ||
continue | ||
} | ||
|
||
if (!args[i]) args[i] = '' | ||
args[i] += c | ||
} | ||
|
||
return args | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* global describe, it */ | ||
|
||
var tokenizeArgString = require('../lib/tokenize-arg-string') | ||
|
||
require('chai').should() | ||
|
||
describe('TokenizeArgString', function () { | ||
it('handles unquoted string', function () { | ||
var args = tokenizeArgString('--foo 99') | ||
args[0].should.equal('--foo') | ||
args[1].should.equal('99') | ||
}) | ||
|
||
it('handles quoted string with no spaces', function () { | ||
var args = tokenizeArgString("--foo 'hello'") | ||
args[0].should.equal('--foo') | ||
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') | ||
}) | ||
|
||
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 quoted string with embeded 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"') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters