Пример #1
0
        private static string[] GetAliases(CommandLineAttribute attribute)
        {
            if (attribute != null)
            {
                return(attribute.Aliases);
            }

            return(new string[0]);
        }
Пример #2
0
        protected ParameterInfo([NotNull] PropertyInfo propertyInfo, [NotNull] CommandLineAttribute commandLineAttribute)
        {
            PropertyInfo         = propertyInfo ?? throw new ArgumentNullException(nameof(propertyInfo));
            CommandLineAttribute = commandLineAttribute ?? throw new ArgumentNullException(nameof(commandLineAttribute));

            ParameterType = propertyInfo.PropertyType;
            Identifiers   = commandLineAttribute.GetIdentifiers().ToArray();
            ParameterName = commandLineAttribute.Name ?? PropertyInfo.Name;
            Index         = commandLineAttribute.GetIndex();
        }
Пример #3
0
        private static string GetArgumentName(PropertyInfo info, CommandLineAttribute commandLineAttribute)
        {
            string primaryName = info.Name;

            if (commandLineAttribute?.Name != null)
            {
                return(commandLineAttribute.Name.ToLowerInvariant());
            }

            return(primaryName.ToLowerInvariant());
        }
        public virtual void PrintPropertyHelp(PropertyInfo property)
        {
            propertyInfo              = property;
            commandLineAttribute      = propertyInfo.GetAttribute <CommandLineAttribute>();
            detailedHelpTextAttribute = propertyInfo.GetAttribute <DetailedHelpTextAttribute>();
            helpTextAttribute         = propertyInfo.GetAttribute <HelpTextAttribute>();

            WriteHeader();
            WriteContent();
            WriteFooter();
        }
Пример #5
0
 private bool IsRequired(CommandLineAttribute commandLineAttribute)
 {
     return(commandLineAttribute is ArgumentAttribute attribute && attribute.Required);
 }
Пример #6
0
        internal static bool SetArgumentValue <T>(T instance, MappingInfo mappingInfo, IDictionary <string, CommandLineArgument> arguments)
        {
            PropertyInfo         propertyInfo = mappingInfo.PropertyInfo;
            CommandLineAttribute attribute    = mappingInfo.CommandLineAttribute;
            var  count = 0;
            bool trim  = attribute.TrimQuotation();

            CommandLineArgument argument;
            string stringValue;

            foreach (var name in mappingInfo.GetNames())
            {
                if (arguments.TryGetValue(name, out argument))
                {
                    if (argument.Value == null)
                    {
                        throw new CommandLineArgumentException($"The value of the argument '{argument.Name}' was not specified.")
                              {
                                  Reason = ErrorReason.ArgumentWithoutValue
                              }
                    }
                    ;

                    stringValue = GetValue(argument.Value, trim);
                    propertyInfo.SetValue(instance, ConvertValue(propertyInfo.PropertyType, stringValue, (t, v) => CreateErrorMessage(t, v, name)), null);
                    mappingInfo.CommandLineArgument = argument;
                    argument.Mapped = true;

                    if (!mappingInfo.IsShared())
                    {
                        arguments.Remove(name);
                    }

                    count++;
                }
            }

            if (count == 0 && TryGetByIndex(arguments, mappingInfo, out var entry))
            {
                argument = entry.Value;
                if (argument != null && argument.Value == null)
                {
                    stringValue = GetValue(argument.Name, trim);

                    propertyInfo.SetValue(instance, ConvertValue(propertyInfo.PropertyType, stringValue, (t, v) => CreateErrorMessage(t, v, attribute.Name)), null);
                    mappingInfo.CommandLineArgument = argument;
                    arguments.Remove(entry.Key);
                    count++;
                }
            }

            if (count > 1)
            {
                throw new AmbiguousCommandLineArgumentsException($"The value for the argument '{mappingInfo.Name}' was specified multiple times.");
            }

            if (count == 0 && attribute.IsRequired())
            {
                throw new MissingCommandLineArgumentException(mappingInfo.Name);
            }

            return(count > 0);
        }
Пример #7
0
 public static bool TrimQuotation(this CommandLineAttribute attribute)
 {
     return(attribute is ArgumentAttribute argumentAttribute && argumentAttribute.TrimQuotation);
 }
Пример #8
0
 public static int GetIndex(this CommandLineAttribute attribute)
 {
     return(attribute is ArgumentAttribute argumentAttribute ? argumentAttribute.Index : -1);
 }
Пример #9
0
 public static bool IsRequired(this CommandLineAttribute attribute)
 {
     return(attribute is ArgumentAttribute argumentAttribute && argumentAttribute.Required);
 }
Пример #10
0
 internal MappingInfo([NotNull] PropertyInfo propertyInfo, [NotNull] CommandLineAttribute commandLineAttribute, [NotNull] MappingList mappingList)
 {
     this.mappingList     = mappingList ?? throw new ArgumentNullException(nameof(mappingList));
     PropertyInfo         = propertyInfo ?? throw new ArgumentNullException(nameof(propertyInfo));
     CommandLineAttribute = commandLineAttribute ?? throw new ArgumentNullException(nameof(commandLineAttribute));
 }