package com.bishnet.cucumber.parallel.cli;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import com.bishnet.cucumber.parallel.runtime.RuntimeConfiguration;

public class ArgumentsParser {

	private List<String> arguments;
	
	public ArgumentsParser(List<String> arguments) {
		this.arguments = arguments;
	}
	
	public RuntimeConfiguration parse() {
		List<String> cucumberArgs = new ArrayList<String>();
		List<String> featurePaths = new ArrayList<String>();
		RuntimeConfiguration runtimeConfiguration = new RuntimeConfiguration();
		runtimeConfiguration.numThreads = Runtime.getRuntime().availableProcessors();
		while (!arguments.isEmpty()) {
			String arg = arguments.remove(0).trim();
			
			if (arg.equals("--num-threads")) {
				runtimeConfiguration.numThreads = Integer.parseInt(arguments.remove(0));
			} else if (arg.equals("--plugin") || arg.equals("-p") || arg.equals("--format") || arg.equals("-f")) {
				String pluginValue = arguments.remove(0);
				String[] pluginArgsArray = pluginValue.split(":");
				if (pluginArgsArray[0].equals("json")) {
					runtimeConfiguration.jsonReportRequired = true;
					runtimeConfiguration.jsonReportPath = Paths.get(pluginArgsArray[1]);
				} else if (pluginArgsArray[0].equals("html")) {
					runtimeConfiguration.htmlReportRequired = true;
					runtimeConfiguration.htmlReportPath = Paths.get(pluginArgsArray[1]);
				} else {
					cucumberArgs.add(arg);
					cucumberArgs.add(pluginValue);
				}
			} else if (arg.equals("--glue") || arg.equals("-g")) {
				cucumberArgs.add(arg);
				cucumberArgs.add(arguments.remove(0));
			} else if (arg.equals("--tags") || arg.equals("-t")) {
				cucumberArgs.add(arg);
				cucumberArgs.add(arguments.remove(0));
			} else if (arg.equals("--i18n")) {
				cucumberArgs.add(arg);
				cucumberArgs.add(arguments.remove(0));
			} else if (arg.equals("--snippets")) {
				cucumberArgs.add(arg);
				cucumberArgs.add(arguments.remove(0));
			} else if (arg.equals("--name") || arg.equals("-n")) {
				cucumberArgs.add(arg);
				cucumberArgs.add(arguments.remove(0));
			} else if (arg.startsWith("-")) {
				cucumberArgs.add(arg);
			}
			else {
				featurePaths.add(arg);
			}
		}
		runtimeConfiguration.cucumberArgs = cucumberArgs;
		runtimeConfiguration.featurePaths = featurePaths;
		return runtimeConfiguration;
	}
}

@@ -640,16 +640,82 @@
 sors();%0A
+%09%09List%3CString%3E parseArguments = new ArrayList%3CString%3E(arguments);%0A
 %09%09while 
@@ -716,17 +716,22 @@
 while (!
-a
+parseA
 rguments
@@ -760,17 +760,22 @@
 g arg = 
-a
+parseA
 rguments
@@ -890,17 +890,22 @@
 arseInt(
-a
+parseA
 rguments
@@ -1048,17 +1048,22 @@
 Value = 
-a
+parseA
 rguments
@@ -1661,33 +1661,38 @@
 ucumberArgs.add(
-a
+parseA
 rguments.remove(
@@ -1794,33 +1794,38 @@
 ucumberArgs.add(
-a
+parseA
 rguments.remove(
@@ -1907,33 +1907,38 @@
 ucumberArgs.add(
-a
+parseA
 rguments.remove(
@@ -2024,33 +2024,38 @@
 ucumberArgs.add(
-a
+parseA
 rguments.remove(
@@ -2165,17 +2165,22 @@
 rgs.add(
-a
+parseA
 rguments

Make ArgumentsParser.parse() idempotent.