public object?Resolve(Type?type)
        {
            if (type == null)
            {
                throw new CommandRuntimeException("Cannot resolve null type.");
            }

            try
            {
                if (_resolver != null)
                {
                    var obj = _resolver.Resolve(type);
                    if (obj == null)
                    {
                        throw CommandRuntimeException.CouldNotResolveType(type);
                    }

                    return(obj);
                }

                // Fall back to use the activator.
                return(Activator.CreateInstance(type));
            }
            catch (CommandAppException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw CommandRuntimeException.CouldNotResolveType(type, ex);
            }
        }
        public static ConfiguredCommand FromType <TCommand>(string name, bool isDefaultCommand = false)
            where TCommand : class, ICommand
        {
            var settingsType = ConfigurationHelper.GetSettingsType(typeof(TCommand));

            if (settingsType == null)
            {
                throw CommandRuntimeException.CouldNotGetSettingsType(typeof(TCommand));
            }

            return(new ConfiguredCommand(name, typeof(TCommand), settingsType, null, isDefaultCommand));
        }
        public static CommandSettings CreateSettings(CommandValueLookup lookup, ConstructorInfo constructor, ITypeResolver resolver)
        {
            if (constructor.DeclaringType == null)
            {
                throw new InvalidOperationException("Cannot create settings since constructor have no declaring type.");
            }

            var parameters = new List <object?>();
            var mapped     = new HashSet <Guid>();

            foreach (var parameter in constructor.GetParameters())
            {
                if (lookup.TryGetParameterWithName(parameter.Name, out var result))
                {
                    parameters.Add(result.Value);
                    mapped.Add(result.Parameter.Id);
                }
                else
                {
                    var value = resolver.Resolve(parameter.ParameterType);
                    if (value == null)
                    {
                        throw CommandRuntimeException.CouldNotResolveType(parameter.ParameterType);
                    }

                    parameters.Add(value);
                }
            }

            // Create the settings.
            if (!(Activator.CreateInstance(constructor.DeclaringType, parameters.ToArray()) is CommandSettings settings))
            {
                throw new InvalidOperationException("Could not create settings");
            }

            // Try to do property injection for parameters that wasn't injected.
            foreach (var(parameter, value) in lookup)
            {
                if (!mapped.Contains(parameter.Id) && parameter.Property.SetMethod != null)
                {
                    parameter.Property.SetValue(settings, value);
                }
            }

            return(settings);
        }
        public static CommandSettings CreateSettings(CommandValueLookup lookup, Type settingsType, ITypeResolver resolver)
        {
            var settings = CreateSettings(resolver, settingsType);

            foreach (var (parameter, value) in lookup)
            {
                parameter.Property.SetValue(settings, value);
            }

            // Validate the settings.
            var validationResult = settings.Validate();
            if (!validationResult.Successful)
            {
                throw CommandRuntimeException.ValidationFailed(validationResult);
            }

            return settings;
        }
        private static Task <int> Execute(
            CommandTree leaf,
            CommandTree tree,
            CommandContext context,
            ITypeResolver resolver,
            IConfiguration configuration)
        {
            // Bind the command tree against the settings.
            var settings = CommandBinder.Bind(tree, leaf.Command.SettingsType, resolver);

            configuration.Settings.Interceptor?.Intercept(context, settings);

            // Create and validate the command.
            var command          = leaf.CreateCommand(resolver);
            var validationResult = command.Validate(context, settings);

            if (!validationResult.Successful)
            {
                throw CommandRuntimeException.ValidationFailed(validationResult);
            }

            // Execute the command.
            return(command.Execute(context, settings));
        }
        public static CommandValueLookup GetParameterValues(CommandTree?tree, ITypeResolver resolver)
        {
            var lookup = new CommandValueLookup();
            var binder = new CommandValueBinder(lookup);

            CommandValidator.ValidateRequiredParameters(tree);

            while (tree != null)
            {
                // Process unmapped parameters.
                foreach (var parameter in tree.Unmapped)
                {
                    if (parameter.IsFlagValue())
                    {
                        // Set the flag value to an empty, not set instance.
                        var instance = Activator.CreateInstance(parameter.ParameterType);
                        lookup.SetValue(parameter, instance);
                    }
                    else
                    {
                        // Is this an option with a default value?
                        if (parameter.DefaultValue != null)
                        {
                            var value = parameter.DefaultValue?.Value;

                            // Need to convert the default value?
                            if (value != null && value.GetType() != parameter.ParameterType)
                            {
                                var converter = GetConverter(lookup, binder, resolver, parameter);
                                if (converter != null)
                                {
                                    value = converter.ConvertFrom(value);
                                }
                            }

                            binder.Bind(parameter, resolver, value);
                            CommandValidator.ValidateParameter(parameter, lookup);
                        }
                        else if (Nullable.GetUnderlyingType(parameter.ParameterType) != null ||
                                 !parameter.ParameterType.IsValueType)
                        {
                            lookup.SetValue(parameter, null);
                        }
                    }
                }

                // Process mapped parameters.
                foreach (var mapped in tree.Mapped)
                {
                    if (mapped.Parameter.WantRawValue)
                    {
                        // Just try to assign the raw value.
                        binder.Bind(mapped.Parameter, resolver, mapped.Value);
                    }
                    else
                    {
                        var converter = GetConverter(lookup, binder, resolver, mapped.Parameter);
                        if (converter == null)
                        {
                            throw CommandRuntimeException.NoConverterFound(mapped.Parameter);
                        }

                        if (mapped.Parameter.IsFlagValue() && mapped.Value == null)
                        {
                            if (mapped.Parameter is CommandOption option && option.DefaultValue != null)
                            {
                                // Set the default value.
                                binder.Bind(mapped.Parameter, resolver, option.DefaultValue?.Value);
                            }
                            else
                            {
                                // Set the flag but not the value.
                                binder.Bind(mapped.Parameter, resolver, null);
                            }
                        }