private ArgumentEnumerator(ArgumentEnumerator enumerator, int startIndex) { enumerator.ParseArguments(); _threadID = Thread.CurrentThread.ManagedThreadId; _namePrefix = enumerator._namePrefix; _originalArguments1 = enumerator._originalArguments1; _originalArguments2 = enumerator._originalArguments2; _arguments = enumerator._arguments; _startIndex = startIndex; _currentIndex = startIndex - 1; }
private static void ExecuteCommand( Command command, TextReader input, TextWriter output, TextWriter error, bool continiousExecution, char namePrefix ) { do { output.Write('>'); try { string arguments = input.ReadLine(); if (arguments == null) { break; } ArgumentEnumerator enumerator = (string.IsNullOrEmpty(arguments)) ? ArgumentEnumerator.Empty : new ArgumentEnumerator(namePrefix, arguments); command.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); }
private void ExecuteHelp(TextWriter output, ArgumentEnumerator args) { string description = Description; if (description != null) { output.WriteLine(description); } char namePrefix = args.NamePrefix; WriteSyntax(output, namePrefix); WriteArguments(output, namePrefix); WriteRemarks(output); WriteExamples(output, namePrefix); }
private void WriteExamples(TextWriter output, char namePrefix) { Attribute[] eas = Attribute.GetCustomAttributes(_method.Method, typeof(ExampleAttribute)); if (eas.Length == 0) { return; } output.WriteLine(); output.WriteLine(Resources.ExamplesSection); foreach (ExampleAttribute ea in eas) { output.WriteLine(); string description = ea.Description; if (description != null && description.Length > 0) { output.WriteLine(description); output.WriteLine(); } output.WriteIndent(ExampleIndent); output.Write(Name); ArgumentEnumerator args = new ArgumentEnumerator(ea.NamePrefix, ea.Example); while (args.MoveNext()) { output.Write(' '); output.Write(namePrefix); output.Write(args.CurrentName); if (args.CurrentValue != bool.FalseString && args.CurrentValue != bool.TrueString) { output.Write(':'); output.Write(args.CurrentValue); } } output.WriteLine(); } }
/// <summary> /// Executes a command using specified IO streams and arguments. /// </summary> /// <param name="input">The <see cref="T:System.IO.TextReader" /> that represents an input stream.</param> /// <param name="output">The <see cref="T:System.IO.TextWriter" /> that represents an output stream.</param> /// <param name="error">The <see cref="T:System.IO.TextWriter" /> that represents an error stream.</param> /// <param name="args">Command-line arguments to be passed to the command.</param> /// <exception cref="T:System.ArgumentNullException"><paramref name="input" />, <paramref name="output" />, /// <paramref name="error" />, or <paramref name="args" /> is null.</exception> public void Execute(TextReader input, TextWriter output, TextWriter error, ArgumentEnumerator args) { if (input == null) { throw new ArgumentNullException("input"); } if (output == null) { throw new ArgumentNullException("output"); } if (error == null) { throw new ArgumentNullException("error"); } if (args == null) { throw new ArgumentNullException("args"); } ExecuteCore(input, output, error, args); }
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 object[] ParseArguments(ArgumentEnumerator args, TextWriter error) { object[] parameterValues = new object[_argumentMap.Count]; for (int i = 0; i < parameterValues.Length; i++) { parameterValues[i] = Type.Missing; } foreach (IGrouping <string, string> argumentValue in args.GroupBy((i) => i.Key, (i) => i.Value)) { Argument argument; if (!_argumentMap.TryGetValue(argumentValue.Key, out argument)) { ThrowHelper.ThrowUnknownArgumentException(Name, argumentValue.Key); } int argumentValueCount = argumentValue.Count(); if (argument.AreMultipleAllowed) { if (argument.Type.IsAssignableFrom(typeof(IEnumerable <string>))) { parameterValues[argument.Position] = argumentValue; } else { try { parameterValues[argument.Position] = argument.TypeConverter.ConvertFrom(argumentValue); } catch (Exception ex) { ThrowHelper.ThrowCanNotParseArgumentValueException(Name, argument.DisplayName, argument.Type, ex); } } } else { if (argumentValueCount > 1) { ThrowHelper.ThrowTooManyValuesForArgumentException(Name, argument.DisplayName); } if (argument.Type.IsAssignableFrom(typeof(string))) { parameterValues[argument.Position] = argumentValue.First().ToString(); } else { try { parameterValues[argument.Position] = argument.TypeConverter.ConvertFrom(argumentValue.First()); } catch (Exception ex) { ThrowHelper.ThrowCanNotParseArgumentValueException(Name, argument.DisplayName, argument.Type, ex); } } } } foreach (Argument argument in _argumentMap.Values) { if (!(argument.IsInput || argument.IsOutput || argument.IsError) && parameterValues[argument.Position] == Type.Missing) { if (argument.IsRequired) { ThrowHelper.ThrowRequiredArgumentException(Name, argument.DisplayName); } parameterValues[argument.Position] = argument.DefaultValue; } } return(parameterValues); }
private void ExecuteMethod(TextReader input, TextWriter output, TextWriter error, ArgumentEnumerator args) { args.Reset(); object[] parameterValues = ParseArguments(args, error); Stream inOutStream = null; foreach (Argument argument in _argumentMap.Values) { if (argument.IsInput && argument.IsOutput) { if (inOutStream == null) { inOutStream = InOutStream.Create(input, output); } parameterValues[argument.Position] = inOutStream; } else { if (argument.IsInput) { parameterValues[argument.Position] = input; } if (argument.IsOutput) { parameterValues[argument.Position] = output; } } if (argument.IsError) { parameterValues[argument.Position] = error; } } try { _method.DynamicInvoke(parameterValues); } catch (Exception ex) { if (ex.InnerException != null && ex is TargetInvocationException) { ex = ex.InnerException; } if (ex is CommandCanceledException) { throw ex; } ThrowHelper.ThrowCommandExecutionException(Name, ex); } }
/// <inheritdoc /> protected override void ExecuteCore(TextReader input, TextWriter output, TextWriter error, ArgumentEnumerator args) { if (args.MoveNext() && string.CompareOrdinal(args.CurrentName, "?") == 0) { ExecuteHelp(output, args); } else { ExecuteMethod(input, output, error, args); } }
/// <summary> /// When overridden in a derived class, provides execution logic. /// </summary> /// <param name="input">The <see cref="T:System.IO.TextReader" /> that represents an input stream.</param> /// <param name="output">The <see cref="T:System.IO.TextWriter" /> that represents an output stream.</param> /// <param name="error">The <see cref="T:System.IO.TextWriter" /> that represents an error stream.</param> /// <param name="args">Command-line arguments which were passed to the command.</param> protected abstract void ExecuteCore(TextReader input, TextWriter output, TextWriter error, ArgumentEnumerator args);
/// <summary> /// Executes a command using specified IO streams and arguments. /// </summary> /// <param name="input">The <see cref="T:System.IO.TextReader" /> that represents an input stream.</param> /// <param name="output">The <see cref="T:System.IO.TextWriter" /> that represents an output stream.</param> /// <param name="args">Command-line arguments to be passed to the command.</param> /// <exception cref="T:System.ArgumentNullException"><paramref name="input" />, <paramref name="output" />, /// or <paramref name="args" /> is null.</exception> public void Execute(TextReader input, TextWriter output, ArgumentEnumerator args) { Execute(input, output, output, args); }
/// <summary> /// Executes a command using standard IO streams and specified arguments. /// </summary> /// <param name="args">Command-line arguments to be passed to the command.</param> public void Execute(ArgumentEnumerator args) { Execute(Console.In, Console.Out, Console.Error, args); }