From 9e1179c980c7be15e7cc0c2c9f943a5477323b2f Mon Sep 17 00:00:00 2001 From: Alstruit <34786806+Alstruit@users.noreply.github.com> Date: Wed, 13 Nov 2024 04:54:30 -0600 Subject: [PATCH] Fix System.ArgumentException System.ArgumentException occurs when handling space-separated arguments provided as a single string. GetParameterList() function attempts to parse the string of numbers as a UInt32. --- DepotDownloader/Program.cs | 51 +++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 6 deletions(-) mode change 100644 => 100755 DepotDownloader/Program.cs diff --git a/DepotDownloader/Program.cs b/DepotDownloader/Program.cs old mode 100644 new mode 100755 index ea65385b..e727a59c --- a/DepotDownloader/Program.cs +++ b/DepotDownloader/Program.cs @@ -367,21 +367,60 @@ namespace DepotDownloader var list = new List(); var index = IndexOfParam(args, param); - if (index == -1 || index == (args.Length - 1)) + // Ensure the parameter was found and there is at least one value after it + if (index == -1 || index >= args.Length - 1) return list; index++; + var converter = TypeDescriptor.GetConverter(typeof(T)); + if (converter == null) + { + Console.WriteLine($"Warning: No type converter available for type {typeof(T)}"); + return list; + } + + var strParam = args[index]; + + // Handle the scenario where we have a single space-separated string of values + if (strParam.Contains(" ") && !strParam.StartsWith("-")) + { + // Directly split and convert all elements into a list of T + try + { + list = strParam.Split(' ', StringSplitOptions.RemoveEmptyEntries) + .Select(val => (T)converter.ConvertFromString(val)) + .Where(convertedVal => convertedVal != null) + .ToList(); + } + catch (Exception ex) + { + Console.WriteLine($"Warning: Unable to convert values from '{strParam}' to type {typeof(T)}. Exception: {ex.Message}"); + } + + return list; + } + + // Handle each value provided as an individual argument while (index < args.Length) { - var strParam = args[index]; + strParam = args[index]; - if (strParam[0] == '-') break; + // Stop parsing if a new parameter starts + if (strParam.StartsWith("-")) + break; - var converter = TypeDescriptor.GetConverter(typeof(T)); - if (converter != null) + try + { + var convertedValue = converter.ConvertFromString(strParam); + if (convertedValue != null) + { + list.Add((T)convertedValue); + } + } + catch (Exception ex) { - list.Add((T)converter.ConvertFromString(strParam)); + Console.WriteLine($"Warning: Unable to convert value '{strParam}' to type {typeof(T)}. Exception: {ex.Message}"); } index++;