private ClassCommandDef(Type classType, CommandContext commandContext) { CommandHostClassType = classType ?? throw new ArgumentNullException(nameof(classType)); _commandContext = commandContext ?? throw new ArgumentNullException(nameof(commandContext)); Name = classType.BuildName(CommandNodeType.Command, commandContext.AppConfig); var(interceptorMethod, defaultCommand, localCommands) = ParseMethods(commandContext.AppConfig); InterceptorMethodDef = interceptorMethod; _defaultCommandDef = defaultCommand; // lazy loading prevents walking the entire hierarchy of sub-commands _subCommands = new Lazy <List <ICommandDef> >(() => GetSubCommands(localCommands)); }
internal static ICommandBuilder ToCommand(this ICommandDef commandDef, Command?parent, CommandContext commandContext) { var command = new Command( commandDef.Name, commandDef.CustomAttributes, commandDef.IsExecutable, parent, commandDef.SourcePath); command.Services.AddOrUpdate(commandDef); var commandAttribute = commandDef.GetCustomAttribute <CommandAttribute>(); if (commandAttribute != null) { command.Description = commandAttribute.Description; command.Usage = commandAttribute.Usage; command.ExtendedHelpText = commandAttribute.ExtendedHelpText; } var appSettings = commandContext.AppConfig.AppSettings; command.IgnoreUnexpectedOperands = commandAttribute?.IgnoreUnexpectedOperandsAsNullable ?? appSettings.IgnoreUnexpectedOperands; command.ArgumentSeparatorStrategy = commandAttribute?.ArgumentSeparatorStrategyAsNullable ?? appSettings.DefaultArgumentSeparatorStrategy; var commandBuilder = new CommandBuilder(command); commandDef.InvokeMethodDef?.ArgumentDefs .Select(a => a.ToArgument(commandContext.AppConfig, false)) .ForEach(commandBuilder.AddArgument); commandDef.InterceptorMethodDef?.ArgumentDefs .Select(a => a.ToArgument(commandContext.AppConfig, true)) .ForEach(commandBuilder.AddArgument); commandDef.SubCommands .Select(c => c.ToCommand(command, commandContext).Command) .ForEach(commandBuilder.AddSubCommand); commandContext.AppConfig.BuildEvents.CommandCreated(commandContext, commandBuilder); return(commandBuilder); }
internal static ICommandBuilder ToCommand(this ICommandDef commandDef, Command parent, CommandContext commandContext) { var command = new Command( commandDef.Name, commandDef.CustomAttributes, commandDef.IsExecutable, parent, commandDef.SourcePath); command.Services.AddOrUpdate(commandDef); var commandAttribute = commandDef.CustomAttributes.GetCustomAttribute <CommandAttribute>() ?? commandDef.CustomAttributes.GetCustomAttribute <ApplicationMetadataAttribute>(); if (commandAttribute != null) { command.Description = commandAttribute.Description; command.Usage = commandAttribute.Usage; command.ExtendedHelpText = commandAttribute.ExtendedHelpText; } var commandBuilder = new CommandBuilder(command); commandDef.InvokeMethodDef.ArgumentDefs .Select(a => a.ToArgument(command, commandContext.AppConfig, false)) .ForEach(commandBuilder.AddArgument); commandDef.InterceptorMethodDef.ArgumentDefs .Select(a => a.ToArgument(command, commandContext.AppConfig, true)) .ForEach(commandBuilder.AddArgument); commandDef.SubCommands .Select(c => c.ToCommand(command, commandContext).Command) .ForEach(commandBuilder.AddSubCommand); commandContext.AppConfig.BuildEvents.CommandCreated(commandContext, commandBuilder); return(commandBuilder); }