Skip to content

Commit cc8a376

Browse files
author
Tom X. Tobin
committed
Make new optional when instantiating ArgumentParser
These are now equivalent: - `var parser = ArgumentParser()` - `var parser = new ArgumentParser()` I'm a fan of APIs that save you from having to always call `new` on the constructor function. :-)
1 parent 19103af commit cc8a376

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

lib/argument_parser.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ var Namespace = require('./namespace');
5353
* [1]:http://docs.python.org/dev/library/argparse.html#argumentparser-objects
5454
**/
5555
var ArgumentParser = module.exports = function ArgumentParser(options) {
56+
if (!(this instanceof ArgumentParser)) {
57+
return new ArgumentParser(options);
58+
}
5659
var self = this;
5760
options = options || {};
5861

test/base.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,12 @@ describe('base', function () {
244244
args = parser.parseArgs([ '-y' ]);
245245
});
246246
});
247+
248+
it('should support instantiation without new', function () {
249+
assert.doesNotThrow(function () {
250+
/* jshint -W064 */
251+
parser = ArgumentParser({debug: true});
252+
/* jshint +W064 */
253+
});
254+
});
247255
});

0 commit comments

Comments
 (0)