From 2b688ac5057811454f350bc1642eb6d5446ceccf Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 9 Jan 2013 20:45:47 -0600 Subject: [PATCH] improve option parsing in bin/marked. --- bin/marked | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/bin/marked b/bin/marked index ccc58c55..90a0fa9b 100755 --- a/bin/marked +++ b/bin/marked @@ -38,7 +38,8 @@ function main(argv, callback) { , input , output , arg - , tokens; + , tokens + , opt; function getarg() { var arg = argv.shift(); @@ -88,12 +89,18 @@ function main(argv, callback) { return help(); default: if (arg.indexOf('--') === 0) { - arg = arg.substring(2); - if (arg.indexOf('no-') === 0) { - arg = arg.substring(3); - options[arg] = false; + opt = camelize(arg.replace(/^--(no-)?/, '')); + if (!marked.defaults.hasOwnProperty(opt)) { + continue; + } + if (arg.indexOf('--no-') === 0) { + options[opt] = typeof marked.defaults[opt] !== 'boolean' + ? null + : false; } else { - options[arg] = true; + options[opt] = typeof marked.defaults[opt] !== 'boolean' + ? argv.shift() + : true; } } else { files.push(arg); @@ -157,6 +164,12 @@ function getStdin(callback) { } } +function camelize(text) { + return text.replace(/(\w)-(\w)/g, function(_, a, b) { + return a + b.toUpperCase(); + }); +} + /** * Expose / Entry Point */