/// <summary> /// Create a new instance of a command. /// </summary> /// <param name="commandInput">The <see cref="CommandInput" /> containing the details of the command to instantiate.</param> public ICommand Create(CommandInput commandInput) { if (commandInput == null) { throw new ArgumentNullException(nameof(commandInput)); } var descriptor = _commandRegistry.Resolve(commandInput.CommandName); if (descriptor == null) { return(null); } var type = descriptor.ImplementingType; var constructor = type.GetConstructors().FirstOrDefault(); var parameters = constructor?.GetParameters(); object commandInstance = null; if (constructor == null || !parameters.Any()) { commandInstance = Activator.CreateInstance(type); } else { var parameterValues = ResolveParameters(parameters); commandInstance = Activator.CreateInstance(type, parameterValues); } return(commandInstance as ICommand); }
private bool DetailCommand(StringBuilder output) { var command = _commandRegistry.Resolve(CommandName); if (command == null) { return(false); } output.Append(_phraseDictionary.GetPhrase(Texts.PhraseKeys.Usage, _executionCultureName)); output.Append(": "); output.Append(command.CommandName); foreach (var numberedParameter in command.NumberedParameters) { output.Append(" "); AppendParameterUsage(numberedParameter.PlaceholderText, numberedParameter.Required, output); } foreach (var namedParameter in command.NamedParameters) { output.Append(" "); AppendNamedParameterUsage(namedParameter.Value.Name, namedParameter.Value.ValuePlaceholderText, namedParameter.Value.Required, output); } foreach (var flagParameter in command.FlagParameters) { output.Append(" "); AppendParameterUsage($"-{flagParameter.Name}", false, output); } output.Append(Environment.NewLine); output.AppendLine(command.GetDescription(_executionCultureName)); output.Append(Environment.NewLine); foreach (var numberedParameter in command.NumberedParameters) { var description = numberedParameter.GetDescription(_executionCultureName); AppendParameterDetail(numberedParameter.PlaceholderText, description, numberedParameter.Required, _executionCultureName, output); } foreach (var namedParameter in command.NamedParameters) { var description = namedParameter.Value.GetDescription(_executionCultureName); var segment = $"-{namedParameter.Value.Name} <{namedParameter.Value.ValuePlaceholderText}>"; AppendParameterDetail(segment, description, namedParameter.Value.Required, _executionCultureName, output); } foreach (var flagParameter in command.FlagParameters) { var description = flagParameter.GetDescription(_executionCultureName); AppendParameterDetail($"-{flagParameter.Name}", description, false, _executionCultureName, output); } return(true); }
public ParameterBindResult Bind(ICommand instance, CommandInput input) { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (input == null) { throw new ArgumentNullException(nameof(input)); } var result = new ParameterBindResult(); var descriptor = _commandRegistry.Resolve(input.CommandName); if (descriptor == null) { throw new InvalidOperationException(string.Format(Texts.DescriptorForCommandCouldNotBeResolved, input.CommandName)); } var parameters = new List <CommandParameter>(input.Parameters); BindFlagParameters(instance, descriptor, parameters, result); BindNamedParameters(instance, descriptor, parameters, result); AssertNoNamedOrFlagParameters(parameters, result); BindNumberedParameters(instance, descriptor, parameters, result); // Anything left over was unexpected foreach (var parameter in parameters) { result.AddError(string.Format(Texts.UnexpectedNumberedParameter, parameter)); } return(result); }