Beispiel #1
0
        private bool TryHandlePrematureError(Exception ex, TargetResolver targetResolver)
        {
            var context = new ExceptionContext(ex);

            if (Register.RegisteredErrorHandler != null)
            {
                Register.RegisteredErrorHandler(context);

                return(context.ReThrow);
            }
            else
            {
                for (int i = 0; i < m_types.Length; i++)
                {
                    var type = m_types[i];

                    var errorHandler = ParserRunner.GetDefinedErrorHandlers(type).FirstOrDefault();

                    if (errorHandler != null)
                    {
                        var target = targetResolver == null ? null : targetResolver.Resolve(type);

                        errorHandler.Invoke(target, new[] { context });

                        return(context.ReThrow);
                    }
                }
            }

            return(true);
        }
Beispiel #2
0
        private void Init()
        {
            Debug.Assert(m_types.Any());

            Register = new ParserRegistration(m_types, GetHelpString);

            foreach (var type in m_types)
            {
                ParserRunner.Validate(type, Register);
            }
        }
Beispiel #3
0
        private void HandleEmptyArguments(TargetResolver targetResolver)
        {
            if (Register.RegisteredEmptyHandler != null)
            {
                Register.RegisteredEmptyHandler();
            }
            else if (m_types.Length == 1)
            {
                var parser = new ParserRunner(m_types.First(), Register, HelpGenerator);

                var target = targetResolver == null ? null : targetResolver.Resolve(m_types[0]);

                parser.HandleEmptyArguments(target);
            }
        }
        private void HandleEmptyArguments(object[] targets)
        {
            if (Register.RegisteredEmptyHandler != null)
            {
                Register.RegisteredEmptyHandler();
            }
            else if (m_types.Length == 1)
            {
                var parser = new ParserRunner(m_types.First(), Register);

                var target = targets == null ? null : targets[0];

                parser.HandleEmptyArguments(target);
            }
        }
Beispiel #5
0
        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);
        }
Beispiel #6
0
        private ParserRunner GetSingleTypeParser(string[] args, object obj, 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;
        }
Beispiel #7
0
        private void HandleEmptyArguments(TargetResolver targetResolver)
        {
            if (Register.RegisteredEmptyHandler != null)
            {
                Register.RegisteredEmptyHandler();
            }
            else if (m_types.Length == 1)
            {
                var parser = new ParserRunner(m_types.First(), Register, HelpGenerator);

                var target = targetResolver == null ? null : targetResolver.Resolve(m_types[0]);

                parser.HandleEmptyArguments(target);
            }
        }
Beispiel #8
0
        private static ParserHelpInfo GetParserHelp(ParserRunner parser)
        {
            var h = new ParserHelpInfo
            {
                Type = parser.Type,
                Verbs = parser.GetVerbs().Select(verb => new VerbHelpInfo
                {
                    Names = verb.Names.OrderByDescending(n => n.Length).ToList(),
                    Description = verb.Description,
                    IsDefault = verb.IsDefault,
                    Validations = verb.MethodInfo.GetInterfaceAttributes<ICollectionValidation>().Select(v => v.Description).ToList(),
                    Parameters = ParserRunner.GetParameters(verb.MethodInfo).
                        Select(p => new ParameterHelpInfo
                        {
                            Required = p.Required,
                            Names = p.Names.OrderBy(n => n.Length).ToList(),
                            Type = p.ParameterInfo.ParameterType,
                            Default = p.DefaultProvider != null ? p.DefaultProvider.Description : p.Default,
                            Description = p.Description,
                            Validations = p.ParameterInfo.GetAttributes<ValidationAttribute>().Select(v => v.Description).ToList(),
                            Separator = p.ParameterInfo.ParameterType.IsArray ?
                                p.Separator ?? SeparatorAttribute.DefaultSeparator : null,
                        }).ToList(),
                }).ToList(),
                Globals = parser.GetDefinedGlobals().Select(g =>
                {
                    var att = g.GetAttribute<GlobalAttribute>();
                    var parameter = ParserRunner.GetParameters(g).FirstOrDefault();

                    return new GlobalParameterHelpInfo
                    {
                        Names = att.Aliases.CommaSplit().Union(new[] { att.Name ?? g.Name }).OrderBy(n => n.Length).ToList(),
                        Type = parameter != null ? parameter.ParameterInfo.ParameterType : typeof(bool),
                        Description = att.Description,
                        Validations = g.GetInterfaceAttributes<ICollectionValidation>().Select(v => v.Description).ToList(),
                        Separator = parameter != null ?
                            parameter.ParameterInfo.ParameterType.IsArray ?
                                parameter.Separator ?? SeparatorAttribute.DefaultSeparator :
                                    null : null,
                    };
                }).Union(parser.Register.RegisteredGlobalHandlers.Values.Select(handler => new GlobalParameterHelpInfo
                {
                    Names = handler.Names.OrderBy(n => n.Length).ToList(),
                    Type = handler.Type,
                    Description = handler.Description,
                    Validations = new List<string>(),
                })).ToList(),
            };

            if (parser.Register.RegisteredHelpHandlers.Any())
            {
                h.Globals.Add(new GlobalParameterHelpInfo
                {
                    Names = parser.Register.RegisteredHelpHandlers.Keys.ToList(),
                    Type = typeof(bool),
                    Description = "Help",
                });
            }

            return h;
        }
Beispiel #9
0
 internal string GetHelp(ParserRunner parser)
 {
     return GetHelp(new[] { parser });
 }
Beispiel #10
0
        private static ParserHelpInfo GetParserHelp(ParserRunner parser)
        {
            var h = new ParserHelpInfo
            {
                Type  = parser.Type,
                Verbs = parser.GetVerbs().Select(verb => new VerbHelpInfo
                {
                    Names       = verb.Names.OrderByDescending(n => n.Length).ToList(),
                    Description = verb.Description,
                    IsDefault   = verb.IsDefault,
                    Validations = verb.MethodInfo.GetInterfaceAttributes <ICollectionValidation>().Select(v => v.Description).ToList(),
                    Parameters  = ParserRunner.GetParameters(verb.MethodInfo).
                                  Select(p => new ParameterHelpInfo
                    {
                        Required    = p.Required,
                        Names       = p.Names.OrderBy(n => n.Length).ToList(),
                        Type        = p.ParameterInfo.ParameterType,
                        Default     = p.DefaultProvider != null ? p.DefaultProvider.Description : p.Default,
                        Description = p.Description,
                        Validations = p.ParameterInfo.GetAttributes <ValidationAttribute>().Select(v => v.Description).ToList(),
                        Separator   = p.ParameterInfo.ParameterType.IsArray ?
                                      p.Separator ?? SeparatorAttribute.DefaultSeparator : null,
                    }).ToList(),
                }).ToList(),
                Globals = parser.GetDefinedGlobals().Select(g =>
                {
                    var att       = g.GetAttribute <GlobalAttribute>();
                    var parameter = ParserRunner.GetParameters(g).FirstOrDefault();

                    return(new GlobalParameterHelpInfo
                    {
                        Names = att.Aliases.CommaSplit().Union(new[] { att.Name ?? g.Name }).OrderBy(n => n.Length).ToList(),
                        Type = parameter != null ? parameter.ParameterInfo.ParameterType : typeof(bool),
                        Description = att.Description,
                        Validations = g.GetInterfaceAttributes <ICollectionValidation>().Select(v => v.Description).ToList(),
                        Separator = parameter != null ?
                                    parameter.ParameterInfo.ParameterType.IsArray ?
                                    parameter.Separator ?? SeparatorAttribute.DefaultSeparator :
                                    null : null,
                    });
                }).Union(parser.Register.RegisteredGlobalHandlers.Values.Select(handler => new GlobalParameterHelpInfo
                {
                    Names       = handler.Names.OrderBy(n => n.Length).ToList(),
                    Type        = handler.Type,
                    Description = handler.Description,
                    Validations = new List <string>(),
                })).ToList(),
            };

            if (parser.Register.RegisteredHelpHandlers.Any())
            {
                h.Globals.Add(new GlobalParameterHelpInfo
                {
                    Names       = parser.Register.RegisteredHelpHandlers.Keys.ToList(),
                    Type        = typeof(bool),
                    Description = "Help",
                });
            }

            return(h);
        }
Beispiel #11
0
 internal string GetHelp(ParserRunner parser)
 {
     return(GetHelp(new[] { parser }));
 }