From 23a483af0332cf55586d6f1b092732c1b08c2bb1 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sun, 5 Oct 2025 15:19:15 -0400 Subject: [PATCH] Add CommandSet helper to retrieve top level items --- .../CommandSetTests.cs | 22 +++++++++++++++++++ SabreTools.CommandLine/CommandSet.cs | 19 ++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/SabreTools.CommandLine.Test/CommandSetTests.cs b/SabreTools.CommandLine.Test/CommandSetTests.cs index baaf6b4..7a71ee1 100644 --- a/SabreTools.CommandLine.Test/CommandSetTests.cs +++ b/SabreTools.CommandLine.Test/CommandSetTests.cs @@ -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() { diff --git a/SabreTools.CommandLine/CommandSet.cs b/SabreTools.CommandLine/CommandSet.cs index 1477f19..e04528c 100644 --- a/SabreTools.CommandLine/CommandSet.cs +++ b/SabreTools.CommandLine/CommandSet.cs @@ -127,6 +127,25 @@ namespace SabreTools.CommandLine return string.Empty; } + /// + /// Gets the top-level user input associated with a given name or flag + /// + /// Name or flag value to match + /// User input associated with the name or flag, if possible + 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; + } + /// /// Check if a flag is a top-level (main application) flag ///