示例#1
0
文件: MultiParser.cs 项目: serra/CLAP
        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);
        }
示例#2
0
文件: MultiParser.cs 项目: serra/CLAP
        private void Init()
        {
            Debug.Assert(m_types.Any());

            Register = new ParserRegistration(m_types, GetHelpString);

            foreach (var type in m_types)
            {
                ParserRunner.Validate(type, Register);
            }
        }
示例#3
0
文件: MultiParser.cs 项目: serra/CLAP
        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);
            }
        }
示例#4
0
        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);
            }
        }
示例#5
0
文件: MultiParser.cs 项目: serra/CLAP
        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);
        }
示例#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;
        }
示例#7
0
文件: MultiParser.cs 项目: serra/CLAP
        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);
            }
        }
示例#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;
        }
示例#9
0
 internal string GetHelp(ParserRunner parser)
 {
     return GetHelp(new[] { parser });
 }
示例#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);
        }
示例#11
0
 internal string GetHelp(ParserRunner parser)
 {
     return(GetHelp(new[] { parser }));
 }