### Setting up Remora.Commands with Dependency Injection Source: https://github.com/remora/remora.commands/blob/main/Remora.Commands/README.md Provides a step-by-step guide on integrating Remora.Commands into an application using dependency injection. It covers declaring command groups, adding commands, configuring the command service, and building the service provider. ```cs var services = new ServiceCollection() .AddCommands() .AddCommandTree() .WithCommandGroup() .Finish() .BuildServiceProvider(); ``` -------------------------------- ### Setting up Remora.Commands with Dependency Injection Source: https://github.com/remora/remora.commands/blob/main/README.md Provides a step-by-step guide on how to integrate Remora.Commands into an application using .NET's dependency injection. It covers declaring command groups, adding commands, configuring the command service, and executing commands from an input source. ```cs var services = new ServiceCollection() .AddCommands() .AddCommandTree() .WithCommandGroup() .Finish() .BuildServiceProvider(); ``` ```cs private readonly CommandService _commandService; public async Task MyInputHandler ( string userInput, CancellationToken ct ) { var executionResult = await _commandService.TryExecuteAsync ( userInput, ct: ct ); if (executionResult.IsSuccess) { return executionResult; } _logger.Error("Oh no!"); _logger.Error("Anyway"); } ``` -------------------------------- ### Basic Command Invocation and C# Mapping Source: https://github.com/remora/remora.commands/blob/main/Remora.Commands/README.md Demonstrates how a typical *nix getopts-style command invocation is translated into a C# method call within the Remora.Commands framework. It shows the command group, command attribute, and parameter mapping including options and switches. ```cs !things add new-thing --description "My thing!" --enable-indexing [Group("things")] public class ThingCommands : CommandGroup { [Command("add")] public Task AddThingAsync ( string name, // = "new-thing" [Option("description")] string description, // = "My thing!" [Switch("enable-indexing")] bool enableIndexing = false // = true ) { return _thingService.AddThing(name, description, enableIndexing); } } ``` -------------------------------- ### Basic Command Invocation and C# Mapping Source: https://github.com/remora/remora.commands/blob/main/README.md Demonstrates how a typical *nix getopts-style command invocation is parsed and mapped to a C# command group and method within the Remora.Commands framework. It shows the input string and the corresponding C# code structure. ```text !things add new-thing --description "My thing!" --enable-indexing ``` ```cs [Group("things")] public class ThingCommands : CommandGroup { [Command("add")] public Task AddThingAsync ( string name, // = "new-thing" [Option("description")] string description, // = "My thing!" [Switch("enable-indexing")] bool enableIndexing = false // = true ) { return _thingService.AddThing(name, description, enableIndexing); } } ``` -------------------------------- ### Executing Commands from Input Source: https://github.com/remora/remora.commands/blob/main/Remora.Commands/README.md Illustrates how to parse and execute user input using the `CommandService` in Remora.Commands. It shows the process of calling `TryExecuteAsync` and handling the execution result, including error logging. ```cs private readonly CommandService _commandService; public async Task MyInputHandler ( string userInput, CancellationToken ct ) { var executionResult = await _commandService.TryExecuteAsync ( userInput, ct: ct ); if (executionResult.IsSuccess) { return executionResult; } _logger.Error("Oh no!"); _logger.Error("Anyway"); } ``` -------------------------------- ### Executing Commands from Specific Trees Source: https://github.com/remora/remora.commands/blob/main/README.md Demonstrates how to execute commands from a specific, named command tree using the `CommandService`. This allows for targeted command execution in multi-tree applications. ```cs using Remora.Commands; using Remora.Results; using Microsoft.Extensions.Logging; using System.Threading; using System.Threading.Tasks; public class CommandExecutor { private readonly CommandService _commandService; private readonly ILogger _logger; public CommandExecutor(CommandService commandService, ILogger logger) { _commandService = commandService; _logger = logger; } public async Task HandleUserInput(string userInput, CancellationToken ct) { // Execute command from the default tree var defaultExecutionResult = await _commandService.TryExecuteAsync(userInput, ct: ct); if (defaultExecutionResult.IsSuccess) { return defaultExecutionResult; } // Execute command from a named tree 'myothertree' var namedExecutionResult = await _commandService.TryExecuteAsync( userInput, treeName: "myothertree", ct: ct); if (namedExecutionResult.IsSuccess) { return namedExecutionResult; } _logger.LogError("Command execution failed for input: {UserInput}", userInput); return Results.Failure("Command not found or failed to execute."); } } ``` -------------------------------- ### Executing Commands from Specific Trees Source: https://github.com/remora/remora.commands/blob/main/Remora.Commands/README.md Demonstrates how to execute commands from a specific, named command tree using the `CommandService`. This allows for targeted command execution in multi-tree applications. ```cs using Remora.Commands; using Remora.Results; using Microsoft.Extensions.Logging; using System.Threading; using System.Threading.Tasks; public class CommandExecutor { private readonly CommandService _commandService; private readonly ILogger _logger; public CommandExecutor(CommandService commandService, ILogger logger) { _commandService = commandService; _logger = logger; } public async Task HandleUserInput(string userInput, CancellationToken ct) { // Execute command from the default tree var defaultExecutionResult = await _commandService.TryExecuteAsync(userInput, ct: ct); if (defaultExecutionResult.IsSuccess) { return defaultExecutionResult; } // Execute command from a named tree 'myothertree' var namedExecutionResult = await _commandService.TryExecuteAsync( userInput, treeName: "myothertree", ct: ct); if (namedExecutionResult.IsSuccess) { return namedExecutionResult; } _logger.LogError("Command execution failed for input: {UserInput}", userInput); return Results.Failure("Command not found or failed to execute."); } } ``` -------------------------------- ### Combined Short-Name Switches and Named Options Source: https://github.com/remora/remora.commands/blob/main/Remora.Commands/README.md Shows how Remora.Commands allows for the combination of short-name switches, similar to GNU tar, and the placement of a normal named option at the end with its value. This demonstrates flexibility in command-line argument parsing. ```cs my-command -xvz my-command -xvf file.bin ``` -------------------------------- ### Registering Multiple Command Trees Source: https://github.com/remora/remora.commands/blob/main/README.md Shows how to register multiple, independent command trees within the same service provider. This is useful for applications that need different sets of commands for different contexts or user groups. ```cs using Remora.Commands; using Microsoft.Extensions.DependencyInjection; using System.Threading; using System.Threading.Tasks; // Assume MyCommands and MyOtherCommands are defined CommandGroup classes public class MyCommands : CommandGroup { } public class MyOtherCommands : CommandGroup { } var services = new ServiceCollection() .AddCommands() .AddCommandTree() // Registers the default, unnamed tree .WithCommandGroup() .Finish() .AddCommandTree("myothertree") // Registers a named tree .WithCommandGroup() .Finish() .BuildServiceProvider(); // Accessing named trees using CommandTreeAccessor var accessor = services.GetRequiredService(); if (accessor.TryGetNamedTree("myothertree", out var tree)) { // Use the 'myothertree' command tree } if (accessor.TryGetNamedTree(null, out var defaultTree)) // or Constants.DefaultTreeName { // Use the default command tree } ``` -------------------------------- ### Combined Short-Name Switches and Named Options Source: https://github.com/remora/remora.commands/blob/main/README.md Shows how Remora.Commands allows for the combination of short-name switches, similar to GNU tar, and the placement of a normal named option followed by its value. ```text my-command -xvz my-command -xvf file.bin ``` -------------------------------- ### Registering Multiple Command Trees Source: https://github.com/remora/remora.commands/blob/main/Remora.Commands/README.md Shows how to register multiple, independent command trees within the same service provider. This is useful for applications that need different sets of commands for different contexts or user groups. ```cs using Remora.Commands; using Microsoft.Extensions.DependencyInjection; using System.Threading; using System.Threading.Tasks; // Assume MyCommands and MyOtherCommands are defined CommandGroup classes public class MyCommands : CommandGroup { } public class MyOtherCommands : CommandGroup { } var services = new ServiceCollection() .AddCommands() .AddCommandTree() // Registers the default, unnamed tree .WithCommandGroup() .Finish() .AddCommandTree("myothertree") // Registers a named tree .WithCommandGroup() .Finish() .BuildServiceProvider(); // Accessing named trees using CommandTreeAccessor var accessor = services.GetRequiredService(); if (accessor.TryGetNamedTree("myothertree", out var tree)) { // Use the 'myothertree' command tree } if (accessor.TryGetNamedTree(null, out var defaultTree)) // or Constants.DefaultTreeName { // Use the default command tree } ``` -------------------------------- ### Cumulative Command Tree Registration Source: https://github.com/remora/remora.commands/blob/main/Remora.Commands/README.md Explains how `AddCommandTree` calls with the same name are cumulative, allowing command groups to be registered from multiple locations, such as different plugins or modules. ```cs using Remora.Commands; using Microsoft.Extensions.DependencyInjection; // Assume MyOtherCommands and MoreCommands are defined CommandGroup classes public class MyOtherCommands : CommandGroup { } public class MoreCommands : CommandGroup { } var services = new ServiceCollection().AddCommands(); // Registering commands for 'myothertree' from one location services.AddCommandTree("myothertree") .WithCommandGroup() .Finish(); // Registering additional commands for the same 'myothertree' from another location services.AddCommandTree("myothertree") .WithCommandGroup() .Finish(); var serviceProvider = services.BuildServiceProvider(); // The 'myothertree' will now contain both MyOtherCommands and MoreCommands. ``` -------------------------------- ### Cumulative Command Tree Registration Source: https://github.com/remora/remora.commands/blob/main/README.md Explains how `AddCommandTree` calls with the same name are cumulative, allowing command groups to be registered from multiple locations, such as different plugins or modules. ```cs using Remora.Commands; using Microsoft.Extensions.DependencyInjection; // Assume MyOtherCommands and MoreCommands are defined CommandGroup classes public class MyOtherCommands : CommandGroup { } public class MoreCommands : CommandGroup { } var services = new ServiceCollection().AddCommands(); // Registering commands for 'myothertree' from one location services.AddCommandTree("myothertree") .WithCommandGroup() .Finish(); // Registering additional commands for the same 'myothertree' from another location services.AddCommandTree("myothertree") .WithCommandGroup() .Finish(); var serviceProvider = services.BuildServiceProvider(); // The 'myothertree' will now contain both MyOtherCommands and MoreCommands. ``` -------------------------------- ### Remora.Commands Option Syntax Types Source: https://github.com/remora/remora.commands/blob/main/README.md Illustrates the different syntax types supported by Remora.Commands for defining command-line options, including positional, named (short and long), switches (short and long), collections, and constrained collections. ```text * Positional options - `T value` * Named options - `[Option('v')] T value` (short) - `[Option("value")] T value` (long) - `[Option('v', "value")] T value` (short and long) * Switches - `[Switch('e')] bool value = false` (short) - `[Switch("enable")] bool value = false` (long) - `[Switch('e', "enable")] bool value = false` (short and long) * Collections - `IEnumerable values` (positional) - `[Option('v', "values")] IEnumerable values` (named, as above) - `[Range(Min = 1, Max = 2)] IEnumerable values` (constrained) * Verbs ``` -------------------------------- ### Remora.Commands Option Syntax Types Source: https://github.com/remora/remora.commands/blob/main/Remora.Commands/README.md Illustrates the various syntax types supported by Remora.Commands for defining command-line options, including positional, named (short and long), switches, collections, and verbs. It highlights the use of attributes like Option, Switch, and Range. ```cs // Positional options T value // Named options [Option('v')] T value // short [Option("value")] T value // long [Option('v', "value")] T value // short and long // Switches [Switch('e')] bool value = false // short [Switch("enable")] bool value = false // long [Switch('e', "enable")] bool value = false // short and long // Collections IEnumerable values // positional [Option('v', "values")] IEnumerable values // named [Range(Min = 1, Max = 2)] IEnumerable values // constrained ``` -------------------------------- ### Nested and Merged Command Groups Source: https://github.com/remora/remora.commands/blob/main/Remora.Commands/README.md Demonstrates how to register command groups, including nested groups and merging groups with the same name. This structure defines the available commands and their hierarchical organization. ```cs using Remora.Commands; using Remora.Results; using Microsoft.Extensions.DependencyInjection; // Example Command Group Registration [Group("commands")] public class MyFirstGroup : CommandGroup { [Command("do-thing")] public Task MyCommand() { return Task.FromResult(Results.Success()); } } [Group("commands")] public class MySecondGroup : CommandGroup { [Group("subcommands") public class MyThirdGroup : CommandGroup { [Command("do-thing")] public Task MyCommand() { return Task.FromResult(Results.Success()); } } } // Example of registering command groups with Dependency Injection var services = new ServiceCollection() .AddCommands() .AddCommandTree() .WithCommandGroup() .WithCommandGroup() .Finish() .BuildServiceProvider(); // Expected command structure: // commands do-thing // commands subcommands do-thing ``` -------------------------------- ### Greedy Option Attribute Usage Source: https://github.com/remora/remora.commands/blob/main/README.md Explains and demonstrates the use of the `Greedy` attribute in Remora.Commands. This attribute allows a single option to consume multiple subsequent values, concatenating them with spaces, which can simplify input for certain scenarios. ```cs [Greedy] T value ``` -------------------------------- ### Greedy Option Attribute Source: https://github.com/remora/remora.commands/blob/main/Remora.Commands/README.md Explains and demonstrates the `Greedy` attribute in Remora.Commands, which allows options to consume multiple subsequent values as a single, concatenated string. This is useful for simplifying input and avoiding quotes. ```cs [Greedy] T value ``` -------------------------------- ### Nested and Merged Command Groups Source: https://github.com/remora/remora.commands/blob/main/README.md Demonstrates how to register command groups, including nested groups and merging groups with the same name. This structure defines the available commands and their hierarchical organization. ```cs using Remora.Commands; using Remora.Results; using Microsoft.Extensions.DependencyInjection; // Example Command Group Registration [Group("commands")] public class MyFirstGroup : CommandGroup { [Command("do-thing")] public Task MyCommand() { return Task.FromResult(Results.Success()); } } [Group("commands")] public class MySecondGroup : CommandGroup { [Group("subcommands") public class MyThirdGroup : CommandGroup { [Command("do-thing")] public Task MyCommand() { return Task.FromResult(Results.Success()); } } } // Example of registering command groups with Dependency Injection var services = new ServiceCollection() .AddCommands() .AddCommandTree() .WithCommandGroup() .WithCommandGroup() .Finish() .BuildServiceProvider(); // Expected command structure: // commands do-thing // commands subcommands do-thing ``` -------------------------------- ### Custom Type Parsing Source: https://github.com/remora/remora.commands/blob/main/README.md Illustrates how to create a custom type parser for Remora.Commands by inheriting from `AbstractTypeParser`. This allows the library to parse custom string formats into specific types. ```cs using Remora.Commands; using Remora.Results; using Microsoft.Extensions.DependencyInjection; using System.Threading; using System.Threading.Tasks; // Assume MyType and its TryParse method are defined elsewhere public class MyType { public static bool TryParse(string value, out MyType result) { result = new MyType(); return true; } } public class MyParser : AbstractTypeParser { public override ValueTask> TryParseAsync ( string value, CancellationToken ct ) { // Example parsing logic if (MyType.TryParse(value, out var result)) { return new ValueTask>(Result.FromSuccess(result)); } else { return new ValueTask>( Result.FromError($"Failed to parse \"{value}\" as an instance of MyType.")); } } } // Example of registering a custom parser with Dependency Injection var services = new ServiceCollection() .AddCommands() .AddCommandTree() .WithCommandGroup() // Assuming MyCommands is a defined CommandGroup .Finish() .AddSingletonParser() .BuildServiceProvider(); ``` -------------------------------- ### Custom Type Parsing Source: https://github.com/remora/remora.commands/blob/main/Remora.Commands/README.md Illustrates how to create a custom type parser for Remora.Commands by inheriting from `AbstractTypeParser`. This allows the library to parse custom string formats into specific types. ```cs using Remora.Commands; using Remora.Results; using Microsoft.Extensions.DependencyInjection; using System.Threading; using System.Threading.Tasks; // Assume MyType and its TryParse method are defined elsewhere public class MyType { public static bool TryParse(string value, out MyType result) { result = new MyType(); return true; } } public class MyParser : AbstractTypeParser { public override ValueTask> TryParseAsync ( string value, CancellationToken ct ) { // Example parsing logic if (MyType.TryParse(value, out var result)) { return new ValueTask>(Result.FromSuccess(result)); } else { return new ValueTask>( Result.FromError($"Failed to parse \"{value}\" as an instance of MyType.")); } } } // Example of registering a custom parser with Dependency Injection var services = new ServiceCollection() .AddCommands() .AddCommandTree() .WithCommandGroup() // Assuming MyCommands is a defined CommandGroup .Finish() .AddSingletonParser() .BuildServiceProvider(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.