Skip to content

Latest commit

 

History

History
117 lines (97 loc) · 3.85 KB

README.md

File metadata and controls

117 lines (97 loc) · 3.85 KB

CommandRunner

A simple command runner to enable quick command line (developer) tools

demo

Simple Usage

static void Main(string[] args)
{
	RunnerConfiguration configuration = new RunnerConfiguration("Example Runner");
	Runner.Start(configuration);
}

public class EchoCommand 
{

	[Command("echo", "Echo back anything following the command.")]
	public void Execute(List<string> args)
	{
		foreach (var arg in args) Console.WriteLine(arg);
	}
}

[NestedCommand("nest")]
public class NestingCommand
{
	[Command("hello", "Say hello")]
	public void Hello()
	{
		Console.WriteLine("Hello");
	}
}

The library accepts typed parameters and is able to easily setup a quick command line tool. The library will try to map the arguments if it sees typed arguments. To prevent this, accept a list of strings in your method to parse it yourself, or no parameter at all if nothing is needed.

Attributes used to setup your runner(make sure to check the CoreconsoleTest app for examples):

[Command(string:identifier, string:help, bool:moveUpAfterSuccessfulExecution)]

This attribute (used on methods) signals the runner it can run this method using the identifier. Can be used in a NestedCommand and a NavigatableCommand

[NestedCommand("pre-identifier", "help")]

This attribute, used on a class is more for easy prepending of all commands. All methods in the class that use the Command attribute will have their identifier prepended like: '{NestedIdentifier} {CommandIdentifier}'.

[NavigatableCommand("identifier", "help")]

This attribute, used on a class, signals a menu that can be used to navigate into. Child items won't be visible until navigated upon. If there is a multi-layer menu, the parent Menu class will be set on the child if there is a property of that type.

[NavigatableCommandAnnouncement]

This attribute, used on a method, will be called in the terminal mode instead of displaying the identifier when navigated into the command.

[NavigatableCommandInitialization]

This attribute, used on a method, will be called when navigating to a command. This can be used to setup the menu environment, for example an account menu with a specific account id to then only execute every method on this instance. The Initialize method is able to accept typed parameters like other command methods.

Settings

Using custom creator

private static void RunWithAutofacCreator()
{
	var container = CreateContainer();
	var container = CreateContainer();
	RunnerConfiguration configuration = new RunnerConfiguration("Example Runner");
	configuration.UseCommandActivator(type => container.Resolve(type));
	Runner.Start(configuration);
}

private static IContainer CreateContainer()
{
	var builder = new ContainerBuilder();
	builder.RegisterType<EchoCommand>().PropertiesAutowired();
	builder.RegisterType<Injectable>().PropertiesAutowired();
	builder.RegisterType<NestingCommand>().PropertiesAutowired();
	return builder.Build();
}

Using provided assemblies

configuration.ScanAssemblies(new List<Assembly>() {typeof(EchoCommand).GetTypeInfo().Assembly});

Using provided types

configuration.ScanTypes(new List<Type>() {typeof(EchoCommand), typeof(AccountMenu)});

Setting specific colors:

configuration.UseTerminalColor(ConsoleColor.DarkGreen);
configuration.UseCommandColor(ConsoleColor.Yellow);

Forcing mode

configuration.ForceTerminal();
configuration.ForceCommandLine();

Provide arguments (only applicable for command line mode)

configuration.UseArguments(new List<string>() {"test", "different", "arguments"});

Provide types to scan. These will be scanned for public methods and identified by PascalCasing or camelCasing splitting

configuration.AddTypes(new List<Type>() { typeof(ExamplePublic) }, true);