{
private readonly Parser _parser;
private readonly Command _rootCommand;
-
- /// <summary>
- /// Domain specific context passed to commands
- /// </summary>
- public object CommandContext { get; set; }
+ private readonly Dictionary<Type, object> _services = new Dictionary<Type, object>();
/// <summary>
/// Create an instance of the command processor;
/// <param name="assemblies">The list of assemblies to look for commands</param>
public CommandProcessor(IEnumerable<Assembly> assemblies)
{
+ _services.Add(typeof(CommandProcessor), this);
var rootBuilder = new CommandLineBuilder();
rootBuilder.UseHelp()
.UseParseDirective()
_parser = rootBuilder.Build();
}
+ /// <summary>
+ /// Adds a service or context to inject into an command.
+ /// </summary>
+ /// <typeparam name="T">type of service</typeparam>
+ /// <param name="instance">service instance</param>
+ public void AddService<T>(T instance)
+ {
+ AddService(typeof(T), instance);
+ }
+
+ /// <summary>
+ /// Adds a service or context to inject into an command.
+ /// </summary>
+ /// <param name="type">service type</param>
+ /// <param name="instance">service instance</param>
+ public void AddService(Type type, object instance)
+ {
+ _services.Add(type, instance);
+ }
+
/// <summary>
/// Parse the command line.
/// </summary>
{
IEnumerable<OptionResult> optionResults = context.ParseResult.CommandResult.Children.OfType<OptionResult>();
- foreach (var property in _properties)
+ foreach ((PropertyInfo Property, Option Option) property in _properties)
{
object value = property.Property.GetValue(instance);
else if (propertyType == typeof(IConsole)) {
value = context.Console;
}
- else if (propertyType == typeof(CommandProcessor)) {
- value = _commandProcessor;
- }
- else if (propertyType == _commandProcessor.CommandContext?.GetType()) {
- value = _commandProcessor.CommandContext;
+ else if (_commandProcessor._services.TryGetValue(propertyType, out object service)) {
+ value = service;
}
else if (property.Option != null)
{
{
_consoleProvider = new ConsoleProvider();
_commandProcessor = new CommandProcessor(new Assembly[] { typeof(Analyzer).Assembly });
+ _commandProcessor.AddService(_consoleProvider);
}
public async Task<int> Analyze(FileInfo dump_path, string[] command)
_consoleProvider.Out.WriteLine("Type 'quit' or 'exit' to exit the session.");
// Create common analyze context for commands
- var analyzeContext = new AnalyzeContext(_consoleProvider, target, _consoleProvider.Stop) {
+ var analyzeContext = new AnalyzeContext(_consoleProvider, target) {
CurrentThreadId = unchecked((int)target.DataReader.EnumerateAllThreads().FirstOrDefault())
};
- _commandProcessor.CommandContext = analyzeContext;
+ _commandProcessor.AddService(analyzeContext);
// Automatically enable symbol server support on Linux and MacOS
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {