项目作者: janpieterz

项目描述 :
A simple command runner to enable quick command line developer tools
高级语言: C#
项目地址: git://github.com/janpieterz/CommandRunner.git
创建时间: 2015-12-03T09:38:56Z
项目社区:https://github.com/janpieterz/CommandRunner

开源协议:MIT License

下载


CommandRunner

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

demo

Simple Usage

  1. static void Main(string[] args)
  2. {
  3. RunnerConfiguration configuration = new RunnerConfiguration("Example Runner");
  4. Runner.Start(configuration);
  5. }
  6. public class EchoCommand
  7. {
  8. [Command("echo", "Echo back anything following the command.")]
  9. public void Execute(List<string> args)
  10. {
  11. foreach (var arg in args) Console.WriteLine(arg);
  12. }
  13. }
  14. [NestedCommand("nest")]
  15. public class NestingCommand
  16. {
  17. [Command("hello", "Say hello")]
  18. public void Hello()
  19. {
  20. Console.WriteLine("Hello");
  21. }
  22. }

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):

  1. [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

  1. [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}’.

  1. [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.

  1. [NavigatableCommandAnnouncement]

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

  1. [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

  1. private static void RunWithAutofacCreator()
  2. {
  3. var container = CreateContainer();
  4. var container = CreateContainer();
  5. RunnerConfiguration configuration = new RunnerConfiguration("Example Runner");
  6. configuration.UseCommandActivator(type => container.Resolve(type));
  7. Runner.Start(configuration);
  8. }
  9. private static IContainer CreateContainer()
  10. {
  11. var builder = new ContainerBuilder();
  12. builder.RegisterType<EchoCommand>().PropertiesAutowired();
  13. builder.RegisterType<Injectable>().PropertiesAutowired();
  14. builder.RegisterType<NestingCommand>().PropertiesAutowired();
  15. return builder.Build();
  16. }

Using provided assemblies

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

Using provided types

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

Setting specific colors:

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

Forcing mode

  1. configuration.ForceTerminal();
  2. configuration.ForceCommandLine();

Provide arguments (only applicable for command line mode)

  1. 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

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