Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CLI Argument Parsing to Handle Space Separated Values #581

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions DepotDownloader/Program.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -367,21 +367,60 @@ static List<T> GetParameterList<T>(string[] args, string param)
var list = new List<T>();
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("-"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If args is already split by space, how would this ever hit?

{
// 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++;
Expand Down
Loading