/// <summary> /// Initializes a new instance of the <see cref="T:Cmd.Net.CommandContextScope" /> class with the specified <see cref="T:Cmd.Net.CommandContext" />. /// </summary> /// <param name="context">The root command context.</param> /// <exception cref="T:System.ArgumentNullException"><paramref name="context" /> is null.</exception> public CommandContextScope(CommandContext context) { if (context == null) { throw new ArgumentNullException("context"); } _originalScope = s_currentScope; _thread = Thread.CurrentThread; _rootContext = context; _contexts = new List <CommandContext>(); _contexts.Add(context); s_currentScope = this; }
/// <summary> /// Restores the original <see cref="T:Cmd.Net.CommandContext" /> to the active context and recycles the <see cref="T:Cmd.Net.CommandContextScope" /> object. /// </summary> public void Dispose() { if (_disposed) { return; } if (_thread != Thread.CurrentThread) { throw new InvalidOperationException("InvalidContextScopeThread"); } if (s_currentScope != this) { throw new InvalidOperationException("InterleavedContextScopes"); } _disposed = true; s_currentScope = _originalScope; _contexts.Clear(); }
private static void ExecuteContext( CommandContext command, TextReader input, TextWriter output, TextWriter error, bool continiousExecution, char namePrefix ) { using (CommandContextScope commandContextScope = new CommandContextScope(command)) { do { IEnumerator <CommandContext> contextEnumerator = commandContextScope.GetContextEnumerator(); bool contextEnumeratorMoveNext = contextEnumerator.MoveNext(); while (contextEnumeratorMoveNext) { output.Write(contextEnumerator.Current.Name); contextEnumeratorMoveNext = contextEnumerator.MoveNext(); if (contextEnumeratorMoveNext) { output.Write(' '); } } output.Write('>'); try { string arguments = input.ReadLine(); if (arguments == null) { break; } ArgumentEnumerator enumerator = (string.IsNullOrEmpty(arguments)) ? ArgumentEnumerator.Empty : new ArgumentEnumerator(namePrefix, arguments); commandContextScope .CurrentContext .Execute(input, output, error, enumerator); } catch (CommandException ex) { if (ex is CommandCanceledException) { break; } Exception exception = ex; do { error.WriteLine(exception.Message); exception = exception.InnerException; }while (exception != null); if (error != output) { error.WriteLine(); } } output.WriteLine(); }while (continiousExecution); } }
/// <inheritdoc /> protected override void ExecuteCore(TextReader input, TextWriter output, TextWriter error, ArgumentEnumerator args) { Command commandBase = null; CommandContext commandContext = this; if (!args.MoveNext()) { return; } CommandContextScope executionScope = CommandContextScope.Current; if (executionScope != null && executionScope.CurrentContext != this) { ThrowHelper.ThrowNotCurrentCommandContextException(Name); } if (args.CurrentName == string.Empty) { while (string.CompareOrdinal(args.CurrentValue, "..") == 0) { if (executionScope == null) { ThrowHelper.ThrowNoCommandContextExecutionScope(Name); } commandContext = executionScope.PopContext(); if (!args.MoveNext()) { return; } } } do { if (args.CurrentName == string.Empty) { if (string.Compare(args.CurrentValue, "help", StringComparison.OrdinalIgnoreCase) == 0) { commandContext.ExecuteHelp(output, executionScope); return; } commandContext._commands.TryGetCommand(args.CurrentValue, out commandBase); commandContext = commandBase as CommandContext; if (commandContext == null) { break; } if (executionScope != null) { executionScope.PushContext(commandContext); } } else { if (string.CompareOrdinal(args.CurrentName, "?") == 0) { commandContext.ExecuteHelp(output, executionScope); return; } else { ThrowHelper.ThrowUnknownArgumentException(Name, args.CurrentName); } } }while (args.MoveNext()); if (commandBase == null) { if (executionScope == null || executionScope.CurrentContext == this) { ThrowHelper.ThrowUnknownCommandException(args.CurrentValue); } } else { args.MoveNext(); commandBase.Execute(input, output, error, args.ContinueFromCurrent()); } }
private void ExecuteHelp(TextWriter output, CommandContextScope executionScope) { if (executionScope != null) { while (executionScope.CurrentContext != this) { executionScope.PopContext(); } } string description = Description; if (description != null) { output.WriteLine(description); } if (_commands.Count == 0) { return; } output.WriteLine(); output.WriteLine(Resources.CommandsSection); output.WriteLine(); int descriptionIndent = 0; foreach (Command command in _commands) { int nameLength = command.Name.Length; if (descriptionIndent < nameLength) { descriptionIndent = nameLength; } } descriptionIndent += ArgumentIndent + DescriptionGap; foreach (Command command in _commands) { int indent; for (indent = 0; indent < ArgumentIndent; ++indent) { output.Write(' '); } indent += command.Name.Length; output.Write(command.Name); if (indent + DescriptionGap > descriptionIndent) { indent = 0; output.WriteLine(); } for (; indent < descriptionIndent; ++indent) { output.Write(' '); } output.WriteIndented(command.Description, indent, false); output.WriteLine(); } }