/*
 * Copyright 2015-2017 OpenCB
 *
 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.opencb.opencga.app.cli.main;

import com.beust.jcommander.ParameterException;
import org.apache.commons.lang3.StringUtils;
import org.opencb.opencga.app.cli.CommandExecutor;
import org.opencb.opencga.app.cli.main.executors.analysis.AlignmentCommandExecutor;
import org.opencb.opencga.app.cli.main.executors.analysis.VariantCommandExecutor;
import org.opencb.opencga.app.cli.main.executors.catalog.*;
import org.opencb.opencga.core.common.ArrayUtils;
import org.opencb.opencga.core.common.GitRepositoryState;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by imedina on 27/05/16.
 */
public class OpencgaMain {

    public static final String VERSION = GitRepositoryState.get().getBuildVersion();

    public static void main(String[] args) {

        if ("users".equals(args[0]) && "login".equals(args[1])) {
            List<String> argsList = new ArrayList<>();
            argsList.addAll(Arrays.asList(args));
            argsList.add("--password");
            args = ArrayUtils.toStringArray(argsList);
        }

        OpencgaCliOptionsParser cliOptionsParser = new OpencgaCliOptionsParser();
        try {
            cliOptionsParser.parse(args);
        } catch (ParameterException e) {
            System.err.println(e.getMessage());
            cliOptionsParser.printUsage();
            System.exit(1);
        }

        String parsedCommand = cliOptionsParser.getCommand();
        if (parsedCommand == null || parsedCommand.isEmpty()) {
            if (cliOptionsParser.getGeneralOptions().version) {
                System.out.println("Version " + GitRepositoryState.get().getBuildVersion());
                System.out.println("Git version: " + GitRepositoryState.get().getBranch() + " " + GitRepositoryState.get().getCommitId());
                System.exit(0);
            } else if (cliOptionsParser.getGeneralOptions().help) {
                cliOptionsParser.printUsage();
                System.exit(0);
            } else {
                cliOptionsParser.printUsage();
                System.exit(1);
            }
        } else {
            CommandExecutor commandExecutor = null;
            // Check if any command -h option is present
            if (cliOptionsParser.isHelp()) {
                cliOptionsParser.printUsage();
                System.exit(0);
            } else {
                String parsedSubCommand = cliOptionsParser.getSubCommand();
                if (parsedSubCommand == null || parsedSubCommand.isEmpty()) {
                    cliOptionsParser.printUsage();
                } else {
                    switch (parsedCommand) {
                        case "users":
                            commandExecutor = new UserCommandExecutor(cliOptionsParser.getUsersCommandOptions());
                            break;
                        case "projects":
                            commandExecutor = new ProjectCommandExecutor(cliOptionsParser.getProjectCommandOptions());
                            break;
                        case "studies":
                            commandExecutor = new StudyCommandExecutor(cliOptionsParser.getStudyCommandOptions());
                            break;
                        case "files":
                            commandExecutor = new FileCommandExecutor(cliOptionsParser.getFileCommands());
                            break;
                        case "jobs":
                            commandExecutor = new JobCommandExecutor(cliOptionsParser.getJobsCommands());
                            break;
                        case "individuals":
                            commandExecutor = new IndividualCommandExecutor(cliOptionsParser.getIndividualsCommands());
                            break;
                        case "samples":
                            commandExecutor = new SampleCommandExecutor(cliOptionsParser.getSampleCommands());
                            break;
                        case "cohorts":
                            commandExecutor = new CohortCommandExecutor(cliOptionsParser.getCohortCommands());
                            break;
                        case "panels":
                            commandExecutor = new PanelCommandExecutor(cliOptionsParser.getPanelCommands());
                            break;
                        case "families":
                            commandExecutor = new FamilyCommandExecutor(cliOptionsParser.getFamilyCommands());
                            break;
                        case "tools":
                            commandExecutor = new ToolCommandExecutor(cliOptionsParser.getToolCommands());
                            break;
                        case "variables":
                            commandExecutor = new VariableCommandExecutor(cliOptionsParser.getVariableCommands());
                            break;
                        case "alignments":
                            commandExecutor = new AlignmentCommandExecutor(cliOptionsParser.getAlignmentCommands());
                            break;
                        case "variant":
                            commandExecutor = new VariantCommandExecutor(cliOptionsParser.getVariantCommands());
                            break;
                        default:
                            System.out.printf("ERROR: not valid command passed: '" + parsedCommand + "'");
                            break;
                    }

                    if (commandExecutor != null) {
                        try {
                            commandExecutor.execute();
                        } catch (Exception e) {
                            e.printStackTrace();
                            System.exit(1);
                        }
                    } else {
                        cliOptionsParser.printUsage();
                        System.exit(1);
                    }
                }
            }
        }
    }

}
@@ -711,22 +711,21 @@
 s.lang3.
-String
+Array
 Utils;%0Ai
@@ -1004,58 +1004,8 @@
 .*;%0A
-import org.opencb.opencga.core.common.ArrayUtils;%0A
 impo
@@ -1063,85 +1063,8 @@
 e;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0A
 /**%0A
@@ -1274,16 +1274,35 @@
     if (
+args.length %3E 1 && 
 %22users%22.
@@ -1363,193 +1363,51 @@
     
-List%3CString%3E argsList = new ArrayList%3C%3E();%0A            argsList.addAll(Arrays.asList(args));%0A            argsList.add(%22--password%22);%0A            args = ArrayUtils.toStringArray(argsList
+args = ArrayUtils.addAll(args, %22--password%22
 );%0A 

use java commons lang to add parameter to args. Add a check in the if. Clean unused imports