namespace CommandLine { public struct CLI { public static Dictionary ArgParser(string[] argsFromCLI, string[] validArguments) { List inputArgs = [.. argsFromCLI]; string[] args = inputArgs.Where(validArguments.Contains).ToArray(); Console.WriteLine($"Input args: [{string.Join(", ", inputArgs)}]"); Console.WriteLine($"args: [{string.Join(", ", args)}]"); List indexes = []; HashSet seenArgs = []; // Sanitise the arguments before processing them for (int i = 0; i < args.Length; i++) { string arg = args[i]; if (seenArgs.Contains(arg)) throw new Exception($"repeated argument '{arg}' found."); seenArgs.Add(arg); indexes.Add(inputArgs.IndexOf(arg)); } // Add the end of the list so we can get a substring // to the end of the string the same way we would with // the other values. indexes.Add(inputArgs.Count); Dictionary convertedArgs = []; for (int x = 0; x < indexes.Count - 1; x++) { // Take the indexes in pairs int firstIndex = indexes[x]; int secondIndex = indexes[x + 1]; string argument = inputArgs[firstIndex]; string supplied = string.Join(' ', inputArgs[(firstIndex + 1) .. secondIndex]); convertedArgs.Add(argument, supplied); } return convertedArgs; } } }