package utils;

import org.apache.commons.cli.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import orwell.tank.config.RobotColourConfigIniFile;
import orwell.tank.config.RobotIniFile;

import java.io.IOException;
import java.util.Arrays;

public class Cli {
    private final static Logger logback = LoggerFactory.getLogger(Cli.class);
    private final Options options = new Options();
    private String[] args = null;

    public Cli(final String[] args) {
        this.args = args;

        final Option optionHelp = new Option("h", "help", false, "shows help and exits program.");
        options.addOption(optionHelp);

        // Add a new optionGroup to make --file
        final Option optionRobotIniFile = new Option("rf", "robotfile", true, "filepath for external robot configuration file");
        final OptionGroup optionGroup = new OptionGroup();
        optionGroup.setRequired(false);
        optionGroup.addOption(optionRobotIniFile);
        options.addOptionGroup(optionGroup);

        final Option optionColourIniFile = new Option("cf", "colourfile", true, "filepath for external colour configuration file");
        final OptionGroup optionGroup2 = new OptionGroup();
        optionGroup2.setRequired(true);
        optionGroup2.addOption(optionColourIniFile);
        options.addOptionGroup(optionGroup2);
    }

    /**
     * Extract the orwell.tank.config parameters used later by the ConfigFactory
     *
     * @return null if help is called or error happens during parsing
     */
    public IniFiles parse() {
        final CommandLineParser parser = new DefaultParser();
        final CommandLine cmd;

        try {
            cmd = parser.parse(options, args);

            if (cmd.hasOption("h"))
                return help();

            String robotIniFileName = "";
            if (cmd.hasOption("rf")) {
                robotIniFileName = cmd.getOptionValue("rf");
            }

            String colourConfigIniFileName = "";
            if (cmd.hasOption("cf")) {
                colourConfigIniFileName = cmd.getOptionValue("cf");
            }

            IniFiles iniFiles = new IniFiles(robotIniFileName, colourConfigIniFileName);

            if (!iniFiles.isEmpty()) {
                return iniFiles;
            }

            if (cmd.hasOption("rf") && cmd.hasOption("cf")) {
                logback.warn(iniFiles.toString());
                return help();
            } else if (args.length > 0) {
                logback.warn("There is an unknown parameter among the following ones: " + Arrays.toString(args));
                return help();
            } else {
                return help();
            }

        } catch (final ParseException e) {
            logback.error("Failed to parse command line properties", e);
            return help();
        }
    }

    private IniFiles help() {
        // This prints out some help
        final HelpFormatter formatter = new HelpFormatter();

        formatter.printHelp("-f filepath", options);
        return null;
    }
}

@@ -339,24 +339,200 @@
 Cli.class);%0A
+    private static final String COLOUR_INI_FILE_OPTION = %22cf%22;%0A    private static final String ROBOT_INI_FILE_OPTION = %22rf%22;%0A    private static final String HELP_OPTION = %22h%22;%0A
     private 
@@ -715,19 +715,27 @@
  Option(
-%22h%22
+HELP_OPTION
 , %22help%22
@@ -918,20 +918,37 @@
  Option(
-%22rf%22
+ROBOT_INI_FILE_OPTION
 , %22robot
@@ -1261,20 +1261,38 @@
  Option(
-%22cf%22
+COLOUR_INI_FILE_OPTION
 , %22colou
@@ -1957,13 +1957,47 @@
 ion(
-%22h%22))
+HELP_OPTION)) %7B%0A                help();
 %0A   
@@ -2016,23 +2016,35 @@
  return 
-help();
+null;%0A            %7D
 %0A%0A      
@@ -2109,20 +2109,37 @@
 sOption(
-%22rf%22
+ROBOT_INI_FILE_OPTION
 )) %7B%0A   
@@ -2189,20 +2189,37 @@
 onValue(
-%22rf%22
+ROBOT_INI_FILE_OPTION
 );%0A     
@@ -2299,36 +2299,54 @@
  (cmd.hasOption(
-%22cf%22
+COLOUR_INI_FILE_OPTION
 )) %7B%0A           
@@ -2395,20 +2395,38 @@
 onValue(
-%22cf%22
+COLOUR_INI_FILE_OPTION
 );%0A     
@@ -2646,35 +2646,70 @@
 ion(
-%22rf%22) && cmd.hasOption(%22cf%22
+ROBOT_INI_FILE_OPTION) && cmd.hasOption(COLOUR_INI_FILE_OPTION
 )) %7B
@@ -2764,39 +2764,8 @@
 ));%0A
-                return help();%0A
     
@@ -2920,91 +2920,8 @@
 ));%0A
-                return help();%0A            %7D else %7B%0A                return help();%0A
     
@@ -3055,26 +3055,26 @@
         
+%7D%0A%0A
     
-return
+   
  help();
@@ -3082,17 +3082,28 @@
         
-%7D
+return null;
 %0A    %7D%0A%0A
@@ -3114,24 +3114,20 @@
 private 
-IniFiles
+void
  help() 
@@ -3280,37 +3280,16 @@
 tions);%0A
-        return null;%0A
     %7D%0A%7D%0A

Simplify Cli.java