Exemple #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);
        }
Exemple #2
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);
            }
        }
Exemple #3
0
        public int RunTargets(string[] args, TargetResolver targetResolver)
        {
            ParserRunner parser;

            try
            {
                if (args.None() || args.All(a => string.IsNullOrEmpty(a)))
                {
                    HandleEmptyArguments(targetResolver);
                    return(SuccessCode);
                }

                if (m_types.Length == 1)
                {
                    parser = GetSingleTypeParser(args, Register);
                }
                else
                {
                    Debug.Assert(m_types.Length > 1);
                    parser = GetMultiTypesParser(args, Register);
                }

                Debug.Assert(parser != null);
            }
            catch (Exception ex)
            {
                // handle error using the first available error handler
                //
                // (if returns true - should rethrow)
                //
                if (TryHandlePrematureError(ex, targetResolver))
                {
                    throw;
                }
                else
                {
                    return(ErrorCode);
                }
            }

            var target = (targetResolver == null || targetResolver.RegisteredTypes.None()) ? null : targetResolver.Resolve(parser.Type);

            return(parser.Run(args, target));
        }