From 21a73b3d262ac9ef5e89be0428ecaa5b6faead77 Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Sun, 13 Sep 2020 09:54:44 -0700 Subject: [PATCH] fix: ensure clustering options is detected correctly when subcommands added via AddSubcommand (#393) --- src/CommandLineUtils/CommandLineApplication.cs | 2 ++ .../CommandLineProcessorTests.cs | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/CommandLineUtils/CommandLineApplication.cs b/src/CommandLineUtils/CommandLineApplication.cs index 7af05ed4..705884d4 100644 --- a/src/CommandLineUtils/CommandLineApplication.cs +++ b/src/CommandLineUtils/CommandLineApplication.cs @@ -458,6 +458,8 @@ public void AddSubcommand(CommandLineApplication subcommand) AssertCommandNameIsUnique(name, null); } + subcommand.Parent = this; + Commands.Add(subcommand); } diff --git a/test/CommandLineUtils.Tests/CommandLineProcessorTests.cs b/test/CommandLineUtils.Tests/CommandLineProcessorTests.cs index c037797b..1f645864 100644 --- a/test/CommandLineUtils.Tests/CommandLineProcessorTests.cs +++ b/test/CommandLineUtils.Tests/CommandLineProcessorTests.cs @@ -159,6 +159,21 @@ public void ItInfersClusterOptionsCannotBeUsed_MultiCharShortNameInSubcommand() Assert.False(app.ClusterOptions); } + [Fact] + public void ItInfersClusterOptionsCannotBeUsed_MultiCharShortNameViaAddedSubcommand() + { + var app = new CommandLineApplication(); + var subcmd = new CommandLineApplication + { + Name = "sub" + }; + subcmd.Option("-au|--auth", "Verbose output", CommandOptionType.NoValue); + app.AddSubcommand(subcmd); + Assert.False(app.ClusterOptionsWasSetExplicitly); + app.Parse("sub", "-au"); + Assert.False(app.ClusterOptions); + } + [Fact] public void ItInfersClusterOptionsCannotBeUsed_MultiCharShortNameAttribute() {