private static void ValidateSeparators(IEnumerable <Method> verbs, ParserRegistration registration) { // check non-arrays // var parameters = verbs.SelectMany(v => v.MethodInfo.GetParameters()).ToList(); var nonArrayWithSeparator = parameters.Where(p => !p.ParameterType.IsArray && p.HasAttribute <SeparatorAttribute>()); if (nonArrayWithSeparator.Any()) { throw new NonArrayParameterWithSeparatorException(nonArrayWithSeparator.First()); } // check invalid separators // var separators = parameters. Where(p => p.HasAttribute <SeparatorAttribute>()). Select(p => Pair.Create(p, p.GetAttribute <SeparatorAttribute>().Separator)); var invalidSeparator = separators. FirstOrDefault(pair => string.IsNullOrEmpty(pair.Second) || pair.Second.Contains(" ")); if (invalidSeparator != null) { throw new InvalidSeparatorException(invalidSeparator.First); } var invalidRegisteredHandlers = registration.RegisteredGlobalHandlers. FirstOrDefault(a => a.Value.Type.IsArray && (string.IsNullOrEmpty(a.Value.Separator) || a.Value.Separator.Contains(" "))); }
private static void ValidateDuplicateGlobals(Type type, ParserRegistration registration) { var definedGlobals = type. GetMethodsWith <GlobalAttribute>(). SelectMany(m => { var att = m.GetAttribute <GlobalAttribute>(); var name = att.Name ?? m.Name; return(att.Aliases.CommaSplit().Union(new[] { name }).Select(s => s.ToLowerInvariant())); }). ToList(); var globals = registration.RegisteredGlobalHandlers.Keys.Select(k => k.ToLowerInvariant()).ToList(); globals.AddRange(definedGlobals); var counts = globals.Distinct().ToDictionary(g => g, g => globals.Count(name => name == g)); var duplicate = counts.Where(c => c.Value > 1); if (duplicate.Any()) { throw new DuplicateGlobalHandlerException(duplicate.First().Key); } }
private void Init() { Debug.Assert(m_types.Any()); Register = new ParserRegistration(m_types, GetHelpString); foreach (var type in m_types) { ParserRunner.Validate(type, Register); } }
internal ParserRunner(Type type, ParserRegistration parserRegistration, HelpGeneratorBase helpGenerator) { Debug.Assert(type != null); Type = type; m_registration = parserRegistration; Validate(type, m_registration); m_helpGenerator = helpGenerator; }
private ParserRunner GetSingleTypeParser(string[] args, ParserRegistration registration) { Debug.Assert(m_types.Length == 1); var type = m_types.First(); var verb = args[0]; var parser = new ParserRunner(type, registration, HelpGenerator); // if there is no verb - leave all the args as is // if (verb.StartsWith(ParserRunner.ArgumentPrefixes)) { return(parser); } // if the verb contains a delimiter - remove the type name from the arg // if (verb.Contains(s_delimiters)) { var parts = verb.Split(s_delimiters, StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 2) { throw new InvalidVerbException(); } Debug.Assert(parts.Length == 2); var typeName = parts[0]; if (!type.Name.Equals(typeName, StringComparison.InvariantCultureIgnoreCase)) { throw new UnknownParserTypeException(typeName); } args[0] = args[0].Substring(typeName.Length + 1); } return(parser); }
internal static void Validate(Type type, ParserRegistration registration) { // no more than one default verb // var verbMethods = type.GetMethodsWith <VerbAttribute>(). Select(m => new Method(m)); var defaultVerbs = verbMethods.Where(m => m.IsDefault); if (defaultVerbs.Count() > 1) { throw new MoreThanOneDefaultVerbException(defaultVerbs.Select(m => m.MethodInfo.Name)); } // no more than one error handler // ValidateDefinedErrorHandlers(type); // validate empty handlers (and empty help handlers) // ValidateDefinedEmptyHandlers(type); // validate pre/post interceptors // ValidateDefinedPreInterceptors(type); ValidateDefinedPostInterceptors(type); // parameters can't have both Default and DefaultProvider // ValidateParameterDefaults(verbMethods); // [Separator] can be applied only to array parameters // ValidateSeparators(verbMethods, registration); // no duplicate globals // ValidateDuplicateGlobals(type, registration); }
private ParserRunner GetMultiTypesParser(string[] args, ParserRegistration registration) { Debug.Assert(args.Any()); var verb = args[0]; // if the first arg is not a verb - throw // if (verb.StartsWith(ParserRunner.ArgumentPrefixes)) { throw new MissingVerbException(); } if (!verb.Contains(s_delimiters)) { throw new MultiParserMissingClassNameException(); } var parts = verb.Split(s_delimiters, StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 2) { throw new InvalidVerbException(); } var typeNameOrAlias = parts[0]; args[0] = args[0].Substring(typeNameOrAlias.Length + 1); var matchingType = registration.GetTargetType(typeNameOrAlias); if (matchingType == null) { throw new UnknownParserTypeException(typeNameOrAlias); } return(new ParserRunner(matchingType, registration, HelpGenerator)); }