-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
53 lines (49 loc) · 1.63 KB
/
Program.cs
File metadata and controls
53 lines (49 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.IO;
using Ninject;
using Ninject.Extensions.Conventions;
using Ninject.Modules;
namespace CommandLineTool
{
public class CommandsModule : NinjectModule
{
public override void Load()
{
Kernel.Bind(f =>
{
var commandTypes = f.FromThisAssembly().Select(typeof(ConsoleCommand).IsAssignableFrom);
commandTypes.BindAllBaseClasses().Configure(c => c.InSingletonScope());
});
Kernel.Bind<ICommandsExecutor>().To<CommandsExecutor>().InSingletonScope();
Kernel.Bind<TextWriter>().To<PromptConsoleWriter>()
.WhenInjectedInto<ConsoleCommand>().InSingletonScope();
Kernel.Bind<TextWriter>().To<RedTextConsoleWriter>()
.WhenInjectedInto<CommandsExecutor>().InSingletonScope();
}
}
public class Program
{
private static ICommandsExecutor CreateExecutor()
{
var container = new StandardKernel(new CommandsModule());
return container.Get<ICommandsExecutor>();
}
static void Main(string[] args)
{
ICommandsExecutor executor = CreateExecutor();
if (args.Length > 0)
executor.Execute(args);
else
RunInteractiveMode(executor);
}
public static void RunInteractiveMode(ICommandsExecutor executor)
{
while (true)
{
var line = Console.ReadLine();
if (line == null || line == "exit") return;
executor.Execute(line.Split(' '));
}
}
}
}