private static string[] GetAliases(CommandLineAttribute attribute) { if (attribute != null) { return(attribute.Aliases); } return(new string[0]); }
public virtual void PrintPropertyHelp(PropertyInfo property) { propertyInfo = property; commandLineAttribute = propertyInfo.GetAttribute <CommandLineAttribute>(); detailedHelpTextAttribute = propertyInfo.GetAttribute <DetailedHelpTextAttribute>(); helpTextAttribute = propertyInfo.GetAttribute <HelpTextAttribute>(); WriteHeader(); WriteContent(); WriteFooter(); }
private static string GetArgumentName(PropertyInfo info, CommandLineAttribute commandLineAttribute) { string primaryName = info.Name; if (commandLineAttribute?.Name != null) { return(commandLineAttribute.Name.ToLowerInvariant()); } return(primaryName.ToLowerInvariant()); }
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; }
public static bool TrimQuotation(this CommandLineAttribute attribute) { return(attribute is ArgumentAttribute argumentAttribute && argumentAttribute.TrimQuotation); }
public static bool IsRequired(this CommandLineAttribute attribute) { return(attribute is ArgumentAttribute argumentAttribute && argumentAttribute.Required); }
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); }
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); }
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; } }
private bool IsRequired(CommandLineAttribute commandLineAttribute) { var attribute = commandLineAttribute as ArgumentAttribute; return(attribute != null && attribute.Required); }