/// <summary> /// /// </summary> /// <param name="argv"></param> /// <param name="isOutputValue"></param> /// <param name="mode"></param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> /// <exception cref="InvalidOperationException"></exception> /// <returns></returns> public Executor Execute([NotNull, ItemNotNull] string[] argv, bool isOutputValue = true, ExecuteMode mode = ExecuteMode.Default) { if (argv == null) { throw new ArgumentNullException(nameof(argv)); } if (argv.Any(string.IsNullOrEmpty)) { throw new ArgumentException($"Elements in <{nameof(argv)}> Cannot be Null Or Empty.", nameof(argv)); } if (this._engine == null) { throw new InvalidOperationException($"{nameof(Executor)} should Create by {nameof(Engine)}."); } if (this.Value != null) { var router = CommandRouter.Build(this._engine.ServiceProvider, this.Value); using (var s = this._engine.ServiceProvider.CreateScope()) { var configurator = s.ServiceProvider.GetRequiredService <SessionConfigurator>(); configurator.Argv = argv; configurator.Mode = mode; var session = (Session)s.ServiceProvider.GetRequiredService <ISession>(); // init session. try { var value = router.Execute(s.ServiceProvider); if (value != null && isOutputValue) { var formater = this._engine.ServiceProvider.GetRequiredService <IValueFormater>(); var ft = formater.Format(value); if (ft != null) { this._engine.ServiceProvider.GetRequiredService <IOutputer>() .WriteLine(OutputLevel.Normal, formater.Format(value)); } } return(new Executor(this._engine, value)); } catch (TerminationException) { /* ignore. */ } catch (CliException e) { if (e.Message.Length > 0) { this._engine.ServiceProvider.GetRequiredService <IOutputer>() .WriteLine(OutputLevel.Error, e.Message); } session.DrawUsage(); } catch (NotImplementedException e) { this._engine.ServiceProvider.GetRequiredService <IOutputer>() .WriteLine(OutputLevel.Error, e.ToString()); } catch (InvalidOperationException e) { this._engine.ServiceProvider.GetRequiredService <IOutputer>() .WriteLine(OutputLevel.Error, e.Message); } catch (Exception e) { this._engine.ServiceProvider.GetRequiredService <IOutputer>() .WriteLine(OutputLevel.Error, e.ToString()); } } } return(new Executor(this._engine, null)); }