Commit bf5589dd bf5589dd0ce000a45d8f49d4ac9b646483cfc096 by Nicolas Perriault

fixes #164 - ability to force CLI parameters as string

1 parent 55dff511
1 CasperJS Changelog 1 CasperJS Changelog
2 ================== 2 ==================
3 3
4 XXXX-XX-XX, v1.0.0
5 ------------------
6
7 - fixed [#164](https://github.com/n1k0/casperjs/issues/164) - ability to force CLI parameters as strings
8
4 2012-06-26, v1.0.0-RC1 9 2012-06-26, v1.0.0-RC1
5 ---------------------- 10 ----------------------
6 11
......
...@@ -45,6 +45,10 @@ exports.parse = function parse(phantomArgs) { ...@@ -45,6 +45,10 @@ exports.parse = function parse(phantomArgs) {
45 var extract = { 45 var extract = {
46 args: [], 46 args: [],
47 options: {}, 47 options: {},
48 raw: {
49 args: [],
50 options: {}
51 },
48 drop: function drop(what) { 52 drop: function drop(what) {
49 if (utils.isNumber(what)) { 53 if (utils.isNumber(what)) {
50 // deleting an arg by its position 54 // deleting an arg by its position
...@@ -92,18 +96,25 @@ exports.parse = function parse(phantomArgs) { ...@@ -92,18 +96,25 @@ exports.parse = function parse(phantomArgs) {
92 var optionMatch = arg.match(/^--(.*?)=(.*)/i); 96 var optionMatch = arg.match(/^--(.*?)=(.*)/i);
93 if (optionMatch) { 97 if (optionMatch) {
94 extract.options[optionMatch[1]] = castArgument(optionMatch[2]); 98 extract.options[optionMatch[1]] = castArgument(optionMatch[2]);
99 extract.raw.options[optionMatch[1]] = optionMatch[2];
95 } else { 100 } else {
96 // flag 101 // flag
97 var flagMatch = arg.match(/^--(.*)/); 102 var flagMatch = arg.match(/^--(.*)/);
98 if (flagMatch) { 103 if (flagMatch) {
99 extract.options[flagMatch[1]] = true; 104 extract.options[flagMatch[1]] = extract.raw.options[flagMatch[1]] = true;
100 } 105 }
101 } 106 }
102 } else { 107 } else {
103 // positional arg 108 // positional arg
104 extract.args.push(arg); 109 extract.args.push(castArgument(arg));
110 extract.raw.args.push(castArgument(arg));
105 } 111 }
106 }); 112 });
113 extract.raw = utils.mergeObjects(extract.raw, {
114 drop: extract.drop,
115 has: extract.has,
116 get: extract.get
117 });
107 return extract; 118 return extract;
108 }; 119 };
109 120
......
...@@ -3,15 +3,24 @@ var cli = require('cli'), t = casper.test; ...@@ -3,15 +3,24 @@ var cli = require('cli'), t = casper.test;
3 t.comment('parse(), get(), has()'); 3 t.comment('parse(), get(), has()');
4 4
5 (function(parsed) { 5 (function(parsed) {
6 // clean
6 t.assertEquals(parsed.args, [], 'parse() returns expected positional args array'); 7 t.assertEquals(parsed.args, [], 'parse() returns expected positional args array');
7 t.assertEquals(parsed.options, {}, 'parse() returns expected options object'); 8 t.assertEquals(parsed.options, {}, 'parse() returns expected options object');
8 t.assertEquals(parsed.get(0), undefined, 'parse() does not return inexistant positional arg'); 9 t.assertEquals(parsed.get(0), undefined, 'parse() does not return inexistant positional arg');
9 t.assertEquals(parsed.get('blah'), undefined, 'parse() does not return inexistant option'); 10 t.assertEquals(parsed.get('blah'), undefined, 'parse() does not return inexistant option');
10 t.assert(!parsed.has(0), 'has() checks if an arg is set'); 11 t.assert(!parsed.has(0), 'has() checks if an arg is set');
11 t.assert(!parsed.has('blah'), 'has() checks if an option is set'); 12 t.assert(!parsed.has('blah'), 'has() checks if an option is set');
13 // raw
14 t.assertEquals(parsed.raw.args, [], 'parse() returns expected positional args array');
15 t.assertEquals(parsed.raw.options, {}, 'parse() returns expected options object');
16 t.assertEquals(parsed.raw.get(0), undefined, 'parse() does not return inexistant positional arg');
17 t.assertEquals(parsed.raw.get('blah'), undefined, 'parse() does not return inexistant option');
18 t.assert(!parsed.raw.has(0), 'has() checks if a raw arg is set');
19 t.assert(!parsed.raw.has('blah'), 'has() checks if a raw option is set');
12 })(cli.parse([])); 20 })(cli.parse([]));
13 21
14 (function(parsed) { 22 (function(parsed) {
23 // clean
15 t.assertEquals(parsed.args, ['foo', 'bar'], 'parse() returns expected positional args array'); 24 t.assertEquals(parsed.args, ['foo', 'bar'], 'parse() returns expected positional args array');
16 t.assertEquals(parsed.options, {}, 'parse() returns expected options object'); 25 t.assertEquals(parsed.options, {}, 'parse() returns expected options object');
17 t.assertEquals(parsed.get(0), 'foo', 'parse() retrieve first positional arg'); 26 t.assertEquals(parsed.get(0), 'foo', 'parse() retrieve first positional arg');
...@@ -19,25 +28,48 @@ t.comment('parse(), get(), has()'); ...@@ -19,25 +28,48 @@ t.comment('parse(), get(), has()');
19 t.assert(parsed.has(0), 'has() checks if an arg is set'); 28 t.assert(parsed.has(0), 'has() checks if an arg is set');
20 t.assert(parsed.has(1), 'has() checks if an arg is set'); 29 t.assert(parsed.has(1), 'has() checks if an arg is set');
21 t.assert(!parsed.has(2), 'has() checks if an arg is not set'); 30 t.assert(!parsed.has(2), 'has() checks if an arg is not set');
31 // raw
32 t.assertEquals(parsed.raw.args, ['foo', 'bar'], 'parse() returns expected positional raw args array');
33 t.assertEquals(parsed.raw.options, {}, 'parse() returns expected raw options object');
34 t.assertEquals(parsed.raw.get(0), 'foo', 'parse() retrieve first positional raw arg');
35 t.assertEquals(parsed.raw.get(1), 'bar', 'parse() retrieve second positional raw arg');
36 t.assert(parsed.raw.has(0), 'has() checks if a arw arg is set');
37 t.assert(parsed.raw.has(1), 'has() checks if a arw arg is set');
38 t.assert(!parsed.raw.has(2), 'has() checks if a arw arg is not set');
22 })(cli.parse(['foo', 'bar'])); 39 })(cli.parse(['foo', 'bar']));
23 40
24 (function(parsed) { 41 (function(parsed) {
42 // clean
25 t.assertEquals(parsed.args, [], 'parse() returns expected positional args array'); 43 t.assertEquals(parsed.args, [], 'parse() returns expected positional args array');
26 t.assertEquals(parsed.options, {foo: 'bar', baz: true}, 'parse() returns expected options object'); 44 t.assertEquals(parsed.options, {foo: 'bar', baz: true}, 'parse() returns expected options object');
27 t.assertEquals(parsed.get('foo'), 'bar', 'parse() retrieve an option value'); 45 t.assertEquals(parsed.get('foo'), 'bar', 'parse() retrieve an option value');
28 t.assert(parsed.get('baz'), 'parse() retrieve boolean option flag'); 46 t.assert(parsed.get('baz'), 'parse() retrieve boolean option flag');
29 t.assert(parsed.has("foo"), 'has() checks if an option is set'); 47 t.assert(parsed.has("foo"), 'has() checks if an option is set');
30 t.assert(parsed.has("baz"), 'has() checks if an option is set'); 48 t.assert(parsed.has("baz"), 'has() checks if an option is set');
49 // raw
50 t.assertEquals(parsed.raw.args, [], 'parse() returns expected positional raw args array');
51 t.assertEquals(parsed.raw.options, {foo: 'bar', baz: true}, 'parse() returns expected options raw object');
52 t.assertEquals(parsed.raw.get('foo'), 'bar', 'parse() retrieve an option raw value');
53 t.assert(parsed.raw.get('baz'), 'parse() retrieve boolean raw option flag');
54 t.assert(parsed.raw.has("foo"), 'has() checks if a raw option is set');
55 t.assert(parsed.raw.has("baz"), 'has() checks if a raw option is set');
31 })(cli.parse(['--foo=bar', '--baz'])); 56 })(cli.parse(['--foo=bar', '--baz']));
32 57
33 (function(parsed) { 58 (function(parsed) {
59 // clean
34 t.assertEquals(parsed.args, [], 'parse() returns expected positional args array'); 60 t.assertEquals(parsed.args, [], 'parse() returns expected positional args array');
35 t.assertEquals(parsed.options, { '&é"à': "42===42" }, 'parse() returns expected options object'); 61 t.assertEquals(parsed.options, { '&é"à': "42===42" }, 'parse() returns expected options object');
36 t.assertEquals(parsed.get('&é"à'), "42===42", 'parse() handles options with exotic names'); 62 t.assertEquals(parsed.get('&é"à'), "42===42", 'parse() handles options with exotic names');
37 t.assert(parsed.has('&é"à'), 'has() checks if an option is set'); 63 t.assert(parsed.has('&é"à'), 'has() checks if an option is set');
64 // raw
65 t.assertEquals(parsed.raw.args, [], 'parse() returns expected positional raw args array');
66 t.assertEquals(parsed.raw.options, { '&é"à': "42===42" }, 'parse() returns expected options raw object');
67 t.assertEquals(parsed.raw.get('&é"à'), "42===42", 'parse() handles raw options with exotic names');
68 t.assert(parsed.raw.has('&é"à'), 'has() checks if a raw option is set');
38 })(cli.parse(['--&é"à=42===42'])); 69 })(cli.parse(['--&é"à=42===42']));
39 70
40 (function(parsed) { 71 (function(parsed) {
72 // clean
41 t.assertEquals(parsed.args, ['foo & bar', 'baz & boz'], 'parse() returns expected positional args array'); 73 t.assertEquals(parsed.args, ['foo & bar', 'baz & boz'], 'parse() returns expected positional args array');
42 t.assertEquals(parsed.options, { universe: 42, lap: 13.37, chucknorris: true, oops: false }, 'parse() returns expected options object'); 74 t.assertEquals(parsed.options, { universe: 42, lap: 13.37, chucknorris: true, oops: false }, 'parse() returns expected options object');
43 t.assertEquals(parsed.get('universe'), 42, 'parse() can cast a numeric option value'); 75 t.assertEquals(parsed.get('universe'), 42, 'parse() can cast a numeric option value');
...@@ -65,6 +97,29 @@ t.comment('parse(), get(), has()'); ...@@ -65,6 +97,29 @@ t.comment('parse(), get(), has()');
65 chucknorris: true, 97 chucknorris: true,
66 oops: false 98 oops: false
67 }, 'drop() did not affect other options'); 99 }, 'drop() did not affect other options');
100
101 // raw
102 t.assertEquals(parsed.raw.args, ['foo & bar', 'baz & boz'], 'parse() returns expected positional raw args array');
103 t.assertEquals(parsed.raw.options, { universe: "42", lap: "13.37", chucknorris: true, oops: "false" }, 'parse() returns expected options raw object');
104 t.assertEquals(parsed.raw.get('universe'), "42", 'parse() does not a raw numeric option value');
105 t.assertEquals(parsed.raw.get('lap'), "13.37", 'parse() does not cast a raw float option value');
106 t.assertType(parsed.raw.get('lap'), "string", 'parse() does not cast a numeric value');
107 t.assert(parsed.raw.get('chucknorris'), 'parse() can get a flag value by its option name');
108 t.assertType(parsed.raw.get('oops'), "string", 'parse() can cast a boolean value');
109 t.assertEquals(parsed.raw.get('oops'), "false", 'parse() can cast a boolean value');
110
111 t.comment('drop() for raw');
112
113 parsed.raw.drop(0);
114 t.assertEquals(parsed.raw.get(0), 'baz & boz', 'drop() dropped raw arg');
115 parsed.raw.drop("universe");
116 t.assert(!parsed.raw.has("universe"), 'drop() dropped raw option');
117 t.assertEquals(parsed.raw.args, ["baz & boz"], 'drop() did not affect other raw args');
118 t.assertEquals(parsed.raw.options, {
119 lap: "13.37",
120 chucknorris: true,
121 oops: "false"
122 }, 'drop() did not affect other raw options');
68 })(cli.parse(['foo & bar', 'baz & boz', '--universe=42', '--lap=13.37', '--chucknorris', '--oops=false'])); 123 })(cli.parse(['foo & bar', 'baz & boz', '--universe=42', '--lap=13.37', '--chucknorris', '--oops=false']));
69 124
70 t.done(); 125 t.done();
......