Skip to content

Commit

Permalink
Different structure
Browse files Browse the repository at this point in the history
  • Loading branch information
DanteDeRuwe committed Nov 19, 2024
1 parent 8e9345e commit f419479
Show file tree
Hide file tree
Showing 15 changed files with 382 additions and 237 deletions.
34 changes: 32 additions & 2 deletions Dotty.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dotty.CLI.IntegrationTest",
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{16C94B47-B0AC-465F-AF52-8834503D8B3E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Live", "src\Live\Live.csproj", "{8EF0791B-3740-4567-A578-91BDB684BE23}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo.Basic", "demos\Demo.Basic\Demo.Basic.csproj", "{8EF0791B-3740-4567-A578-91BDB684BE23}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo.Basic.Net6", "demos\Demo.Basic.Net6\Demo.Basic.Net6.csproj", "{0C024D6E-4C33-4BCB-A79C-0A4462BF63A2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo.GenericHost", "demos\Demo.GenericHost\Demo.GenericHost.csproj", "{BE16BF78-23BF-45B7-AD6E-CFBCB3136425}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo.SpectreConsoleCli", "demos\Demo.SpectreConsoleCli\Demo.SpectreConsoleCli.csproj", "{C8153F73-926C-4F11-88F9-123BCDE798CD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo.Cocona", "demos\Demo.Cocona\Demo.Cocona.csproj", "{B3ED9A9B-B354-4544-8C78-311C6107B1B4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "demos", "demos", "{496C1F80-5DEA-4148-879E-6C6507B5745C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -39,10 +49,30 @@ Global
{8EF0791B-3740-4567-A578-91BDB684BE23}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8EF0791B-3740-4567-A578-91BDB684BE23}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8EF0791B-3740-4567-A578-91BDB684BE23}.Release|Any CPU.Build.0 = Release|Any CPU
{0C024D6E-4C33-4BCB-A79C-0A4462BF63A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C024D6E-4C33-4BCB-A79C-0A4462BF63A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C024D6E-4C33-4BCB-A79C-0A4462BF63A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C024D6E-4C33-4BCB-A79C-0A4462BF63A2}.Release|Any CPU.Build.0 = Release|Any CPU
{BE16BF78-23BF-45B7-AD6E-CFBCB3136425}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE16BF78-23BF-45B7-AD6E-CFBCB3136425}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE16BF78-23BF-45B7-AD6E-CFBCB3136425}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE16BF78-23BF-45B7-AD6E-CFBCB3136425}.Release|Any CPU.Build.0 = Release|Any CPU
{C8153F73-926C-4F11-88F9-123BCDE798CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C8153F73-926C-4F11-88F9-123BCDE798CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C8153F73-926C-4F11-88F9-123BCDE798CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C8153F73-926C-4F11-88F9-123BCDE798CD}.Release|Any CPU.Build.0 = Release|Any CPU
{B3ED9A9B-B354-4544-8C78-311C6107B1B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B3ED9A9B-B354-4544-8C78-311C6107B1B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3ED9A9B-B354-4544-8C78-311C6107B1B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3ED9A9B-B354-4544-8C78-311C6107B1B4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{794E14E8-E350-427E-9AC5-A09DF53239A9} = {AE5D96E8-180C-4E9A-99D3-B709B0517EAF}
{7A43ABC4-DEA2-4EF1-969D-A6362CB221C7} = {16C94B47-B0AC-465F-AF52-8834503D8B3E}
{8EF0791B-3740-4567-A578-91BDB684BE23} = {16C94B47-B0AC-465F-AF52-8834503D8B3E}
{8EF0791B-3740-4567-A578-91BDB684BE23} = {496C1F80-5DEA-4148-879E-6C6507B5745C}
{0C024D6E-4C33-4BCB-A79C-0A4462BF63A2} = {496C1F80-5DEA-4148-879E-6C6507B5745C}
{BE16BF78-23BF-45B7-AD6E-CFBCB3136425} = {496C1F80-5DEA-4148-879E-6C6507B5745C}
{C8153F73-926C-4F11-88F9-123BCDE798CD} = {496C1F80-5DEA-4148-879E-6C6507B5745C}
{B3ED9A9B-B354-4544-8C78-311C6107B1B4} = {496C1F80-5DEA-4148-879E-6C6507B5745C}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions demos/Demo.Basic.Net6/Demo.Basic.Net6.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
</ItemGroup>

</Project>
68 changes: 68 additions & 0 deletions demos/Demo.Basic.Net6/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
if (!args.Any())
{
Console.Error.WriteLine("No command provided. Possible commands: 'ping', 'generate'.");
return 1;
}

switch (args[0])
{
case "ping":
Console.WriteLine("pong");
return 0;
case "generate":
switch (args[1])
{
case "guid":
Console.WriteLine($"Generated GUID: {Guid.NewGuid()}");
return 0;
case "number":
return ExecuteGenerateNumberCommand(args.Skip(2).ToArray());
default:
Console.Error.WriteLine("Invalid command. Use 'guid' or 'number'.");
return 1;
}
default:
Console.WriteLine("Unknown command.");
return 1;
}

int ExecuteGenerateNumberCommand(string[] options)
{
int from = 0, to = 100;

// Parse '--from' argument
if (options.Contains("--from"))
{
var index = Array.IndexOf(options, "--from");
var hasFrom = index + 1 < options.Length && int.TryParse(options[index + 1], out from);
if (!hasFrom)
{
Console.Error.WriteLine("Invalid value for --from.");
return 1;
}
}

// Parse '--to' argument
if (options.Contains("--to"))
{
var index = Array.IndexOf(options, "--to");
var hasTo = index + 1 < options.Length && int.TryParse(options[index + 1], out to);
if (!hasTo)
{
Console.Error.WriteLine("Invalid value for --to.");
return 1;
}
}

// Validate: If 'from' is greater than 'to', error!
if (from >= to)
{
Console.Error.WriteLine("The 'from' value cannot be greater than the 'to' value.");
return 1;
}

// Generate a random number in the specified range
var number = Random.Shared.Next(from, to);
Console.WriteLine($"Generated number: {number}");
return 0;
}
10 changes: 10 additions & 0 deletions demos/Demo.Basic/Demo.Basic.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
61 changes: 61 additions & 0 deletions demos/Demo.Basic/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
switch (args)
{
case []:
Console.Error.WriteLine("No command provided. Possible commands: 'ping', 'generate'.");
return 1;
case ["ping"]:
Console.WriteLine("pong");
return 0;
case ["generate", "guid"]:
Console.WriteLine($"Generated GUID: {Guid.NewGuid()}");
return 0;
case ["generate", "number", ..var options]:
return ExecuteGenerateNumberCommand(options);
case ["generate", ..]:
Console.Error.WriteLine("Invalid command. Use 'guid' or 'number'.");
return 1;
default:
Console.WriteLine("Unknown command.");
return 1;
}

int ExecuteGenerateNumberCommand(string[] options)
{
int from = 0, to = 100;

// Parse '--from' argument
if (options.Contains("--from"))
{
var index = Array.IndexOf(options, "--from");
var hasFrom = index + 1 < options.Length && int.TryParse(options[index + 1], out from);
if (!hasFrom)
{
Console.Error.WriteLine("Invalid value for --from.");
return 1;
}
}

// Parse '--to' argument
if (options.Contains("--to"))
{
var index = Array.IndexOf(options, "--to");
var hasTo = index + 1 < options.Length && int.TryParse(options[index + 1], out to);
if (!hasTo)
{
Console.Error.WriteLine("Invalid value for --to.");
return 1;
}
}

// Validate: If 'from' is greater than 'to', error!
if (from >= to)
{
Console.Error.WriteLine("The 'from' value cannot be greater than the 'to' value.");
return 1;
}

// Generate a random number in the specified range
var number = Random.Shared.Next(from, to);
Console.WriteLine($"Generated number: {number}");
return 0;
}
14 changes: 14 additions & 0 deletions demos/Demo.Cocona/Demo.Cocona.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Cocona" Version="2.2.0" />
</ItemGroup>

</Project>
44 changes: 44 additions & 0 deletions demos/Demo.Cocona/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Cocona;
using Cocona.Filters;

var builder = CoconaApp.CreateBuilder();

// Define services here

var app = builder.Build();

// Commands without arguments
app.AddCommand("ping", () => Console.WriteLine("pong"))
.WithDescription("Send a ping to the CLI")
.WithAliases("p")
.WithFilter(new DelegateCommandFilter(async (ctx, next) =>
{
Console.WriteLine("Before command execution");
await next(ctx);
Console.WriteLine("After command execution");
return 0;
}));

// SHOW HELP!

// Subcommands without arguments
app.AddSubCommand("generate", group =>
{
group.AddCommand("guid", () => Console.WriteLine(Guid.NewGuid()));
group.AddCommand("number", () => Console.WriteLine(Random.Shared.Next(0, 100)));
});


// Commands with arguments

// option by default, switch to argument
// app.AddCommand("echo", (string message) => Console.WriteLine(message));
app.AddCommand("echo", ([Argument] string message) => Console.WriteLine(message));

// Mix and match
app.AddCommand("repeat", (string message, int times) =>
{
for (var i = 0; i < times; i++) Console.WriteLine(message);
});

app.Run();
18 changes: 18 additions & 0 deletions demos/Demo.GenericHost/Demo.GenericHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
</ItemGroup>

</Project>
106 changes: 106 additions & 0 deletions demos/Demo.GenericHost/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

var builder = Host.CreateDefaultBuilder(args);

builder.ConfigureLogging(logging => { logging.AddFilter("Microsoft", LogLevel.Warning); });

builder.ConfigureServices(services =>
{
services.AddKeyedSingleton("args", args);
services.AddHostedService<ConsoleHostedService>();
});

var app = builder.Build();

app.Run();

// ---

file class ConsoleHostedService(
[FromKeyedServices("args")] string[] args,
IHostApplicationLifetime appLifetime)
: IHostedService
{
public Task StartAsync(CancellationToken cancellationToken)
{
appLifetime.ApplicationStarted.Register(() =>
{
var exitCode = Handle();
Environment.ExitCode = exitCode;
});

return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;

private int Handle()
{
switch (args)
{
case []:
Console.WriteLine("No command provided. Possible commands: 'ping', 'generate'.");
return 1;
case ["ping"]:
Console.WriteLine("pong");
return 0;
case ["generate", "guid"]:
{
// Generate a GUID
Guid guid = Guid.NewGuid();
Console.WriteLine($"Generated GUID: {guid}");
return 0;
}
case ["generate", "number", .. var options]:
{
int from = 0, to = 100;
bool hasFrom = false, hasTo = false;

// Parse '--from' argument
if (options.Contains("--from"))
{
var index = Array.IndexOf(options, "--from");
hasFrom = index + 1 < options.Length && int.TryParse(options[index + 1], out from);
if (!hasFrom)
{
Console.WriteLine("Invalid value for --from.");
return 1;
}
}

// Parse '--to' argument
if (options.Contains("--to"))
{
var index = Array.IndexOf(options, "--to");
hasTo = index + 1 < options.Length && int.TryParse(options[index + 1], out to);
if (!hasTo)
{
Console.WriteLine("Invalid value for --to.");
return 1;
}
}

// Validate if 'from' is greater than 'to'
if (hasFrom && hasTo && from > to)
{
Console.WriteLine("The 'from' value cannot be greater than the 'to' value.");
return 1;
}

// Generate a random number in the specified range
Random rand = new Random();
var number = rand.Next(from, to + 1); // The upper bound is exclusive, so add +1 to include 'to'
Console.WriteLine($"Generated number: {number}");
return 0;
}
case ["generate", ..]:
Console.WriteLine("Invalid command. Use 'guid' or 'number'.");
return 1;
default:
Console.WriteLine("Unknown command.");
return 1;
}
}
}
Loading

0 comments on commit f419479

Please sign in to comment.