|
| 1 | +# Commands |
| 2 | + |
| 3 | +Commands are the fundamental abstraction used to run custom logic. |
| 4 | + |
| 5 | +## Create a Command |
| 6 | + |
| 7 | +The InEngine.Core package is required. Install it into your own Visual Studio project. |
| 8 | + |
| 9 | +**Package Manager** |
| 10 | +```bash |
| 11 | +Install-Package InEngine.Core |
| 12 | +``` |
| 13 | + |
| 14 | +**Nuget CLI** |
| 15 | +```bash |
| 16 | +nuget install InEgine.Core |
| 17 | +``` |
| 18 | + |
| 19 | +**.NET CLI** |
| 20 | +```bash |
| 21 | +dotnet add package InEngine.Core |
| 22 | +``` |
| 23 | + |
| 24 | +**Paket CLI** |
| 25 | +```bash |
| 26 | +paket add InEngine.Core |
| 27 | +``` |
| 28 | + |
| 29 | +Adding a class that implements **InEngine.Core.ICommand** is the simplest way to create a command. |
| 30 | + |
| 31 | +```csharp |
| 32 | +using System; |
| 33 | +using InEngine.Core; |
| 34 | + |
| 35 | +namespace MyCommandPlugin |
| 36 | +{ |
| 37 | + public class MyCommand : ICommand |
| 38 | + { |
| 39 | + public CommandResult Run() |
| 40 | + { |
| 41 | + Console.WriteLine("Hello, world!"); |
| 42 | + return new CommandResult(true); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Extending the **InEngine.Core.AbstractCommand** class adds extra functionality, like a logger, a progress bar, and the ability to schedule the command using the scheduler. |
| 49 | +Minimally, the Run method should be overridden. |
| 50 | + |
| 51 | +```csharp |
| 52 | +using System; |
| 53 | +using InEngine.Core; |
| 54 | + |
| 55 | +namespace MyCommandPlugin |
| 56 | +{ |
| 57 | + public class MyCommand : ICommand |
| 58 | + { |
| 59 | + public override CommandResult Run() |
| 60 | + { |
| 61 | + Console.WriteLine("Hello, world!"); |
| 62 | + return new CommandResult(true); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +## Run a Command |
| 69 | + |
| 70 | +Create a class that implements **InEngine.Core.IOptions** in the same assembly as the command class. |
| 71 | +Add a VerbOptions attribute from the CommandLine namespace that defines the name of the command and optional help text. |
| 72 | +The help text can be auto-generated from the attribute or manually specified in the GetUsage method. |
| 73 | + |
| 74 | +```csharp |
| 75 | +using CommandLine; |
| 76 | +using CommandLine.Text; |
| 77 | +using InEngine.Core; |
| 78 | + |
| 79 | +namespace MyCommandPlugin |
| 80 | +{ |
| 81 | + public class MyOptions : IOptions |
| 82 | + { |
| 83 | + [VerbOption("my-command", HelpText="My example command.")] |
| 84 | + public MyCommand MyCommand { get; set; } |
| 85 | + |
| 86 | + [HelpVerbOption] |
| 87 | + public string GetUsage(string verb) |
| 88 | + { |
| 89 | + return HelpText.AutoBuild(this, verb); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +Download the InEngineCli tool that matches the version of the InEngine.Core package you included from the [GitHub Releases](https://github.com/InEngine-NET/InEngine.NET/releases) page. |
| 96 | + |
| 97 | +Copy your project's DLLs into the same directory as InEngineCli.exe. |
| 98 | + |
| 99 | +Run your command... |
| 100 | + |
| 101 | +```bash |
| 102 | +InEngineCli.exe -pMyCommandPlugin my-command |
| 103 | +``` |
| 104 | + |
| 105 | +## Discover Commands Plugins |
| 106 | + |
| 107 | +Run InEngineCli.exe without any arguments to see a list of arguments. |
| 108 | + |
| 109 | +``` |
| 110 | +Available plugins... |
| 111 | +InEngine.Commands |
| 112 | +InEngine.Core |
| 113 | +``` |
| 114 | + |
| 115 | +## Discover Commands in a Plugin |
| 116 | + |
| 117 | +Run InEngineCli.exe with only the plugin specified. |
| 118 | + |
| 119 | +``` |
| 120 | +InEngineCli.exe -pInEngine.Core |
| 121 | +``` |
| 122 | + |
| 123 | +The **InEngine.Core** library is itself a plugin that contains queue related commands. |
| 124 | +This is the help output for the core plugin. |
| 125 | + |
| 126 | +``` |
| 127 | +InEngine 3.1.0 |
| 128 | +Copyright © Ethan Hann 2017 |
| 129 | +
|
| 130 | + queue:publish Publish a command message to a queue. |
| 131 | +
|
| 132 | + queue:consume Consume one or more command messages from the queue. |
| 133 | +
|
| 134 | + queue:length Get the number of messages in the primary and secondary |
| 135 | + queues. |
| 136 | +
|
| 137 | + queue:clear Clear the primary and secondary queues. |
| 138 | +``` |
| 139 | + |
| 140 | +## Print Help Text for a Plugin's Commands |
| 141 | + |
| 142 | +Run the command with the -h or --help arguments. |
| 143 | + |
| 144 | +``` |
| 145 | +InEngineCli.exe -pInEngine.Core queue:clear -h |
| 146 | +``` |
| 147 | + |
| 148 | +The **InEngine.Core** plugin's command to clear the InEngine.NET's queues produces this help message. |
| 149 | + |
| 150 | +``` |
| 151 | +InEngine 3.1.0 |
| 152 | +Copyright © Ethan Hann 2017 |
| 153 | +
|
| 154 | + --processing-queue Clear the processing queue. |
| 155 | +
|
| 156 | + --secondary Clear the secondary queue. |
| 157 | +``` |
| 158 | + |
0 commit comments