jenkins-bot has submitted this change and it was merged.

Change subject: Refactor 'booleanOption' helper (for optimist package) into 
Util.
......................................................................


Refactor 'booleanOption' helper (for optimist package) into Util.

This makes processing boolean command-line options more user-friendly
by allowing --debug=false and --debug=no in addition to --no-debug.

Change-Id: I264660332becd7f30f21d361de012089b532452e
---
M js/lib/mediawiki.Util.js
M js/tests/domdiff.test.js
M js/tests/dumpGrepper.js
M js/tests/parse.js
M js/tests/parserTests.js
M js/tests/roundtrip-test.js
6 files changed, 36 insertions(+), 32 deletions(-)

Approvals:
  Subramanya Sastry: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/js/lib/mediawiki.Util.js b/js/lib/mediawiki.Util.js
index e91001b..9e8e1a5 100644
--- a/js/lib/mediawiki.Util.js
+++ b/js/lib/mediawiki.Util.js
@@ -36,7 +36,7 @@
         * @returns {Object} The modified object.
         */
        setDebuggingFlags: function(obj, opts) {
-               obj.debug = opts.debug;
+               obj.debug = Util.booleanOption( opts.debug );
                obj.trace = (opts.trace === true);
                obj.traceFlags = opts.trace && opts.trace !== true ? 
opts.trace.split(",") : null;
                obj.dumpFlags = opts.dump ? opts.dump.split(",") : null;
@@ -47,6 +47,25 @@
        /**
         * @method
         *
+        * Parse a boolean option returned by the optimist package.
+        * The strings 'false' and 'no' are also treated as false values.
+        * This allows --debug=no and --debug=false to mean the same as
+        * --no-debug.
+        *
+        * @param {Boolean} a boolean, or a string naming a boolean value.
+        */
+       booleanOption: function ( val ) {
+               if ( !val ) { return false; }
+               if ( (typeof val) === 'string' &&
+                        /^(no|false)$/i.test(val)) {
+                       return false;
+               }
+               return true;
+       },
+
+       /**
+        * @method
+        *
         * Update only those properties that are undefined or null
         * $.extend updates properties that are falsy (which means false gets 
updated as well)
         *
diff --git a/js/tests/domdiff.test.js b/js/tests/domdiff.test.js
index 26b3a9b..2d3af6e 100755
--- a/js/tests/domdiff.test.js
+++ b/js/tests/domdiff.test.js
@@ -41,24 +41,13 @@
        newhtml = fs.readFileSync(argv._[1], 'utf8')
 }
 
-// user-friendly 'boolean' command-line options:
-// allow --debug=no and --debug=false to mean the same as --no-debug
-var booleanOption = function ( val ) {
-       if ( !val ) { return false; }
-       if ( (typeof val) === 'string' &&
-            /^(no|false)$/i.test(val)) {
-               return false;
-       }
-       return true;
-};
-
-if (booleanOption( argv.help ) || !oldhtml || !newhtml) {
+if (Util.booleanOption( argv.help ) || !oldhtml || !newhtml) {
        optimist.showHelp();
        return;
 }
 
 var dummyEnv = {
-       conf: { parsoid: { debug: booleanOption( argv.debug ) } },
+       conf: { parsoid: { debug: Util.booleanOption( argv.debug ) } },
        page: { id: null },
        isParsoidObjectId: function() { return true; }
 };
@@ -68,7 +57,7 @@
        newDOM = Util.parseHTML(newhtml);
 
 dd.doDOMDiff(oldDOM, newDOM);
-if ( !booleanOption( argv.quiet ) ) {
+if ( !Util.booleanOption( argv.quiet ) ) {
        console.warn("----- DIFF-marked DOM -----");
 }
 console.log(newDOM.outerHTML );
diff --git a/js/tests/dumpGrepper.js b/js/tests/dumpGrepper.js
old mode 100644
new mode 100755
index 66882a3..f1c3de3
--- a/js/tests/dumpGrepper.js
+++ b/js/tests/dumpGrepper.js
@@ -1,3 +1,4 @@
+#!/usr/bin/env node
 /**
  * A simple dump grepper based on the DumpReader module.
  */
@@ -5,7 +6,8 @@
 var dumpReader = require('./dumpReader.js'),
        events = require('events'),
        optimist = require('optimist'),
-       colors = require('colors');
+       colors = require('colors'),
+       Util = require( '../lib/mediawiki.Util.js' ).Util;
 
 function DumpGrepper ( regexp ) {
        // inherit from EventEmitter
@@ -50,7 +52,7 @@
        }
 
        var flags = 'g';
-       if(argv.i) {
+       if( Util.booleanOption( argv.i ) ) {
                flags += 'i';
        }
 
diff --git a/js/tests/parse.js b/js/tests/parse.js
index 0986e1b..e99c62a 100755
--- a/js/tests/parse.js
+++ b/js/tests/parse.js
@@ -175,7 +175,7 @@
 
        var argv = opts.argv;
 
-       if ( argv.help ) {
+       if ( Util.booleanOption( argv.help ) ) {
                optimist.showHelp();
                console.error(traceUsage());
                console.error("\n");
@@ -212,10 +212,10 @@
                }
 
                // XXX: add options for this!
-               env.conf.parsoid.fetchTemplates = argv.fetchTemplates;
-               env.conf.parsoid.usePHPPreProcessor = 
env.conf.parsoid.fetchTemplates && argv.usephppreprocessor;
+               env.conf.parsoid.fetchTemplates = Util.booleanOption( 
argv.fetchTemplates );
+               env.conf.parsoid.usePHPPreProcessor = 
env.conf.parsoid.fetchTemplates && Util.booleanOption( argv.usephppreprocessor 
);
                env.conf.parsoid.maxDepth = argv.maxdepth || 
env.conf.parsoid.maxDepth;
-               env.conf.parsoid.editMode = argv.editMode;
+               env.conf.parsoid.editMode = Util.booleanOption( argv.editMode );
 
                Util.setDebuggingFlags( env.conf.parsoid, argv );
 
diff --git a/js/tests/parserTests.js b/js/tests/parserTests.js
index e10d38a..14ab488 100755
--- a/js/tests/parserTests.js
+++ b/js/tests/parserTests.js
@@ -29,6 +29,7 @@
        alea = require('alea'),
        // Handle options/arguments with optimist module
        optimist = require('optimist');
+var booleanOption = Util.booleanOption; // shortcut
 
 // Run a mock API in the background so we can request things from it
 var forkedAPI = fork( __dirname + '/mockAPI.js', [], { silent: true } );
@@ -139,16 +140,6 @@
        }).join(' ');
 };
 
-// user-friendly 'boolean' command-line options:
-// allow --debug=no and --debug=false to mean the same as --no-debug
-var booleanOption = function ( val ) {
-       if ( !val ) { return false; }
-       if ( (typeof val) === 'string' &&
-            /^(no|false)$/i.test(val)) {
-               return false;
-       }
-       return true;
-};
 
 /**
  * @method
diff --git a/js/tests/roundtrip-test.js b/js/tests/roundtrip-test.js
old mode 100644
new mode 100755
index e907898..e356b07
--- a/js/tests/roundtrip-test.js
+++ b/js/tests/roundtrip-test.js
@@ -1,3 +1,4 @@
+#!/usr/bin/env node
 var fs = require( 'fs' ),
        path = require( 'path' ),
        colors = require( 'colors' ),
@@ -479,7 +480,9 @@
        title = argv._[0];
 
        if ( title ) {
-               callback = cbCombinator.bind( null, argv.xml ? xmlCallback : 
plainCallback, consoleOut );
+               callback = cbCombinator.bind( null,
+                                             Util.booleanOption( argv.xml ) ?
+                                             xmlCallback : plainCallback, 
consoleOut );
                fetch( title, callback, argv );
        } else {
                opts.showHelp();

-- 
To view, visit https://gerrit.wikimedia.org/r/60122
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I264660332becd7f30f21d361de012089b532452e
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Parsoid
Gerrit-Branch: master
Gerrit-Owner: Cscott <wikime...@cscott.net>
Gerrit-Reviewer: GWicke <gwi...@wikimedia.org>
Gerrit-Reviewer: Subramanya Sastry <ssas...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to