diff --git a/src/System.CommandLine.Tests/Builder/ConfigurableBuilderTests.cs b/src/System.CommandLine.Tests/Builder/ConfigurableBuilderTests.cs new file mode 100644 index 0000000000..0d5fab2d05 --- /dev/null +++ b/src/System.CommandLine.Tests/Builder/ConfigurableBuilderTests.cs @@ -0,0 +1,44 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.CommandLine.Builder; +using FluentAssertions; +using Xunit; + +namespace System.CommandLine.Tests.Builder +{ + public class ConfigurableBuilderTests + { + [Fact] + public void When_a_ConfigurableCommandLineBuider_is_created_it_has_a_null_Command() + { + var builder = new ConfigurableCommandLineBuilder(); + builder.Command.Should().BeNull(); + } + + [Fact] + public void ConfigurableCommandLineBuider_Command_can_be_set() + { + var builder = new ConfigurableCommandLineBuilder(); + builder.SetCommand(new Command("Bob")); + builder.Command.Should().NotBeNull() + .And.BeEmpty("Bob"); + + } + + [Fact] + public void ConfigurableCommandLineBuider_Command_should_throw_if_Command_already_set() + { + var builder = new ConfigurableCommandLineBuilder(); + builder.SetCommand(new Command("Bob")); + Action create = () => builder.SetCommand(new Command("Joe")); + + create.Should() + .Throw() + .Which + .Message + .Should() + .Be("Command has already been set."); + } + } +} diff --git a/src/System.CommandLine/Builder/CommandBuilder.cs b/src/System.CommandLine/Builder/CommandBuilder.cs index 8ae8d41a0b..f2bb18ffee 100644 --- a/src/System.CommandLine/Builder/CommandBuilder.cs +++ b/src/System.CommandLine/Builder/CommandBuilder.cs @@ -8,15 +8,27 @@ namespace System.CommandLine.Builder { public class CommandBuilder { - public CommandBuilder(Command command) + public CommandBuilder(Command command) { Command = command; } - public Command Command { get; } + private protected CommandBuilder() + { } + + public Command? Command { get; private set; } public IEnumerable