Skip to content
Merged
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
1 change: 0 additions & 1 deletion .idea/.idea.Ploch.CommandLine/.idea/.name

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/.idea.Ploch.CommandLine/.idea/git_toolbox_blame.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/.idea.Ploch.CommandLine/.idea/indexLayout.xml

This file was deleted.

4 changes: 2 additions & 2 deletions qodana.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: "1.0"
linter: jetbrains/qodana-cdnet:2024.1
linter: jetbrains/qodana-cdnet:latest
profile:
name: qodana.recommended
include:
- name: CheckDependencyLicenses
- name: CheckDependencyLicenses
5 changes: 5 additions & 0 deletions samples/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion samples/HelloWorldConsoleApp/HelloWorldConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
Expand Down
37 changes: 37 additions & 0 deletions samples/HelloWorldConsoleApp/HellowWorldCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.ComponentModel.DataAnnotations;
using McMaster.Extensions.CommandLineUtils;
using Ploch.Common.CommandLine;
#pragma warning disable Ex0100

namespace HelloWorldConsoleApp;

[Command(Name = "HelloWorld", Description = "Prints Hello World")]
#pragma warning disable ClassDocumentationHeader
public class HellowWorldCommand : IAsyncCommand
#pragma warning restore ClassDocumentationHeader
#pragma warning disable MethodDocumentationHeader
#pragma warning disable PropertyDocumentationHeader
{
[Option(Description = "Person First Name", ShortName = "f")]
[Required]

public required string FirstName { get; set; }


[Option(Description = "Person Last Name", ShortName = "l")]
[Required]
public required string LastName { get; set; }

[Option(Description = "Person Age", ShortName = "a")]
[Required]
public required int Age { get; set; }


public Task OnExecuteAsync(CancellationToken cancellationToken = default)
{
Console.WriteLine($"{FirstName} {LastName} ({Age}) - Hello World!");

return Task.CompletedTask;
}
}
#pragma warning restore PropertyDocumentationHeader
32 changes: 3 additions & 29 deletions samples/HelloWorldConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
// See https://aka.ms/new-console-template for more information

using System.ComponentModel.DataAnnotations;
using McMaster.Extensions.CommandLineUtils;
using HelloWorldConsoleApp;
using Ploch.Common.CommandLine;

var app = AppBuilder.CreateDefault().Build();
app.Command<HellowWorldCommand>();

[Command(Name = "HelloWorld", Description = "Prints Hello World")]
public class HellowWorldCommand : IAsyncCommand
{
[Option(Description = "Person First Name", ShortName = "f")]
[Required]
public required string FirstName { get; set; }

[Option(Description = "Person Last Name", ShortName = "l")]
[Required]
public required string LastName { get; set; }

[Option(Description = "Person Age", ShortName = "a")]
[Required]
public required int Age { get; set; }

public Task OnExecuteAsync(CancellationToken cancellationToken = default)
{
Console.WriteLine($"{FirstName} {LastName} ({Age}) - Hello World!");

return Task.CompletedTask;
}
}
var app = AppBuilder.CreateDefault("Hello World App", "A sample app").Build();
app.Command<HellowWorldCommand>();
21 changes: 21 additions & 0 deletions samples/HostingSample/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM mcr.microsoft.com/dotnet/runtime:9.0 AS base
USER $APP_UID
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["HostingSample/HostingSample.csproj", "HostingSample/"]
RUN dotnet restore "HostingSample/HostingSample.csproj"
COPY . .
WORKDIR "/src/HostingSample"
RUN dotnet build "HostingSample.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "HostingSample.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "HostingSample.dll"]
14 changes: 14 additions & 0 deletions samples/HostingSample/HelloRootCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Ploch.Common.CommandLine;
using McMaster.Extensions.CommandLineUtils;
using System.ComponentModel.DataAnnotations;

namespace HostingSample;
public class HelloRootCommand(CommandLineApplication app) : HelpOnlyCommand(app)
{
[Option(Inherited = true)]
public bool Verbose { get; set; }

[Option(Inherited = true)]
[Required]
public string HelloText { get; set; } = null!;
}
25 changes: 25 additions & 0 deletions samples/HostingSample/HostingSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-HostingSample-ba67250b-2688-481f-ae6e-990048d3df9a</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="10.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.2" />
</ItemGroup>

<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\CommandLine\Ploch.Common.CommandLine.csproj" />
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions samples/HostingSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Autofac.Core;
using Autofac.Core.Registration;
using HostingSample;

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
//builder.ConfigureContainer(new AutofacServiceProviderFactory(), collection => collection.RegisterModule<Worker>);

var host = builder.Build();
host.Run();

public class AutofacBuilder : IModule
{
public AutofacBuilder()
{
throw new NotImplementedException();
}

public void Configure(IComponentRegistryBuilder componentRegistry)
{
// componentRegistry.AddRegistrationSource(new Se);
}
}
12 changes: 12 additions & 0 deletions samples/HostingSample/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"HostingSample": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}
24 changes: 24 additions & 0 deletions samples/HostingSample/Worker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace HostingSample;

public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;

public Worker(ILogger<Worker> logger)
{
_logger = logger;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}

await Task.Delay(1000, stoppingToken);
}
}
}
8 changes: 8 additions & 0 deletions samples/HostingSample/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
8 changes: 8 additions & 0 deletions samples/HostingSample/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
18 changes: 11 additions & 7 deletions src/CommandLine.Autofac/Ploch.Common.CommandLine.Autofac.csproj
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<WarningsAsErrors>Nullable</WarningsAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection"/>
<PackageReference Include="McMaster.Extensions.CommandLineUtils"/>
<PackageReference Include="Autofac.Extensions.DependencyInjection" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" />
<PackageReference Update="Microsoft.CodeAnalysis.NetAnalyzers">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Update="Roslynator.Analyzers">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Update="SonarAnalyzer.CSharp">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CommandLine\Ploch.Common.CommandLine.csproj"/>
<ProjectReference Include="..\CommandLine\Ploch.Common.CommandLine.csproj" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions src/CommandLine.Hosting/IUnhandledExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using McMaster.Extensions.CommandLineUtils;
using Ploch.CommandLine.Hosting.Internal;

namespace Ploch.CommandLine.Hosting;

/// <summary>
/// Used by <see cref="CommandLineLifetime" /> to handle exceptions that are emitted from the
/// <see cref="CommandLineApplication{TModel}" /> e.g. during parsing or execution
/// </summary>
public interface IUnhandledExceptionHandler
{
/// <summary>
/// Handle otherwise uncaught exception. You are free to log, rethrow, … the exception
/// </summary>
/// <param name="e">An otherwise uncaught exception</param>
void HandleException(Exception e);
}
Loading
Loading