cli.get() supports fallback values
Showing
4 changed files
with
21 additions
and
11 deletions
... | @@ -112,6 +112,7 @@ Last, all the casper test suites have been upgraded to use the new testing featu | ... | @@ -112,6 +112,7 @@ Last, all the casper test suites have been upgraded to use the new testing featu |
112 | - Added [`Tester#skip`](http://docs.casperjs.org/en/latest/modules/tester.html#skip) method | 112 | - Added [`Tester#skip`](http://docs.casperjs.org/en/latest/modules/tester.html#skip) method |
113 | - Added [`Casper#eachThen()`](http://docs.casperjs.org/en/latest/modules/casper.html#eachThen) | 113 | - Added [`Casper#eachThen()`](http://docs.casperjs.org/en/latest/modules/casper.html#eachThen) |
114 | - `cli`: Now dropping an arg or an option will be reflected in their *raw* equivalent | 114 | - `cli`: Now dropping an arg or an option will be reflected in their *raw* equivalent |
115 | - `cli.get()` now supports fallback values | ||
115 | 116 | ||
116 | 2013-02-08, v1.0.2 | 117 | 2013-02-08, v1.0.2 |
117 | ------------------ | 118 | ------------------ | ... | ... |
... | @@ -126,8 +126,8 @@ var Casper = function Casper(options) { | ... | @@ -126,8 +126,8 @@ var Casper = function Casper(options) { |
126 | this.options = utils.mergeObjects(this.defaults, options); | 126 | this.options = utils.mergeObjects(this.defaults, options); |
127 | // factories | 127 | // factories |
128 | this.cli = phantom.casperArgs; | 128 | this.cli = phantom.casperArgs; |
129 | this.options.logLevel = this.cli.get('log-level') || this.options.logLevel; | 129 | this.options.logLevel = this.cli.get('log-level', this.options.logLevel); |
130 | this.options.verbose = this.cli.get('direct') || this.options.verbose; | 130 | this.options.verbose = this.cli.get('direct', this.options.verbose); |
131 | this.colorizer = this.getColorizer(); | 131 | this.colorizer = this.getColorizer(); |
132 | this.mouse = mouse.create(this); | 132 | this.mouse = mouse.create(this); |
133 | this.popups = pagestack.create(); | 133 | this.popups = pagestack.create(); | ... | ... |
... | @@ -83,20 +83,20 @@ exports.parse = function parse(phantomArgs) { | ... | @@ -83,20 +83,20 @@ exports.parse = function parse(phantomArgs) { |
83 | has: function has(what) { | 83 | has: function has(what) { |
84 | if (utils.isNumber(what)) { | 84 | if (utils.isNumber(what)) { |
85 | return what in this.args; | 85 | return what in this.args; |
86 | } else if (utils.isString(what)) { | 86 | } |
87 | if (utils.isString(what)) { | ||
87 | return what in this.options; | 88 | return what in this.options; |
88 | } else { | ||
89 | throw new CasperError("Unsupported cli arg tester " + typeof what); | ||
90 | } | 89 | } |
90 | throw new CasperError("Unsupported cli arg tester " + typeof what); | ||
91 | }, | 91 | }, |
92 | get: function get(what) { | 92 | get: function get(what, def) { |
93 | if (utils.isNumber(what)) { | 93 | if (utils.isNumber(what)) { |
94 | return this.args[what]; | 94 | return what in this.args ? this.args[what] : def; |
95 | } else if (utils.isString(what)) { | 95 | } |
96 | return this.options[what]; | 96 | if (utils.isString(what)) { |
97 | } else { | 97 | return what in this.options ? this.options[what] : def; |
98 | throw new CasperError("Unsupported cli arg getter " + typeof what); | ||
99 | } | 98 | } |
99 | throw new CasperError("Unsupported cli arg getter " + typeof what); | ||
100 | } | 100 | } |
101 | }; | 101 | }; |
102 | phantomArgs.forEach(function _forEach(arg) { | 102 | phantomArgs.forEach(function _forEach(arg) { | ... | ... |
... | @@ -147,3 +147,12 @@ casper.test.begin('parsing commands containing args and options', 34, function(t | ... | @@ -147,3 +147,12 @@ casper.test.begin('parsing commands containing args and options', 34, function(t |
147 | 147 | ||
148 | test.done(); | 148 | test.done(); |
149 | }); | 149 | }); |
150 | |||
151 | casper.test.begin('default values', 2, function(test) { | ||
152 | var parsed = cli.parse(['foo', '--bar']); | ||
153 | test.assertEquals(parsed.get(42, 'boz'), 'boz', | ||
154 | 'get() can return a default arg value'); | ||
155 | test.assertEquals(parsed.get('--zorg', 'boz'), 'boz', | ||
156 | 'get() can return a default option value'); | ||
157 | test.done(); | ||
158 | }); | ... | ... |
-
Please register or sign in to post a comment