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

            return(new string[0]);
        }
Пример #2
0
        public virtual void PrintPropertyHelp(PropertyInfo property)
        {
            propertyInfo              = property;
            commandLineAttribute      = propertyInfo.GetAttribute <CommandLineAttribute>();
            detailedHelpTextAttribute = propertyInfo.GetAttribute <DetailedHelpTextAttribute>();
            helpTextAttribute         = propertyInfo.GetAttribute <HelpTextAttribute>();

            WriteHeader();
            WriteContent();
            WriteFooter();
        }
Пример #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());
        }
Пример #4
0
        protected ParameterInfo([NotNull] PropertyInfo propertyInfo, [NotNull] CommandLineAttribute commandLineAttribute)
        {
            if (propertyInfo == null)
            {
                throw new ArgumentNullException(nameof(propertyInfo));
            }
            if (commandLineAttribute == null)
            {
                throw new ArgumentNullException(nameof(commandLineAttribute));
            }

            PropertyInfo         = propertyInfo;
            ParameterType        = propertyInfo.PropertyType;
            CommandLineAttribute = commandLineAttribute;
            Identifiers          = commandLineAttribute.GetIdentifiers().ToArray();
            ParameterName        = commandLineAttribute.Name;
        }
Пример #5
0
 public static bool TrimQuotation(this CommandLineAttribute attribute)
 {
     return(attribute is ArgumentAttribute argumentAttribute && argumentAttribute.TrimQuotation);
 }
Пример #6
0
 public static bool IsRequired(this CommandLineAttribute attribute)
 {
     return(attribute is ArgumentAttribute argumentAttribute && argumentAttribute.Required);
 }
Пример #7
0
        internal static bool SetPropertyValue <T>(T instance, PropertyInfo propertyInfo, IDictionary <string, CommandLineArgument> arguments, CommandLineAttribute attribute)
        {
            var  count        = 0;
            var  propertyName = attribute.Name ?? propertyInfo.Name;
            bool trim         = attribute.TrimQuotation();

            string stringValue;

            if (arguments.TryGetValue(propertyName, out var 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, propertyName)), null);
                arguments.Remove(propertyName);
                count++;
            }

            foreach (var alias in attribute.Aliases)
            {
                if (arguments.TryGetValue(alias, 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, propertyName)), null);
                    arguments.Remove(alias);
                    count++;
                }
            }

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

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

            return(count > 0);
        }
Пример #8
0
        internal static bool SetOptionValue <T>(T instance, PropertyInfo propertyInfo, IDictionary <string, CommandLineArgument> arguments, CommandLineAttribute attribute)
        {
            bool wasSet       = false;
            var  propertyName = attribute.Name ?? propertyInfo.Name;

            if (arguments.TryGetValue(propertyName, out var argument))
            {
                if (argument.Value != null)
                {
                    throw new CommandLineArgumentException($"The option '{argument.Name}' was specified with a value. This is not allowed for option.")
                          {
                              Reason = ErrorReason.OptionWithValue
                          }
                }
                ;

                propertyInfo.SetValue(instance, true, null);
                arguments.Remove(propertyName);
                wasSet = true;
            }

            foreach (var alias in attribute.Aliases)
            {
                if (arguments.TryGetValue(alias, out argument))
                {
                    if (argument.Value != null)
                    {
                        throw new CommandLineArgumentException($"The option '{alias}' was specified with a value. This is not allowed for option.")
                              {
                                  Reason = ErrorReason.OptionWithValue
                              }
                    }
                    ;

                    propertyInfo.SetValue(instance, true, null);
                    arguments.Remove(alias);
                    wasSet = true;
                }
            }

            return(wasSet);
        }
Пример #9
0
        internal static void EnsureUnique <T>(Dictionary <string, PropertyInfo> usedNames, CommandLineAttribute commandLineAttribute, PropertyInfo propertyInfo)
        {
            var names = new List <string> {
                commandLineAttribute.Name ?? propertyInfo.Name
            };

            names.AddRange(commandLineAttribute.Aliases);

            foreach (var name in names)
            {
                if (usedNames.TryGetValue(name, out var firstProperty))
                {
                    var message = $"The properties '{firstProperty.Name}' and '{propertyInfo.Name}' of the class '{typeof(T).Name}' define both a name (or alias) called '{name}'";
                    throw new CommandLineAttributeException(message)
                          {
                              Name = name, FirstProperty = firstProperty, SecondProperty = propertyInfo
                          };
                }
            }

            foreach (var name in names)
            {
                usedNames[name] = propertyInfo;
            }
        }
Пример #10
0
        private bool IsRequired(CommandLineAttribute commandLineAttribute)
        {
            var attribute = commandLineAttribute as ArgumentAttribute;

            return(attribute != null && attribute.Required);
        }