Skip to content

Commit 68a3a7b

Browse files
feature: implement API on host builder to fetch CommandLineContext from non DI contexts (#368)
Co-authored-by: Daniel Meza <[email protected]>
1 parent 5f9e167 commit 68a3a7b

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using McMaster.Extensions.CommandLineUtils.Abstractions;
7+
using McMaster.Extensions.Hosting.CommandLine.Internal;
8+
9+
namespace Microsoft.Extensions.Hosting
10+
{
11+
/// <summary>
12+
/// Extensions methods for the <see cref="HostBuilderContext"/>
13+
/// </summary>
14+
public static class HostBuilderContextExtensions
15+
{
16+
/// <summary>
17+
/// Get the <see cref="CommandLineContext"/> used to run the app
18+
/// </summary>
19+
/// <param name="context">This instance</param>
20+
/// <returns>The <see cref="CommandLineContext"/> used to run the app</returns>
21+
public static CommandLineContext GetCommandLineContext(this HostBuilderContext context)
22+
=> (CommandLineContext)context.Properties[typeof(CommandLineState)];
23+
}
24+
}

src/Hosting.CommandLine/HostBuilderExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public static async Task<int> RunCommandLineApplicationAsync<TApp>(
6060
configure ??= app => { };
6161
var exceptionHandler = new StoreExceptionHandler();
6262
var state = new CommandLineState(args);
63+
hostBuilder.Properties[typeof(CommandLineState)] = state;
6364
hostBuilder.ConfigureServices(
6465
(context, services)
6566
=>
@@ -111,6 +112,7 @@ public static async Task<int> RunCommandLineApplicationAsync(
111112
{
112113
var exceptionHandler = new StoreExceptionHandler();
113114
var state = new CommandLineState(args);
115+
hostBuilder.Properties[typeof(CommandLineState)] = state;
114116
hostBuilder.ConfigureServices(
115117
(context, services)
116118
=>

src/Hosting.CommandLine/PublicAPI.Shipped.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ Microsoft.Extensions.Hosting.HostBuilderExtensions
44
static Microsoft.Extensions.Hosting.HostBuilderExtensions.RunCommandLineApplicationAsync<TApp>(this Microsoft.Extensions.Hosting.IHostBuilder hostBuilder, string[] args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<int>
55
static Microsoft.Extensions.Hosting.HostBuilderExtensions.RunCommandLineApplicationAsync<TApp>(this Microsoft.Extensions.Hosting.IHostBuilder hostBuilder, string[] args, System.Action<McMaster.Extensions.CommandLineUtils.CommandLineApplication<TApp>> configure, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<int>
66
static Microsoft.Extensions.Hosting.HostBuilderExtensions.RunCommandLineApplicationAsync(this Microsoft.Extensions.Hosting.IHostBuilder hostBuilder, string[] args, System.Action<McMaster.Extensions.CommandLineUtils.CommandLineApplication> configure, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<int>
7+
Microsoft.Extensions.Hosting.HostBuilderContextExtensions
8+
static Microsoft.Extensions.Hosting.HostBuilderContextExtensions.GetCommandLineContext(this Microsoft.Extensions.Hosting.HostBuilderContext context) -> McMaster.Extensions.CommandLineUtils.Abstractions.CommandLineContext
9+

test/Hosting.CommandLine.Tests/HostBuilderExtensionsBuilderAPITests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Threading.Tasks;
44
using McMaster.Extensions.CommandLineUtils;
5+
using McMaster.Extensions.CommandLineUtils.Abstractions;
56
using McMaster.Extensions.CommandLineUtils.Conventions;
67
using McMaster.Extensions.Hosting.CommandLine.Tests.Utilities;
78
using Microsoft.Extensions.DependencyInjection;
@@ -102,5 +103,28 @@ public async Task TestUsingServiceProvider()
102103

103104
Assert.NotNull(env);
104105
}
106+
107+
[Fact]
108+
public async Task TestCommandLineContextFromNonDIContexts()
109+
{
110+
CommandLineContext configureServicesContext = null;
111+
CommandLineContext confgureAppContext = null;
112+
await new HostBuilder()
113+
.ConfigureServices((context, collection) =>
114+
{
115+
configureServicesContext = context.GetCommandLineContext();
116+
collection.AddSingleton<IConsole>(new TestConsole(_output));
117+
})
118+
.ConfigureAppConfiguration((context, builder) =>
119+
{
120+
confgureAppContext = context.GetCommandLineContext();
121+
})
122+
.RunCommandLineApplicationAsync(new string[0], app => app.OnExecute(() =>
123+
{
124+
}));
125+
126+
Assert.NotNull(configureServicesContext);
127+
Assert.NotNull(confgureAppContext);
128+
}
105129
}
106130
}

0 commit comments

Comments
 (0)