import modules from './modules/';
import MupAPI from './mup-api';
import checkUpdates from './updates';
import program from 'commander';

let settingsPath;
let configPath;
const args = process.argv.slice(2);

program
  .arguments('<arg> [subarg]')
  .action(argAction)
  .option('--settings <filePath>', 'Meteor settings file', setSettingsPath)
  .option('--config <filePath>', 'mup.js config file', setConfigPath)
  .parse(process.argv);

function argAction(arg, subarg) {

  let moduleArg = arg;
  let command = subarg;

  if(!command && !modules[moduleArg]) {
    command = moduleArg;
    moduleArg = 'default';
  }

  let module;

  if(modules[moduleArg]) {
    module = modules[moduleArg];
  } else {
    console.error('No such module');
  }

  if(!command) {
    module.help(args);
    process.exit(0);
  }

  if (!module[command]) {
    console.error('error: unknown command %s', command);
    module.help(args);
    process.exit(1);
  }

  if(program.settings) {
    args.splice(0, 2);
  }

  if(program.config) {
    args.splice(0, 2);
  }

  checkUpdates().then(() => {
    const base = process.cwd();
    const api = new MupAPI(base, args, configPath, settingsPath);
    module[command](api);
  });
}

function handleErrors(e) {
  console.log(e.name, e.message);
  process.exit(1);
}

function setSettingsPath(settingsPathArg) {
  settingsPath = settingsPathArg;
}

function setConfigPath(configPathArg) {
  configPath = configPathArg;
}

process.on('uncaughtException', handleErrors);

@@ -229,20 +229,28 @@
 s('%3C
-arg%3E %5Bsubarg
+command%3E %5Bsubcommand
 %5D')%0A
@@ -423,28 +423,588 @@
 %0A  .
-parse(process.argv);
+on('--help', function()%7B%0A    console.log('   Commands:');%0A%0A    function listModuleCommands(commands) %7B%0A      Object.keys(commands).forEach((command) =%3E %7B%0A        if (command === 'default') %7B%0A          listModuleCommands(commands%5B'default'%5D);%0A          return;%0A        %7D%0A        console.log(%60     $%7Bcommand%7D%60);%0A      %7D)%0A    %7D%0A%0A    listModuleCommands(modules)%0A  %0A    console.log('');%0A    console.log('    For list of subcommands, run ')%0A    console.log('      mup %3Ccommand%3E help')%0A  %7D)%0A  .parse(process.argv);%0A%0Aif (program.args.length === 0) %7B%0A  program.help();%0A  process.exit(0);%0A%7D
 %0A%0Afu
@@ -1035,17 +1035,16 @@
 barg) %7B%0A
-%0A
   let mo
@@ -1180,16 +1180,116 @@
 ';%0A  %7D%0A%0A
+  if (moduleArg === 'default' && command === 'help') %7B%0A    program.help();%0A    process.exit();%0A  %7D%0A%0A
   let mo
@@ -1409,25 +1409,136 @@
 ;%0A  
-%7D%0A%0A  if(!command)
+  program.help();%0A    process.exit(1);%0A  %7D%0A%0A  if(!command) %7B%0A    if (moduleArg === 'default') %7B%0A      program.help();%0A    %7D else
  %7B%0A 
@@ -1551,32 +1551,38 @@
 ule.help(args);%0A
+    %7D%0A
     process.exit
@@ -1674,16 +1674,103 @@
 mmand);%0A
+    if (moduleArg === 'default') %7B%0A      program.help();%0A      process.exit(1);%0A    %7D%0A%0A
     modu

Improve help