Commit 1fe9afcb 1fe9afcba2322fbdb815adf1ed3d7a1df94acedb by Nicolas Perriault

cli.get() supports fallback values

1 parent d7a1665a
......@@ -112,6 +112,7 @@ Last, all the casper test suites have been upgraded to use the new testing featu
- Added [`Tester#skip`](http://docs.casperjs.org/en/latest/modules/tester.html#skip) method
- Added [`Casper#eachThen()`](http://docs.casperjs.org/en/latest/modules/casper.html#eachThen)
- `cli`: Now dropping an arg or an option will be reflected in their *raw* equivalent
- `cli.get()` now supports fallback values
2013-02-08, v1.0.2
------------------
......
......@@ -126,8 +126,8 @@ var Casper = function Casper(options) {
this.options = utils.mergeObjects(this.defaults, options);
// factories
this.cli = phantom.casperArgs;
this.options.logLevel = this.cli.get('log-level') || this.options.logLevel;
this.options.verbose = this.cli.get('direct') || this.options.verbose;
this.options.logLevel = this.cli.get('log-level', this.options.logLevel);
this.options.verbose = this.cli.get('direct', this.options.verbose);
this.colorizer = this.getColorizer();
this.mouse = mouse.create(this);
this.popups = pagestack.create();
......
......@@ -83,20 +83,20 @@ exports.parse = function parse(phantomArgs) {
has: function has(what) {
if (utils.isNumber(what)) {
return what in this.args;
} else if (utils.isString(what)) {
}
if (utils.isString(what)) {
return what in this.options;
} else {
throw new CasperError("Unsupported cli arg tester " + typeof what);
}
throw new CasperError("Unsupported cli arg tester " + typeof what);
},
get: function get(what) {
get: function get(what, def) {
if (utils.isNumber(what)) {
return this.args[what];
} else if (utils.isString(what)) {
return this.options[what];
} else {
throw new CasperError("Unsupported cli arg getter " + typeof what);
return what in this.args ? this.args[what] : def;
}
if (utils.isString(what)) {
return what in this.options ? this.options[what] : def;
}
throw new CasperError("Unsupported cli arg getter " + typeof what);
}
};
phantomArgs.forEach(function _forEach(arg) {
......
......@@ -147,3 +147,12 @@ casper.test.begin('parsing commands containing args and options', 34, function(t
test.done();
});
casper.test.begin('default values', 2, function(test) {
var parsed = cli.parse(['foo', '--bar']);
test.assertEquals(parsed.get(42, 'boz'), 'boz',
'get() can return a default arg value');
test.assertEquals(parsed.get('--zorg', 'boz'), 'boz',
'get() can return a default option value');
test.done();
});
......