var optimist = require('optimist');
var util = require('./util');
var constant = require('./constants');

var sharedConfig = function() {
  optimist
    .describe('port-runner', 'Port where the server is listening for runner.')
    .describe('help', 'Print usage.')
    .describe('version', 'Print current version.');
};


var processOptions = function(argv) {
  argv = optimist.parse(argv || process.argv);

  if (argv.help) {
    optimist.showHelp();
    process.exit(0);
  }

  if (argv.version) {
    console.log('Testacular version: ' + constant.VERSION);
    process.exit(0);
  }

  // TODO(vojta): warn/throw when unknown argument (probably mispelled)
  var options = {};
  Object.getOwnPropertyNames(argv).forEach(function(name) {
    if (name === '_' || name === '$0') return;
    options[util.dashToCamel(name)] = argv[name];
  });

  if (util.isString(options.autoWatch)) {
    options.autoWatch = options.autoWatch === 'true';
  }

  if (util.isString(options.logColors)) {
    options.logColors = options.logColors === 'true';
  }

  if (util.isString(options.logLevel)) {
    options.logLevel = constant['LOG_' + options.logLevel.toUpperCase()] || constant.LOG_DISABLE;
  }

  options.configFile = argv._[2] || 'testacular.conf';

  return options;
}


exports.runner = function() {
  optimist.usage('Execute - send execute command to Testacular server.\nUsage: $0 [options]');
  sharedConfig();

  return processOptions();
};


exports.server = function(argv) {
  optimist
    .usage('Start Testacular server.\nUsage: $0 [configFile] [options]')
    .describe('port', 'Port where the web server is running.')
    .describe('auto-watch', 'Auto watch source files and run on change ?')
    .describe('auto-watch-interval', 'Interval for OS that do polling, in ms.')
    .describe('log-level', 'What logs should be printed (disable | error | warn | info | debug).')
    .describe('log-colors', 'Should logs be colorful ?');

  sharedConfig();

  return processOptions(argv);
};

@@ -168,24 +168,34 @@
 t-runner', '
+%3Cinteger%3E 
 Port where t
@@ -431,24 +431,36 @@
 help) %7B%0A    
+console.log(
 optimist.sho
@@ -460,18 +460,15 @@
 ist.
-showH
+h
 elp()
+)
 ;%0A  
@@ -1596,16 +1596,26 @@
 port', '
+%3Cinteger%3E 
 Port whe
@@ -1716,18 +1716,78 @@
 n change
- ?
+.')%0A    .describe('no-auto-watch', 'Do not watch source files.
 ')%0A    .
@@ -1819,16 +1819,26 @@
 rval', '
+%3Cinteger%3E 
 Interval
@@ -1903,37 +1903,9 @@
 ', '
-What logs should be printed (
+%3C
 disa
@@ -1937,17 +1937,34 @@
  %7C debug
-)
+%3E Level of logging
 .')%0A    
@@ -1992,33 +1992,110 @@
 ', '
-Should logs be colorful ?
+Use colors when printing logs.')%0A    .describe('no-log-colors', 'Do not use colors when printing logs.
 ');%0A

Improve --help a bit