/**
 * Copyright (c) 2012-2014 Axelor. All Rights Reserved.
 *
 * The contents of this file are subject to the Common Public
 * Attribution License Version 1.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://license.axelor.com/.
 *
 * The License is based on the Mozilla Public License Version 1.1 but
 * Sections 14 and 15 have been added to cover use of software over a
 * computer network and provide for limited attribution for the
 * Original Developer. In addition, Exhibit A has been modified to be
 * consistent with Exhibit B.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is part of "Axelor Business Suite", developed by
 * Axelor exclusively.
 *
 * The Original Developer is the Initial Developer. The Initial Developer of
 * the Original Code is Axelor.
 *
 * All portions of the code written by Axelor are
 * Copyright (c) 2012-2014 Axelor. All Rights Reserved.
 */
package com.axelor.cli;

import java.util.Properties;

import com.axelor.auth.AuthModule;
import com.axelor.db.JpaModule;
import com.axelor.meta.loader.ModuleManager;
import com.axelor.rpc.ObjectMapperProvider;
import com.beust.jcommander.JCommander;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;

public class Main {
	
	static class MainModule extends AbstractModule {
		
		protected Properties properties = new Properties();
		
		private String jpaUnit;

		public MainModule(String jpaUnit) {
			this.jpaUnit = jpaUnit;
		}
		
		@Override
		protected void configure() {
			
			bind(ObjectMapper.class).toProvider(ObjectMapperProvider.class);

			install(new JpaModule(jpaUnit).properties(properties));
			install(new AuthModule.Simple().properties(properties));
		}
	}
	
	public static void main(String[] args) {
		
		Options opts = new Options();
		JCommander cmd = new JCommander(opts);

		cmd.setProgramName("axelor");
		
		try {
			cmd.parse(args);
		} catch (Exception e) {
			System.err.println(e.getMessage());
			cmd.usage();
			System.exit(-1);
			return;
		}

		if ((opts.showHelp == Boolean.TRUE)
				|| (opts.init == Boolean.FALSE && opts.update == Boolean.FALSE)) {
			cmd.usage();
			System.exit(0);
			return;
		}
		
		Injector injector = Guice.createInjector(new MainModule(opts.unit));
		ModuleManager manager = injector.getInstance(ModuleManager.class);
		
		if (opts.init == Boolean.TRUE) {
			manager.initialize(opts.update == Boolean.TRUE, opts.importDemo == Boolean.TRUE);
			System.exit(0);
			return;
		}

		String[] names = {};
		if (opts.modules != null) {
			names = opts.modules.toArray(new String[] {});
		}
		manager.update(opts.importDemo == Boolean.TRUE, names);
		System.exit(0);
	}
}

@@ -1617,17 +1617,71 @@
  Main %7B%0A
-%09
+%0A%09private static final String PROGRAM_NAME = %22axelor%22;%0A
 %0A%09static
@@ -1723,19 +1723,17 @@
 odule %7B%0A
-%09%09
 %0A
+
 %09%09protec
@@ -1778,19 +1778,17 @@
 ties();%0A
-%09%09
 %0A
+
 %09%09privat
@@ -1875,19 +1875,17 @@
 it;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09@Overr
@@ -1923,12 +1923,9 @@
 ) %7B%0A
-%09%09%09
 %0A
+
 %09%09%09b
@@ -2115,18 +2115,17 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
 %0A
+
 %09public 
@@ -2160,16 +2160,96 @@
 gs) %7B%0A%09%09
+System.exit(process(args));%0A%09%7D%0A%0A%09public static Integer process(String%5B%5D args) %7B%0A
 %0A%09%09Optio
@@ -2340,21 +2340,23 @@
 ame(
-%22axelor%22
+PROGRAM_NAME
 );%0A
-%09%09
 %0A%09%09t
@@ -2465,28 +2465,8 @@
 ();%0A
-%09%09%09System.exit(-1);%0A
 %09%09%09r
@@ -2462,32 +2462,35 @@
 age();%0A%09%09%09return
+ -1
 ;%0A%09%09%7D%0A%0A%09%09if ((op
@@ -2605,35 +2605,16 @@
 sage();%0A
-%09%09%09System.exit(0);%0A
 %09%09%09retur
@@ -2618,17 +2618,17 @@
 turn
+ 0
 ;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09In
@@ -2767,11 +2767,9 @@
 s);%0A
-%09%09
 %0A
+
 %09%09if
@@ -2888,27 +2888,8 @@
 E);%0A
-%09%09%09System.exit(0);%0A
 %09%09%09r
@@ -2893,16 +2893,18 @@
 %09%09return
+ 0
 ;%0A%09%09%7D%0A%0A%09
@@ -3073,22 +3073,16 @@
 ;%0A%09%09
-System.exit(0)
+return 0
 ;%0A%09%7D

Refactor command line class.