improve option parsing in bin/marked.

This commit is contained in:
Christopher Jeffrey 2013-01-09 20:45:47 -06:00
parent 5a1c7a5871
commit 2b688ac505

View File

@ -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
*/