Skip to content

Commit 8ab5df9

Browse files
Keboojonsequitur
authored andcommitted
Adding global options to the builder.
1 parent fff5199 commit 8ab5df9

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.CommandLine.Builder;
2+
using System.CommandLine.Parsing;
3+
using FluentAssertions;
4+
using Xunit;
5+
6+
namespace System.CommandLine.Tests.Builder
7+
{
8+
public class CommandLineBuilderExtensionsTests
9+
{
10+
[Fact]
11+
public void Global_options_are_added_to_the_root_command()
12+
{
13+
var globalOption = new Option("global");
14+
var builder = new CommandLineBuilder()
15+
.AddGlobalOption(globalOption);
16+
17+
Parser parser = builder.Build();
18+
19+
Command rootCommand = (Command)parser.Configuration.RootCommand;
20+
rootCommand.GlobalOptions
21+
.Should()
22+
.Contain(globalOption);
23+
}
24+
}
25+
}

src/System.CommandLine.Tests/CommandTests.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.CommandLine.Binding;
5-
using System.CommandLine.Invocation;
6-
using System.CommandLine.Parsing;
74
using FluentAssertions;
5+
using System.CommandLine.Parsing;
86
using System.Linq;
97
using Xunit;
108

src/System.CommandLine/Builder/CommandBuilder.cs

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public CommandBuilder(Command command)
2121

2222
internal void AddOption(Option option) => Command.AddOption(option);
2323

24+
internal void AddGlobalOption(Option option) => Command.AddGlobalOption(option);
25+
2426
internal void AddArgument(Argument argument) => Command.AddArgument(argument);
2527
}
2628
}

src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ public static TBuilder AddOption<TBuilder>(
6464
return builder;
6565
}
6666

67+
public static TBuilder AddGlobalOption<TBuilder>(
68+
this TBuilder builder,
69+
Option option)
70+
where TBuilder : CommandBuilder
71+
{
72+
builder.AddGlobalOption(option);
73+
74+
return builder;
75+
}
76+
6777
public static CommandLineBuilder CancelOnProcessTermination(this CommandLineBuilder builder)
6878
{
6979
builder.AddMiddleware(async (context, next) =>

0 commit comments

Comments
 (0)