Add Get/TryGet for Feature in UserInput

This commit is contained in:
Matt Nadareski
2025-10-05 22:24:25 -04:00
parent 0bb996153a
commit 608cdae1d8
2 changed files with 115 additions and 0 deletions

View File

@@ -114,6 +114,63 @@ namespace SabreTools.CommandLine.Test.Inputs
#endregion
#region GetFeature
[Fact]
public void GetFeature_InvalidKey_Null()
{
UserInput userInput = new MockUserInput("a", "a", "a");
var child = new MockFeature("b", "b", "b");
userInput.Add(child);
Feature? actual = userInput.GetFeature("c");
Assert.Null(actual);
}
[Fact]
public void GetFeature_Exists_WrongType_Throws()
{
UserInput userInput = new MockUserInput("a", "a", "a");
var child = new MockUserInput("b", "b", "b");
userInput.Add(child);
Assert.Throws<ArgumentException>(() => _ = userInput.GetFeature("b"));
}
[Fact]
public void GetFeature_Exists_Returns()
{
UserInput userInput = new MockUserInput("a", "a", "a");
var child = new MockFeature("b", "b", "b");
userInput.Add(child);
int index = 0;
child.ProcessInput(["b"], ref index);
Feature? actual = userInput.GetFeature("b");
Assert.NotNull(actual);
Assert.Equal("b", actual.Name);
Assert.True(actual.Value);
}
[Fact]
public void GetFeature_NestedExists_Null()
{
UserInput userInput = new MockUserInput("a", "a", "a");
var child = new MockUserInput("b", "b", "b");
userInput.Add(child);
var subChild = new MockFeature("c", "c", "c");
child.Add(subChild);
int index = 0;
subChild.ProcessInput(["c"], ref index);
Feature? actual = userInput.GetFeature("c");
Assert.Null(actual);
}
#endregion
#region GetInt8
[Fact]
@@ -666,6 +723,28 @@ namespace SabreTools.CommandLine.Test.Inputs
#endregion
/// <summary>
/// Mock Feature implementation for testing
/// </summary>
private class MockFeature : Feature
{
public MockFeature(string name, string flag, string description, string? detailed = null)
: base(name, flag, description, detailed)
{
}
public MockFeature(string name, string[] flags, string description, string? detailed = null)
: base(name, flags, description, detailed)
{
}
/// <inheritdoc/>
public override bool Execute() => true;
/// <inheritdoc/>
public override bool VerifyInputs() => true;
}
/// <summary>
/// Mock UserInput implementation for testing
/// </summary>

View File

@@ -135,6 +135,19 @@ namespace SabreTools.CommandLine.Inputs
return defaultValue;
}
/// <summary>
/// Get a Feature value from a named input
/// </summary>
/// <param name="key">Input name to retrieve, if possible</param>
/// <returns>The value if found, null otherwise</returns>
public Feature? GetFeature(string key)
{
if (TryGetFeature(key, out Feature? value))
return value;
return null;
}
/// <summary>
/// Get an Int8 value from a named input
/// </summary>
@@ -312,6 +325,29 @@ namespace SabreTools.CommandLine.Inputs
return false;
}
/// <summary>
/// Get a Feature value from a named input
/// </summary>
/// <param name="key">Input name to retrieve, if possible</param>
/// <param name="value">Value that was found, default value otherwise</param>
/// <returns>True if the value was found, false otherwise</returns>
public bool TryGetFeature(string key, out Feature? value)
{
// Try to check immediate children
if (Children.TryGetValue(key, out var input))
{
if (input is not Feature i)
throw new ArgumentException("Input is not a Feature");
value = i;
return true;
}
// TODO: Investigate if nested features should be supported
value = null;
return false;
}
/// <summary>
/// Get an Int8 value from a named input
/// </summary>