/
tokenize-arg-string.js
70 lines (59 loc) · 2.24 KB
/
tokenize-arg-string.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* global describe, it */
const tokenizeArgString = require('../lib/tokenize-arg-string')
require('chai').should()
const expect = require('chai').expect
describe('TokenizeArgString', function () {
it('handles unquoted string', function () {
const args = tokenizeArgString('--foo 99')
args[0].should.equal('--foo')
args[1].should.equal('99')
})
it('handles unquoted numbers', function () {
const args = tokenizeArgString(['--foo', 9])
args[0].should.equal('--foo')
args[1].should.equal('9')
})
it('handles quoted string with no spaces', function () {
const args = tokenizeArgString("--foo 'hello'")
args[0].should.equal('--foo')
args[1].should.equal("'hello'")
})
it('handles single quoted string with spaces', function () {
const 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 () {
const 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 single quoted empty string', function () {
const 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 () {
const args = tokenizeArgString('--foo "" --bar=""')
args[0].should.equal('--foo')
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"\'')
})
// https://github.com/yargs/yargs-parser/pull/100
// https://github.com/yargs/yargs-parser/pull/106
it('ignores unneeded spaces', function () {
const args = tokenizeArgString(' foo bar "foo bar" ')
args[0].should.equal('foo')
expect(args[1]).equal('bar')
expect(args[2]).equal('"foo bar"')
})
})