Skip to content

Commit

Permalink
Add better options parsing
Browse files Browse the repository at this point in the history
- Use commander.js
- Support cleaver --help
- Support cleaver --version
- Execute `cleaver` with no arguments will show help message
  • Loading branch information
Kuang-Che Liu (kcliu) authored and jdan committed Dec 15, 2013
1 parent d77c5ef commit 4492d16
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
35 changes: 25 additions & 10 deletions bin/cleaver
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,33 @@
var path = require('path');
var fs = require('fs');
var Cleaver = require(path.join(__dirname,'../lib/cleaver.js'));
var program = require('commander');

if (process.argv[2] === 'watch') {
var file = process.argv[3];
new Cleaver(file).run();
program
.version(require('../package.json').version)
.usage('[command] [file]');

console.log('Watching for changes on ' + process.argv[3] + '. Ctrl-C to abort.');
fs.watchFile(file, { persistent: true, interval: 100 }, function () {
console.log('Rebuilding: ' + new Date());
new Cleaver(file).run();
});
program
.command('watch')
.description('Watch for changes on markdown file')
.action(function(){
file = program.args[0];
new Cleaver(file).run();

console.log('Watching for changes on ' + file + '. Ctrl-C to abort.');
fs.watchFile(file, { persistent: true, interval: 100 }, function () {
console.log('Rebuilding: ' + new Date());
new Cleaver(file).run();
});
});

program.parse(process.argv);

if (!program.args.length) {
program.help();
} else {
var cleaver = new Cleaver(process.argv[2]);
cleaver.run();
var file = program.args[0];
var cleaver = new Cleaver(file);
cleaver.run();
}

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"q": "0.9.6",
"js-yaml": "2.1.0",
"highlight.js": "~7.3.0",
"marked": "~0.2.9"
"marked": "~0.2.9",
"commander": "~2.1.0"
},
"devDependencies": {
"jshint": "~2.3.0"
Expand Down

0 comments on commit 4492d16

Please sign in to comment.