in src/Commands/GlobalCommand.cs [191:281]
protected async Task<bool> ProcessArguments(CommandContext context, TArgs args)
{
var arguments = GetArguments();
// Ensure we have arguments to process
if (arguments == null || !arguments.Any())
{
return true;
}
// First, add all arguments to the response and apply default values if needed
foreach (var argDef in arguments)
{
if (argDef is ArgumentBuilder<TArgs> typedArgDef)
{
// Get the current value and handle "null" string case
string value = typedArgDef.ValueAccessor(args) ?? string.Empty;
value = value.Equals("null", StringComparison.OrdinalIgnoreCase) ? string.Empty : value;
// Special handling for subscription when it's "default"
if (typedArgDef.Name.Equals("subscription", StringComparison.OrdinalIgnoreCase) &&
value.Equals("default", StringComparison.OrdinalIgnoreCase))
{
value = string.Empty;
// Update the args object if it's a subscription-based argument type
if (args is SubscriptionArguments baseArgs)
{
baseArgs.Subscription = string.Empty;
}
}
// If the value is empty but there's a default value, use the default value
if (string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(typedArgDef.DefaultValue))
{
// Try to set the default value on the args object using reflection
try
{
var prop = typeof(TArgs).GetProperty(typedArgDef.Name,
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.IgnoreCase);
if (prop != null && prop.CanWrite)
{
prop.SetValue(args, typedArgDef.DefaultValue);
value = typedArgDef.DefaultValue;
}
}
catch (Exception)
{
// Silently handle reflection errors
}
}
}
}
// Then, process required arguments that are missing values
bool allRequiredArgumentsProvided = true;
var missingArgs = new List<string>();
foreach (var argDef in arguments)
{
if (argDef is ArgumentBuilder<TArgs> typedArgDef && typedArgDef.Required)
{
// Get the current value
string value = typedArgDef.ValueAccessor(args) ?? string.Empty;
// If the value is missing and this is a required argument
if (string.IsNullOrEmpty(value))
{
// Check if there's a default value
if (!string.IsNullOrEmpty(typedArgDef.DefaultValue))
{
// We consider this argument as provided since it has a default value
continue;
}
// Add to missing arguments list
missingArgs.Add(typedArgDef.Name);
allRequiredArgumentsProvided = false;
}
}
}
if (!allRequiredArgumentsProvided)
{
context.Response.Status = 400;
context.Response.Message = $"Missing required arguments: {string.Join(", ", missingArgs)}";
}
return allRequiredArgumentsProvided;
}