Skip to content
Browse files

Use an evil global so that different modules may bring in the flags l…

…ibrary from different places
  • Loading branch information...
1 parent 8941c8f commit 8e4021ea31cdad224eb276955a848e2171dd1076 @dpup committed Apr 15, 2011
Showing with 16 additions and 3 deletions.
  1. +9 −3 lib/flags.js
  2. +7 −0 lib/flags_test.js
View
12 lib/flags.js
@@ -8,10 +8,16 @@ var util = require('util');
/**
- * An object containing a map of flag objects.
+ * An object containing a map of flag objects. If different modules are
+ * importing different versions of the library we want them to use the same
+ * FLAGS object. This does mean that the flags library needs to be backwards
+ * compatible.
* @type {!Object}
*/
-var FLAGS = exports.FLAGS = {};
+if (typeof GLOBAL_FLAGS == 'undefined') {
+ GLOBAL_FLAGS = {};
+}
+var FLAGS = exports.FLAGS = GLOBAL_FLAGS;
/**
@@ -130,7 +136,7 @@ exports.help = function() {
*/
exports.reset = function() {
parseCalled = false;
- FLAGS = exports.FLAGS = {};
+ FLAGS = exports.FLAGS = GLOBAL_FLAGS = {};
registerInternalFlags();
};
View
7 lib/flags_test.js
@@ -2,6 +2,13 @@
var flags = require('flags');
flags.exitOnError = false;
+exports.testGlobalFlagsObject = function(test) {
+ flags.defineString('one', '111');
+ flags.defineString('two', '222');
+ test.deepEqual(global.GLOBAL_FLAGS, flags.FLAGS);
+ test.done();
+};
+
exports.testStringFlagParsing = function(test) {
flags.reset();
flags.defineString('one', '111');

0 comments on commit 8e4021e

Please sign in to comment.
Something went wrong with that request. Please try again.