Add CommandSet helper to retrieve top level items

This commit is contained in:
Matt Nadareski
2025-10-05 15:19:15 -04:00
parent 6cd5cf947b
commit 23a483af03
2 changed files with 41 additions and 0 deletions

View File

@@ -49,6 +49,28 @@ namespace SabreTools.CommandLine.Test
Assert.Empty(actualName3);
}
[Fact]
public void GetTopLevelTest()
{
var input1 = new FlagInput("input1", "--input1", "input1");
var input2 = new FlagInput("input2", "--input2", "input2");
var featureSet = new CommandSet();
featureSet.Add(input1);
featureSet.Add(input2);
var actualInput1 = featureSet.GetTopLevel("input1");
Assert.NotNull(actualInput1);
Assert.Equal("input1", actualInput1.Name);
var actualInput2 = featureSet.GetTopLevel("--input2");
Assert.NotNull(actualInput2);
Assert.Equal("input2", actualInput2.Name);
var actualInput3 = featureSet.GetTopLevel("input3");
Assert.Null(actualInput3);
}
[Fact]
public void TopLevelFlagTest()
{

View File

@@ -127,6 +127,25 @@ namespace SabreTools.CommandLine
return string.Empty;
}
/// <summary>
/// Gets the top-level user input associated with a given name or flag
/// </summary>
/// <param name="value">Name or flag value to match</param>
/// <returns>User input associated with the name or flag, if possible</returns>
public UserInput? GetTopLevel(string value)
{
// Validate the value matches something
string inputName = GetInputName(value);
if (inputName.Length == 0)
return null;
// Try to get the child based on the name
if (!_inputs.TryGetValue(inputName, out var input))
return null;
return input;
}
/// <summary>
/// Check if a flag is a top-level (main application) flag
/// </summary>