fix(driver-ablegacy-cli): resolve Medium code-review finding (Driver.AbLegacy.Cli-001)
WriteCommand.ParseValue wraps FormatException/OverflowException as CliFx CommandException so a bad --value yields a clean one-line CLI error naming the value and target type instead of a raw .NET stack trace. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -59,17 +59,38 @@ public sealed class WriteCommand : AbLegacyCommandBase
|
||||
}
|
||||
|
||||
/// <summary>Parse <c>--value</c> per <see cref="AbLegacyDataType"/>, invariant culture.</summary>
|
||||
internal static object ParseValue(string raw, AbLegacyDataType type) => type switch
|
||||
/// <exception cref="CliFx.Exceptions.CommandException">
|
||||
/// Thrown when <paramref name="raw"/> cannot be parsed as the requested type (malformed
|
||||
/// input or out-of-range value) so CliFx renders a clean one-line error instead of a raw
|
||||
/// stack trace.
|
||||
/// </exception>
|
||||
internal static object ParseValue(string raw, AbLegacyDataType type)
|
||||
{
|
||||
AbLegacyDataType.Bit => ParseBool(raw),
|
||||
AbLegacyDataType.Int or AbLegacyDataType.AnalogInt => short.Parse(raw, CultureInfo.InvariantCulture),
|
||||
AbLegacyDataType.Long => int.Parse(raw, CultureInfo.InvariantCulture),
|
||||
AbLegacyDataType.Float => float.Parse(raw, CultureInfo.InvariantCulture),
|
||||
AbLegacyDataType.String => raw,
|
||||
AbLegacyDataType.TimerElement or AbLegacyDataType.CounterElement
|
||||
or AbLegacyDataType.ControlElement => int.Parse(raw, CultureInfo.InvariantCulture),
|
||||
_ => throw new CliFx.Exceptions.CommandException($"Unsupported DataType '{type}' for write."),
|
||||
};
|
||||
try
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
AbLegacyDataType.Bit => ParseBool(raw),
|
||||
AbLegacyDataType.Int or AbLegacyDataType.AnalogInt => short.Parse(raw, CultureInfo.InvariantCulture),
|
||||
AbLegacyDataType.Long => int.Parse(raw, CultureInfo.InvariantCulture),
|
||||
AbLegacyDataType.Float => float.Parse(raw, CultureInfo.InvariantCulture),
|
||||
AbLegacyDataType.String => raw,
|
||||
AbLegacyDataType.TimerElement or AbLegacyDataType.CounterElement
|
||||
or AbLegacyDataType.ControlElement => int.Parse(raw, CultureInfo.InvariantCulture),
|
||||
_ => throw new CliFx.Exceptions.CommandException($"Unsupported DataType '{type}' for write."),
|
||||
};
|
||||
}
|
||||
catch (FormatException ex)
|
||||
{
|
||||
throw new CliFx.Exceptions.CommandException(
|
||||
$"Value '{raw}' is not a valid {type}: {ex.Message}", innerException: ex);
|
||||
}
|
||||
catch (OverflowException ex)
|
||||
{
|
||||
throw new CliFx.Exceptions.CommandException(
|
||||
$"Value '{raw}' is out of range for {type}: {ex.Message}", innerException: ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool ParseBool(string raw) => raw.Trim().ToLowerInvariant() switch
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user