-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e9345e
commit f419479
Showing
15 changed files
with
382 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.