コード例 #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
ファイル: Parser.cs プロジェクト: cloudified/CLAP
        public static object Run(string[] args, TargetResolver targetResolver)
        {
            Debug.Assert(targetResolver != null);

            var p = new Parser(targetResolver.RegisteredTypes);

            return(p.RunTargets(args, targetResolver));
        }
コード例 #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
        public int RunTargets(string[] args, TargetResolver targetResolver)
        {
            ParserRunner parser;

            try
            {
                args = ProcessHideAll(args);

                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));
        }
コード例 #5
0
ファイル: MultiParser.cs プロジェクト: serra/CLAP
        /// <summary>
        /// Run a parser of instance verbs against instances of the verb classes
        /// </summary>
        /// <param name="args">The user arguments</param>
        /// <param name="targets">The instances of the verb classes</param>
        public int RunTargets(string[] args, params object[] targets)
        {
            var targetResolver = new TargetResolver(targets);

            return(RunTargets(args, targetResolver));
        }
コード例 #6
0
ファイル: MultiParser.cs プロジェクト: olegtarasov/CLAP
        /// <summary>
        /// Run a parser of instance verbs against instances of the verb classes
        /// </summary>
        /// <param name="args">The user arguments</param>
        /// <param name="targets">The instances of the verb classes</param>
        public async Task <int> RunTargetsAsync(string[] args, params object[] targets)
        {
            var targetResolver = new TargetResolver(targets);

            return(await RunTargetsAsync(args, targetResolver));
        }