Add AddChildrenFrom method to CommandSet

This commit is contained in:
Matt Nadareski
2025-10-07 12:03:00 -04:00
parent 39acb90dbc
commit df54b92031
3 changed files with 39 additions and 1 deletions

View File

@@ -13,9 +13,16 @@ namespace SabreTools.CommandLine.Test
var input1 = new FlagInput("input1", "--input1", "input1");
var input2 = new FlagInput("input2", "--input2", "input2");
var feature1 = new MockFeature("feature1", "feature1", "feature1");
var inputA = new FlagInput("inputA", "--inputA", "inputA");
var inputB = new FlagInput("inputB", "--inputB", "inputB");
feature1.Add(inputA);
feature1.Add(inputB);
var featureSet = new CommandSet();
featureSet.Add(input1);
featureSet.Add(input2);
featureSet.AddChildrenFrom(feature1);
var actualInput1 = featureSet["input1"];
Assert.NotNull(actualInput1);
@@ -27,6 +34,14 @@ namespace SabreTools.CommandLine.Test
var actualInput3 = featureSet["input3"];
Assert.Null(actualInput3);
var actualInputA = featureSet["inputA"];
Assert.NotNull(actualInputA);
Assert.Equal("inputA", actualInputA.Name);
var actualinputB = featureSet["inputB"];
Assert.NotNull(actualinputB);
Assert.Equal("inputB", actualinputB.Name);
}
[Fact]

View File

@@ -120,6 +120,29 @@ namespace SabreTools.CommandLine
public void Add(UserInput input)
=> _inputs.Add(input.Name, input);
/// <summary>
/// Add all children from an input to the set
/// </summary>
/// <param name="input">UserInput object to retrieve children from</param>
/// <remarks>
/// This should only be used in situations where an input is defined
/// but not used within the context of the command set directly.
///
/// This is helpful for when there are applications with default functionality
/// that need to be able to expose both defined features as well as
/// the default functionality in help text.
///
/// If there is any overlap between existing names and the names from
/// any of the children, this operation will overwrite them.
/// </reamrks>
public void AddChildrenFrom(UserInput input)
{
foreach (var kvp in input.Children)
{
_inputs.Add(kvp.Key, kvp.Value);
}
}
#endregion
#region Children

View File

@@ -41,7 +41,7 @@ namespace SabreTools.CommandLine.Inputs
/// <summary>
/// Set of children associated with this input
/// </summary>
protected readonly Dictionary<string, UserInput> Children = [];
internal protected readonly Dictionary<string, UserInput> Children = [];
#endregion