Skip to content

Commit ae46306

Browse files
authored
Add GenericHost playground sample (#912)
1 parent 1607aed commit ae46306

File tree

8 files changed

+133
-0
lines changed

8 files changed

+133
-0
lines changed

System.CommandLine.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.CommandLine.Hosting"
5656
EndProject
5757
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.CommandLine.Hosting.Tests", "src\System.CommandLine.Hosting.Tests\System.CommandLine.Hosting.Tests.csproj", "{39483140-BC26-4CAD-BBAE-3DC76C2F16CF}"
5858
EndProject
59+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HostingPlayground", "samples\HostingPlayground\HostingPlayground.csproj", "{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}"
60+
EndProject
5961
Global
6062
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6163
Debug|Any CPU = Debug|Any CPU
@@ -222,6 +224,18 @@ Global
222224
{39483140-BC26-4CAD-BBAE-3DC76C2F16CF}.Release|x64.Build.0 = Release|Any CPU
223225
{39483140-BC26-4CAD-BBAE-3DC76C2F16CF}.Release|x86.ActiveCfg = Release|Any CPU
224226
{39483140-BC26-4CAD-BBAE-3DC76C2F16CF}.Release|x86.Build.0 = Release|Any CPU
227+
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
228+
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|Any CPU.Build.0 = Debug|Any CPU
229+
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|x64.ActiveCfg = Debug|Any CPU
230+
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|x64.Build.0 = Debug|Any CPU
231+
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|x86.ActiveCfg = Debug|Any CPU
232+
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|x86.Build.0 = Debug|Any CPU
233+
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|Any CPU.ActiveCfg = Release|Any CPU
234+
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|Any CPU.Build.0 = Release|Any CPU
235+
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x64.ActiveCfg = Release|Any CPU
236+
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x64.Build.0 = Release|Any CPU
237+
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x86.ActiveCfg = Release|Any CPU
238+
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x86.Build.0 = Release|Any CPU
225239
EndGlobalSection
226240
GlobalSection(SolutionProperties) = preSolution
227241
HideSolutionNode = FALSE
@@ -240,6 +254,7 @@ Global
240254
{C39B0705-993E-43DB-B66A-A37A587F0BF7} = {E5B1EC71-0FC4-4FAA-9C65-32D5016FBC45}
241255
{644C4B4A-4A32-4307-9F71-C3BF901FFB66} = {E5B1EC71-0FC4-4FAA-9C65-32D5016FBC45}
242256
{39483140-BC26-4CAD-BBAE-3DC76C2F16CF} = {E5B1EC71-0FC4-4FAA-9C65-32D5016FBC45}
257+
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906} = {6749FB3E-39DE-4321-A39E-525278E9408D}
243258
EndGlobalSection
244259
GlobalSection(ExtensibilityGlobals) = postSolution
245260
SolutionGuid = {5C159F93-800B-49E7-9905-EE09F8B8434A}

samples/HostingPlayground/Greeter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace HostingPlayground
2+
{
3+
public class Greeter : IGreeter
4+
{
5+
6+
}
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace HostingPlayground
2+
{
3+
public class GreeterOptions
4+
{
5+
public string Name { get; }
6+
7+
public GreeterOptions(string name)
8+
{
9+
Name = name;
10+
}
11+
}
12+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
7+
<LangVersion>8</LangVersion>
8+
</PropertyGroup>
9+
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\src\System.CommandLine.Hosting\System.CommandLine.Hosting.csproj" />
13+
<ProjectReference Include="..\..\src\System.CommandLine\System.CommandLine.csproj" />
14+
</ItemGroup>
15+
16+
17+
<ItemGroup>
18+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.4" />
19+
<PackageReference Include="microsoft.extensions.hosting" Version="3.1.4" />
20+
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.4" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace HostingPlayground
2+
{
3+
public class HostingPlaygroundLogEvents
4+
{
5+
public const int GreetEvent = 1000;
6+
}
7+
}

samples/HostingPlayground/IGreeter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace HostingPlayground
4+
{
5+
interface IGreeter
6+
{
7+
void Greet(string name) => Console.WriteLine($"Hello, {name ?? "anonymous"}");
8+
}
9+
}

samples/HostingPlayground/Program.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.CommandLine;
2+
using System.CommandLine.Builder;
3+
using System.CommandLine.Hosting;
4+
using System.CommandLine.Invocation;
5+
using Microsoft.Extensions.Hosting;
6+
using Microsoft.Extensions.Logging;
7+
using System.CommandLine.Parsing;
8+
using System.Threading.Tasks;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using static HostingPlayground.HostingPlaygroundLogEvents;
11+
12+
namespace HostingPlayground
13+
{
14+
class Program
15+
{
16+
static async Task Main(string[] args) => await BuildCommandLine()
17+
.UseHost(_ => Host.CreateDefaultBuilder(),
18+
host =>
19+
{
20+
host.ConfigureServices(services =>
21+
{
22+
services.AddSingleton<IGreeter, Greeter>();
23+
});
24+
})
25+
.UseDefaults()
26+
.Build()
27+
.InvokeAsync(args);
28+
29+
private static CommandLineBuilder BuildCommandLine()
30+
{
31+
var root = new RootCommand(@"$ dotnet run --name 'Joe'"){
32+
new Option<string>("--name"){
33+
Required = true
34+
}
35+
};
36+
root.Handler = CommandHandler.Create<GreeterOptions, IHost>(Run);
37+
return new CommandLineBuilder(root);
38+
}
39+
40+
private static void Run(GreeterOptions options, IHost host)
41+
{
42+
var serviceProvider = host.Services;
43+
var greeter = serviceProvider.GetRequiredService<IGreeter>();
44+
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
45+
var logger = loggerFactory.CreateLogger(typeof(Program));
46+
47+
var name = options.Name;
48+
logger.LogInformation(GreetEvent, "Greeting was requested for: {name}", name);
49+
greeter.Greet(name);
50+
}
51+
}
52+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)