#!/usr/bin/env node

/*jslint node: true */
"use strict";

var fs = require('fs');
var path = require('path');
var transform = require('../lib/comments.js');
var styles = Object.keys(transform.STYLES);
var listify = require('listify');

var opt = require('optimist')
	.usage('Usage: $0 --style [style] [file1.js] [file2.js] … [fileN.js]')
	.describe('style', 'Specify a style. Currently supported: ' + listify(styles))
	.demand('style')
	.check(function (opt) {
		if (styles.indexOf(opt.style) === -1) {
			throw new SyntaxError('--style must be one of: ' + listify(styles));
		} else if (!Array.isArray(opt._) || opt._.length === 0) {
			throw new ReferenceError('please specify one or more files');
		} else {
			var nonexistent = opt._.filter(function (filename) {
				return !fs.existsSync(filename);
			});
			if (nonexistent.length > 0) {
				var message = nonexistent.map(function (filename) {
					return opt.$0 + ': ' + filename + ': no such file or directory';
				}).join('\n');
				throw new ReferenceError('\n' + message + '\n');
			}
		}
	})
	.argv;

var files = opt._;

if (opt.help) {
	opt.showHelp();
} else {
	var filenames = files.map(function (filename) {
		return path.resolve(String(filename));
	});
	filenames.forEach(function (filename) {
		var code = fs.readFileSync(filename).toString();
		transform(code, transform.STYLES[opt.style], function (err, transformed) {
			console.log(transformed);
		});
	});
}


@@ -430,16 +430,109 @@
 style')%0A
+%09.describe('save', 'Save the transformed code back to the original file.')%0A%09.boolean('save')%0A
 %09.check(
@@ -1477,16 +1477,200 @@
 rmed) %7B%0A
+%09%09%09if (opt.save) %7B%0A%09%09%09%09fs.writeFile(filename, transformed, function (err) %7B%0A%09%09%09%09%09if (err) %7B throw err; %7D%0A%09%09%09%09%09console.log(filename + ' transformed successfully');%0A%09%09%09%09%7D);%0A%09%09%09%7D else %7B%0A%09
 %09%09%09conso
@@ -1690,16 +1690,21 @@
 ormed);%0A
+%09%09%09%7D%0A
 %09%09%7D);%0A%09%7D

Add support for the "save" option.