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); }
public static object Run(string[] args, TargetResolver targetResolver) { Debug.Assert(targetResolver != null); var p = new Parser(targetResolver.RegisteredTypes); return(p.RunTargets(args, targetResolver)); }
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); } }
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)); }
/// <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)); }
/// <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)); }