Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/scott-the-programmer/cli.commandhandler

An easy way to build map command models to their corresponding handlers
https://github.com/scott-the-programmer/cli.commandhandler

Last synced: 16 days ago
JSON representation

An easy way to build map command models to their corresponding handlers

Awesome Lists containing this project

README

        

# CLI.CommandHandler


This is currently a WIP

## Intent

This project aims to automatically map cli command models to corresponding command handlers. The overall goal is to eliminate manual command line arg parsing, and allow developers to focus on the logic.

At its current state, this project is highly coupled to https://github.com/commandlineparser/commandline

## Example

### Command Model

```csharp
[Verb("make-noise")]
public class MakeNoise : ICommand
{
[Option('n', "noise", Required = true, HelpText = "Noise to emit")]
public string Noise { get; set; }
}
```

### Command Handler

```csharp
public class MakeNoiseCommand : ICommandHandler
{
public Task HandleAsync(MakeNoise command)
{
Console.WriteLine(command.Noise)
return Task.CompletedTask;
}
}
```

### Program.cs

```csharp
public static void Main(string[] args)
{
var types = LoadRunCommands(); //TODO: Define this a bit better

Parser.Default.ParseArguments(args, types)
.WithParsed(Run);

Console.ReadLine();
}

public static void Run(object command)
{
var commandDispatcher = new CommandDispatcher(Assembly.GetExecutingAssembly().GetName());
commandDispatcher.DispatchAsync(command) //Run the MakeNoise command
}
```