private new bool RunCommand(ExecutionContext context) { switch (context.Command.State) { case ResultState.PromptForCommand: context.Arguments = new List<string>(); context.IO.WriteLine("Which command do you want to execute?"); context.IO.WriteLine(""); var cancel = false; var line = context.IO.ReadLine(() => cancel = true); if (cancel) return false; var num = ParsePlus(line); if (num > -1 && context.Commands != null) { line = context.Commands.ElementAt(num - 1).CommandName; context.IO.ClearLastLine(); context.IO.WriteLine("> " + line); } context.Arguments.Add(line); context.IO.WriteLine(""); context.Command = context.Runner.Run(line); break; case ResultState.ShowHelpOverview: context.Commands = context.Command.Result as List<CommandInfo>; int ii = 1; context.IO.WriteLine("Commands available:"); context.IO.WriteLine(""); context.IO.WriteLine("/help Command overview (this view)"); context.IO.WriteLine("/mode silent - No prompting"); context.IO.WriteLine("[ESC] Cancel command"); context.IO.WriteLine(""); context.Commands?.ForEach(cmd => { context.IO.Write("[" + ii + "]" + string.Join(" ", new String(' ', 8 - ("[" + ii + "]").Length)) + cmd.CommandName); cmd?.Arguments.ToList().ForEach(arg => { context.IO.Write(" /" + arg.Name + ":" + (!string.IsNullOrWhiteSpace(arg.DefaultValue?.ToString()) ? arg.DefaultValue?.ToString() : "?")); }); context.IO.WriteLine(""); ii += 1; }); context.IO.WriteLine(""); context.IO.WriteLine("Type number or command name."); context.IO.WriteLine(""); context.Command.State = ResultState.PromptForCommand; break; default: return base.RunCommand(context); } // return true; }
/// <summary> /// Runs the command in interactive mode. /// </summary> /// <param name="context">Execution context</param> /// <returns></returns> public void Run(ExecutionContext context) { bool keepon = true; while (keepon) { keepon = RunCommand(context); } }
/// <summary> /// Mode Execute silent. /// </summary> /// <param name="context">Execution context</param> /// <returns></returns> public void Run(ExecutionContext context) { context.IO.WriteLine("> {0}", string.Join(" ", context.Arguments)); context.IO.WriteLine(""); if (context.Command?.State == ResultState.Success) { RunCommand(context); return; } context.IO.WriteLine("No success."); }
/// <summary> /// Run command in console. /// </summary> /// <param name="commands"></param> /// <param name="args"></param> /// <returns></returns> public static void RunConsole(this NCommands commands, string[] args) { // Run var arguments = args.ToList(); var runner = new CommandRunner(new CommandParser(commands.Context.Configuration.Notation), new CommandLookup(commands.Context.Configuration)); var result = runner.Run(string.Join(" ", arguments)); // Execution flow var context = new ExecutionContext(ConsoleUtility.Instance, result, runner, arguments, commands.Context.Configuration); var flowManager = new ExecutionFlowFactory().Get(context); // todo: make this injectable flowManager.Run(context); }
/// <summary> /// Mode Execute. Not silent, missing parameters will be asked. /// </summary> /// <param name="context">Execution context</param> /// <returns></returns> public void Run(ExecutionContext context) { context.IO.WriteLine("> {0}", string.Join(" ", context.Arguments)); context.IO.WriteLine(""); bool keepon = true; while (keepon) { keepon = RunCommand(context); if (context.Command != null && context.Command.State == ResultState.PromptForCommand) return; } }
/// <summary> /// Gets the appropriate execution flow. /// </summary> /// <param name="context">Execution context.</param> /// <returns></returns> public IExecutionFlow Get(ExecutionContext context) { // Silent mode if (context.Arguments.Any(x => x.ToLower() == "/mode:silent")) { return new RunExecuteSilent(); } // Interactive interactive if (context.Command.State == ResultState.ShowHelpOverview) { return new RunInteractive(); } // Execution mode return new RunExecute(); }
/// <summary> /// Runs the command. /// </summary> public bool RunCommand(ExecutionContext context) { switch (context.Command.State) { case ResultState.Success: var res = context.Command.Result; if (context.Command.Result is Task) res = context.Command.Result.Result; if (res is IEnumerable && !(res is IEnumerable<char>)) { foreach (var item in (res as IEnumerable)) { context.IO.WriteLine(item?.ToString()); } context.IO.WriteLine(""); } else { context.IO.WriteLine(res?.ToString()); context.IO.WriteLine(""); } context.Command.State = ResultState.PromptForCommand; break; case ResultState.MissingArguments: context.IO.WriteLine(@"{0} arguments missing. ", context.Command.MissingArguments.Count()); context.IO.WriteLine(""); var cancelThisCommand = false; var missingArguments = new List<string>(); for (int i = 0; i < context.Command.MissingArguments.Count(); i++) { var argument = context.Command.MissingArguments.ElementAt(i); if (!string.IsNullOrWhiteSpace(argument.Description)) { context.IO.WriteLine($"DescriptiOn: {argument.Description}"); context.IO.WriteLine($"Enter value for '{argument.Name}':"); } else { context.IO.WriteLine($"Enter value for '{argument.Name}':"); } var input = context.IO.ReadLine(() => cancelThisCommand = true); if (cancelThisCommand) { context.Command.State = ResultState.PromptForCommand; break; } missingArguments.Add("/" + argument.Name + ":" + input); // todo: ReRun (enteredValues) method on CommandRunner (Initial paramaters saved in a state) context.IO.WriteLine(""); } if (cancelThisCommand) break; context.IO.WriteLine(""); context.Arguments.AddRange(missingArguments); context.Command = context.Runner.Run(string.Join(" ", context.Arguments)); break; case ResultState.UnknownCommand: context.IO.WriteLine(@"Unknown command."); context.IO.WriteLine(""); context.Command.State = ResultState.PromptForCommand; break; case ResultState.ParsingError: context.IO.WriteLine(@"Parsing error."); context.IO.WriteLine(""); context.Command.State = ResultState.PromptForCommand; break; case ResultState.ErrorWhileExecuting: context.IO.WriteLine(@"Error while executing."); context.IO.WriteLine(""); context.Command.State = ResultState.PromptForCommand; break; default: throw new Exception(@"Error in NCommands. Unknown state: {currentState}"); } // return true; }
/// <summary> /// Runs the command. /// </summary> public bool RunCommand(ExecutionContext context) { switch (context.Command.State) { case ResultState.Success: var res = context.Command.Result; if (context.Command.Result is Task) res = context.Command.Result.Result; if (res is IEnumerable && !(res is IEnumerable<char>)) { foreach (var item in (res as IEnumerable)) { context.IO.WriteLine(item?.ToString()); } context.IO.WriteLine(""); } else { context.IO.WriteLine(res?.ToString()); context.IO.WriteLine(""); } context.Command.State = ResultState.PromptForCommand; break; case ResultState.MissingArguments: context.IO.WriteLine(@"Arguments missing. ", context.Command.MissingArguments.Count()); var argumentsToPromptFor = new List<ArgumentInfo>(); argumentsToPromptFor.AddRange(context.Command.MissingArguments); argumentsToPromptFor.AddRange(context.Command.MissingDefaultArguments); for (int i = 0; i < argumentsToPromptFor.Count(); i++) { var argument = argumentsToPromptFor.ElementAt(i); context.IO.WriteLine($"* {argument.Name}: {argument.Description}"); } context.IO.WriteLine(""); var cancelThisCommand = false; var missingArguments = new List<string>(); for (int i = 0; i < argumentsToPromptFor.Count(); i++) { var argument = argumentsToPromptFor.ElementAt(i); context.IO.WriteLine($"Enter value for '{argument.Name}':"); var input = context.IO.ReadLine(() => cancelThisCommand = true); if (cancelThisCommand) { context.Command.State = ResultState.PromptForCommand; break; } var seperator1 = context.Configuration.Notation == ParserNotation.Windows ? '/' : '-'; var seperator2 = context.Configuration.Notation == ParserNotation.Windows ? ':' : '='; missingArguments.Add(seperator1 + argument.Name + seperator2 + input); // todo: ReRun (enteredValues) method on CommandRunner (Initial paramaters saved in a state) context.IO.WriteLine(""); } if (cancelThisCommand) break; context.IO.WriteLine(""); context.Arguments.AddRange(missingArguments); context.Command = context.Runner.Run(string.Join(" ", context.Arguments)); break; case ResultState.UnknownCommand: context.IO.WriteLine(@"Unknown command."); context.IO.WriteLine(""); context.Command.State = ResultState.PromptForCommand; break; case ResultState.ParsingError: context.IO.WriteLine(@"Parsing error."); context.IO.WriteLine(""); context.Command.State = ResultState.PromptForCommand; break; case ResultState.ErrorWhileExecuting: context.IO.WriteLine(@"Error while executing."); context.IO.WriteLine(""); if (context.Configuration.DisplayExceptionDetails) { context.IO.WriteLine(string.Join(Environment.NewLine, context.Command.Exceptions.Select(x => x.Message + Environment.NewLine + x.StackTrace))); } context.IO.WriteLine(""); if (context.Configuration.TraceExceptions) { Trace.WriteLine(context.Command.Exceptions, "Exceptions"); } context.Command.State = ResultState.PromptForCommand; break; case ResultState.Canceled: return false; default: throw new Exception(@"Error in NCommands. Unknown state: {currentState}"); } // return true; }