[Pkg-javascript-commits] [node-optimist] 13/33: Imported Upstream version 0.6.0

Sebastiaan Couwenberg sebastic at moszumanska.debian.org
Fri Feb 27 23:21:47 UTC 2015


This is an automated email from the git hooks/post-receive script.

sebastic pushed a commit to branch master
in repository node-optimist.

commit ff0e3bf2090c29b75b0f2ce29f9bf9d987837e1b
Author: Bas Couwenberg <sebastic at xs4all.nl>
Date:   Fri Feb 27 20:00:33 2015 +0100

    Imported Upstream version 0.6.0
---
 index.js     | 223 +++++++++--------------------------------------------------
 package.json |   5 +-
 2 files changed, 37 insertions(+), 191 deletions(-)

diff --git a/index.js b/index.js
index 5f3d53a..4da5a6d 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,5 @@
 var path = require('path');
+var minimist = require('minimist');
 var wordwrap = require('wordwrap');
 
 /*  Hack an instance of Argv with process.argv into Argv
@@ -17,7 +18,7 @@ Object.keys(inst).forEach(function (key) {
 });
 
 var exports = module.exports = Argv;
-function Argv (args, cwd) {
+function Argv (processArgs, cwd) {
     var self = {};
     if (!cwd) cwd = process.cwd();
     
@@ -37,50 +38,44 @@ function Argv (args, cwd) {
         );
     }
     
-    var flags = { bools : {}, strings : {} };
+    var options = {
+        boolean: [],
+        string: [],
+        alias: {},
+        default: []
+    };
     
     self.boolean = function (bools) {
-        if (!Array.isArray(bools)) {
-            bools = [].slice.call(arguments);
-        }
-        
-        bools.forEach(function (name) {
-            flags.bools[name] = true;
-        });
-        
+        options.boolean.push.apply(options.boolean, [].concat(bools));
         return self;
     };
     
     self.string = function (strings) {
-        if (!Array.isArray(strings)) {
-            strings = [].slice.call(arguments);
+        options.string.push.apply(options.string, [].concat(strings));
+        return self;
+    };
+    
+    self.default = function (key, value) {
+        if (typeof key === 'object') {
+            Object.keys(key).forEach(function (k) {
+                self.default(k, key[k]);
+            });
+        }
+        else {
+            options.default[key] = value;
         }
-        
-        strings.forEach(function (name) {
-            flags.strings[name] = true;
-        });
-        
         return self;
     };
     
-    var aliases = {};
     self.alias = function (x, y) {
         if (typeof x === 'object') {
             Object.keys(x).forEach(function (key) {
                 self.alias(key, x[key]);
             });
         }
-        else if (Array.isArray(y)) {
-            y.forEach(function (yy) {
-                self.alias(x, yy);
-            });
-        }
         else {
-            var zs = (aliases[x] || []).concat(aliases[y] || []).concat(x, y);
-            aliases[x] = zs.filter(function (z) { return z != x });
-            aliases[y] = zs.filter(function (z) { return z != y });
+            options.alias[x] = (options.alias[x] || []).concat(y);
         }
-        
         return self;
     };
     
@@ -128,20 +123,6 @@ function Argv (args, cwd) {
         return self;
     };
     
-    var defaults = {};
-    self.default = function (key, value) {
-        if (typeof key === 'object') {
-            Object.keys(key).forEach(function (k) {
-                self.default(k, key[k]);
-            });
-        }
-        else {
-            defaults[key] = value;
-        }
-        
-        return self;
-    };
-    
     var descriptions = {};
     self.describe = function (key, desc) {
         if (typeof key === 'object') {
@@ -155,9 +136,8 @@ function Argv (args, cwd) {
         return self;
     };
     
-    self.parse = function (args_) {
-        args = args_;
-        return self.argv;
+    self.parse = function (args) {
+        return parseArgs(args);
     };
     
     self.option = self.options = function (key, opt) {
@@ -204,7 +184,7 @@ function Argv (args, cwd) {
         var keys = Object.keys(
             Object.keys(descriptions)
             .concat(Object.keys(demanded))
-            .concat(Object.keys(defaults))
+            .concat(Object.keys(options.default))
             .reduce(function (acc, key) {
                 if (key !== '_') acc[key] = true;
                 return acc;
@@ -218,7 +198,7 @@ function Argv (args, cwd) {
         }
         
         var switches = keys.reduce(function (acc, key) {
-            acc[key] = [ key ].concat(aliases[key] || [])
+            acc[key] = [ key ].concat(options.alias[key] || [])
                 .map(function (sw) {
                     return (sw.length > 1 ? '--' : '-') + sw
                 })
@@ -255,8 +235,8 @@ function Argv (args, cwd) {
             
             var type = null;
             
-            if (flags.bools[key]) type = '[boolean]';
-            if (flags.strings[key]) type = '[string]';
+            if (options.boolean[key]) type = '[boolean]';
+            if (options.string[key]) type = '[string]';
             
             if (!wrap && dpadding.length > 0) {
                 desc += dpadding;
@@ -269,8 +249,8 @@ function Argv (args, cwd) {
                     ? '[required]'
                     : null
                 ,
-                defaults[key] !== undefined
-                    ? '[default: ' + JSON.stringify(defaults[key]) + ']'
+                options.default[key] !== undefined
+                    ? '[default: ' + JSON.stringify(options.default[key]) + ']'
                     : null
                 ,
             ].filter(Boolean).join('  ');
@@ -299,123 +279,13 @@ function Argv (args, cwd) {
     };
     
     Object.defineProperty(self, 'argv', {
-        get : parseArgs,
+        get : function () { return parseArgs(processArgs) },
         enumerable : true,
     });
     
-    function parseArgs () {
-        var argv = { _ : [], $0 : self.$0 };
-        Object.keys(flags.bools).forEach(function (key) {
-            setArg(key, defaults[key] || false);
-        });
-        
-        function setArg (key, val) {
-            var value = !flags.strings[key] && isNumber(val)
-                ? Number(val) : val
-            ;
-            setKey(argv, key.split('.'), value);
-            
-            (aliases[key] || []).forEach(function (x) {
-                argv[x] = argv[key];
-            });
-        }
-        
-        for (var i = 0; i < args.length; i++) {
-            var arg = args[i];
-            
-            if (arg === '--') {
-                argv._.push.apply(argv._, args.slice(i + 1));
-                break;
-            }
-            else if (arg.match(/^--.+=/)) {
-                // Using [\s\S] instead of . because js doesn't support the
-                // 'dotall' regex modifier. See:
-                // http://stackoverflow.com/a/1068308/13216
-                var m = arg.match(/^--([^=]+)=([\s\S]*)$/);
-                setArg(m[1], m[2]);
-            }
-            else if (arg.match(/^--no-.+/)) {
-                var key = arg.match(/^--no-(.+)/)[1];
-                setArg(key, false);
-            }
-            else if (arg.match(/^--.+/)) {
-                var key = arg.match(/^--(.+)/)[1];
-                var next = args[i + 1];
-                if (next !== undefined && !next.match(/^-/)
-                && !flags.bools[key]
-                && (aliases[key] ? !flags.bools[aliases[key]] : true)) {
-                    setArg(key, next);
-                    i++;
-                }
-                else if (/^(true|false)$/.test(next)) {
-                    setArg(key, next === 'true');
-                    i++;
-                }
-                else {
-                    setArg(key, true);
-                }
-            }
-            else if (arg.match(/^-[^-]+/)) {
-                var letters = arg.slice(1,-1).split('');
-                
-                var broken = false;
-                for (var j = 0; j < letters.length; j++) {
-                    var next = arg.slice(j+2);
-                    
-                    if (next === '-') {
-                        setArg(letters[j], next)
-                        continue;
-                    }
-                    
-                    if (/[A-Za-z]/.test(letters[j])
-                    && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
-                        setArg(letters[j], next);
-                        break;
-                    }
-                    
-                    if (letters[j+1] && letters[j+1].match(/\W/)) {
-                        setArg(letters[j], arg.slice(j+2));
-                        broken = true;
-                        break;
-                    }
-                    else {
-                        setArg(letters[j], true);
-                    }
-                }
-                
-                var key = arg.slice(-1)[0];
-                if (!broken && key !== '-') {
-                    
-                    if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1])
-                    && !flags.bools[key]
-                    && (aliases[key] ? !flags.bools[aliases[key]] : true)) {
-                        setArg(key, args[i+1]);
-                        i++;
-                    }
-                    else if (args[i+1] && /true|false/.test(args[i+1])) {
-                        setArg(key, args[i+1] === 'true');
-                        i++;
-                    }
-                    else {
-                        setArg(key, true);
-                    }
-                }
-            }
-            else {
-                argv._.push(
-                    flags.strings['_'] || !isNumber(arg) ? arg : Number(arg)
-                );
-            }
-        }
-        
-        Object.keys(defaults).forEach(function (key) {
-            if (!(key in argv)) {
-                argv[key] = defaults[key];
-                if (key in aliases) {
-                    argv[aliases[key]] = defaults[key];
-                }
-            }
-        });
+    function parseArgs (args) {
+        var argv = minimist(args, options);
+        argv.$0 = self.$0;
         
         if (demanded._ && argv._.length < demanded._) {
             fail('Not enough non-option arguments: got '
@@ -471,28 +341,3 @@ function rebase (base, dir) {
     ).replace(/\/$/,'').replace(/^$/, '.');
     return p.match(/^[.\/]/) ? p : './' + p;
 };
-
-function setKey (obj, keys, value) {
-    var o = obj;
-    keys.slice(0,-1).forEach(function (key) {
-        if (o[key] === undefined) o[key] = {};
-        o = o[key];
-    });
-    
-    var key = keys[keys.length - 1];
-    if (o[key] === undefined || typeof o[key] === 'boolean') {
-        o[key] = value;
-    }
-    else if (Array.isArray(o[key])) {
-        o[key].push(value);
-    }
-    else {
-        o[key] = [ o[key], value ];
-    }
-}
-
-function isNumber (x) {
-    if (typeof x === 'number') return true;
-    if (/^0x[0-9a-f]+$/i.test(x)) return true;
-    return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
-}
diff --git a/package.json b/package.json
index 2a370fe..f8852b2 100644
--- a/package.json
+++ b/package.json
@@ -1,10 +1,11 @@
 {
     "name" : "optimist",
-    "version" : "0.5.2",
+    "version" : "0.6.0",
     "description" : "Light-weight option parsing with an argv hash. No optstrings attached.",
     "main" : "./index.js",
     "dependencies" : {
-        "wordwrap" : "~0.0.2"
+        "wordwrap" : "~0.0.2",
+        "minimist" : "~0.0.1"
     },
     "devDependencies" : {
         "hashish": "~0.0.4",

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/collab-maint/node-optimist.git



More information about the Pkg-javascript-commits mailing list