Skip to content

Configurable command builder #925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/System.CommandLine.Tests/Builder/ConfigurableBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -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<InvalidOperationException>()
.Which
.Message
.Should()
.Be("Command has already been set.");
}
}
}
16 changes: 14 additions & 2 deletions src/System.CommandLine/Builder/CommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option> Options => Command.Children.OfType<Option>();

private protected void SetCommandInternal(Command command)
{
if (!(Command is null))
{
throw new InvalidOperationException("Command has already been set.");
}
Command = command;
}

internal void AddCommand(Command command) => Command.AddCommand(command);

internal void AddOption(Option option) => Command.AddOption(option);
Expand Down
4 changes: 4 additions & 0 deletions src/System.CommandLine/Builder/CommandLineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public CommandLineBuilder(Command? rootCommand = null)
{
}

private protected CommandLineBuilder()
: base()
{ }

public bool EnableDirectives { get; set; } = true;

public bool EnablePosixBundling { get; set; } = true;
Expand Down
14 changes: 14 additions & 0 deletions src/System.CommandLine/Builder/ConfigurableCommandLineBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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.

namespace System.CommandLine.Builder
{
public class ConfigurableCommandLineBuilder : CommandLineBuilder
{
public void SetCommand(Command command)
{
SetCommandInternal(command);
}

}
}