예제 #1
0
        private void Map <TBag>(TBag bag, ICommandLine commandLine, CommandParameter parameter) where TBag : ICommandBag, new()
        {
            if (commandLine.TryGetArgumentValues(parameter.Id, parameter.Position, out var values))
            {
                if (parameter.Type.IsEnumerableOfT(except: typeof(string)))
                {
                    if (!values.Any())
                    {
                        throw DynamicException.Factory.CreateDynamicException(
                                  "EmptyCollection",
                                  "Collection parameter and must have at least one value."
                                  );
                    }

                    var value = _converter.Convert(values, parameter.Type);
                    parameter.SetValue(bag, value);
                }
                else
                {
                    if (values.Count > 1)
                    {
                        throw DynamicException.Create(
                                  "TooManyValues",
                                  "Simple parameter must have exactly one value."
                                  );
                    }

                    if (parameter.Type == typeof(bool))
                    {
                        if (values.Any())
                        {
                            var value = _converter.Convert(values.Single(), typeof(bool));
                            parameter.SetValue(bag, value);
                        }
                        else
                        {
                            if (parameter.DefaultValue is bool defaultValue)
                            {
                                parameter.SetValue(bag, !defaultValue);
                            }
                            else
                            {
                                // Without a DefaultValue assume false but using the parameter negates it so use true.
                                parameter.SetValue(bag, true);
                            }
                        }
                    }
                    else
                    {
                        var value = _converter.Convert(values.Single(), parameter.Type);
                        parameter.SetValue(bag, value);
                    }
                }
            }
            else
            {
                if (parameter.IsRequired)
                {
                    throw DynamicException.Factory.CreateDynamicException(
                              "MissingValue",
                              "Required parameter must specify a value."
                              );
                }

                if (parameter.DefaultValue.IsNotNull())
                {
                    var value =
                        parameter.DefaultValue is string
                        ?_converter.Convert(parameter.DefaultValue, parameter.Type)
                            : parameter.DefaultValue;

                    parameter.SetValue(bag, value);
                }
            }
        }