Example #1
0
        /// <summary>
        /// Creates a command processor for "get-help [helpTarget]"
        /// </summary>
        /// <param name="context">context for the command processor</param>
        /// <param name="helpTarget">help target</param>
        /// <param name="helpCategory">help category</param>
        /// <returns>command processor for "get-help [helpTarget]"</returns>
        internal static CommandProcessorBase CreateGetHelpCommandProcessor(
            ExecutionContext context,
            string helpTarget,
            HelpCategory helpCategory)
        {
            if (context == null)
            {
                throw PSTraceSource.NewArgumentNullException("context");
            }
            if (string.IsNullOrEmpty(helpTarget))
            {
                throw PSTraceSource.NewArgumentNullException("helpTarget");
            }

            CommandProcessorBase helpCommandProcessor = context.CreateCommand("get-help", false);
            var cpi = CommandParameterInternal.CreateParameterWithArgument(
                /*parameterAst*/ null, "Name", "-Name:",
                /*argumentAst*/ null, helpTarget,
                false);

            helpCommandProcessor.AddParameter(cpi);
            cpi = CommandParameterInternal.CreateParameterWithArgument(
                /*parameterAst*/ null, "Category", "-Category:",
                /*argumentAst*/ null, helpCategory.ToString(),
                false);
            helpCommandProcessor.AddParameter(cpi);
            return(helpCommandProcessor);
        }
Example #2
0
        private PipelineProcessor BuildRedirectionPipeline(
            string path,
            ExecutionContext context)
        {
            CommandProcessorBase command = context.CreateCommand("out-file");

            command.AddParameter("-encoding", (object)"unicode");
            if (this.Appending)
            {
                command.AddParameter("-append", (object)true);
            }
            command.AddParameter("-filepath", (object)path);
            PipelineProcessor pipelineProcessor = new PipelineProcessor();

            pipelineProcessor.Add(command);
            try
            {
                pipelineProcessor.StartStepping(true);
            }
            catch (RuntimeException ex)
            {
                if (ex.ErrorRecord.Exception is ArgumentException)
                {
                    throw InterpreterError.NewInterpreterExceptionWithInnerException((object)null, typeof(RuntimeException), this._token, "RedirectionFailed", ex.ErrorRecord.Exception, (object)path, (object)ex.ErrorRecord.Exception.Message);
                }
                ex.ErrorRecord.SetInvocationInfo(new InvocationInfo((CommandInfo)null, this._token, context));
                throw ex;
            }
            return(pipelineProcessor);
        }
Example #3
0
        internal CommandProcessorBase AddToPipeline(
            PipelineProcessor pipeline,
            ExecutionContext context)
        {
            if (pipeline == null)
            {
                throw ParseTreeNode.tracer.NewArgumentNullException(nameof(pipeline));
            }
            int index;
            CommandProcessorBase commandProcessor = this.CreateCommandProcessor(out index, context);

            foreach (CommandParameterInternal parametersAndArgument in (IEnumerable <CommandParameterInternal>) this.BindParametersAndArguments(index, context))
            {
                commandProcessor.AddParameter(parametersAndArgument);
            }
            string       helpTarget;
            HelpCategory helpCategory;

            if (commandProcessor.IsHelpRequested(out helpTarget, out helpCategory))
            {
                commandProcessor = CommandProcessorBase.CreateGetHelpCommandProcessor(context, helpTarget, helpCategory);
                commandProcessor.Command.MyInvocation.ScriptToken = (Token)this._elements[0];
            }
            pipeline.Add(commandProcessor);
            this.BindRedirectionPipes(commandProcessor, pipeline, context);
            return(commandProcessor);
        }
 internal static void AddArgumentsToCommandProcessor(CommandProcessorBase commandProcessor, object[] arguments)
 {
     if (arguments != null)
     {
         for (int i = 0; i < arguments.Length; i++)
         {
             CommandParameterInternal internal2;
             string arg = arguments[i] as string;
             if (ArgumentLooksLikeParameter(arg))
             {
                 int index = arg.IndexOf(':');
                 if ((index != -1) && (index != (arg.Length - 1)))
                 {
                     internal2 = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, arg.Substring(1, index - 1), arg, PositionUtilities.EmptyExtent, arg.Substring(index + 1).Trim(), false);
                 }
                 else if ((i == (arguments.Length - 1)) || (arg[arg.Length - 1] != ':'))
                 {
                     internal2 = CommandParameterInternal.CreateParameter(PositionUtilities.EmptyExtent, arg.Substring(1), arg);
                 }
                 else
                 {
                     internal2 = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, arg.Substring(1, arg.Length - 2), arg, PositionUtilities.EmptyExtent, arguments[i + 1], false);
                     i++;
                 }
             }
             else
             {
                 internal2 = CommandParameterInternal.CreateArgument(PositionUtilities.EmptyExtent, arguments[i], false);
             }
             commandProcessor.AddParameter(internal2);
         }
     }
 }
Example #5
0
 internal static void AddArgumentsToCommandProcessor(CommandProcessorBase commandProcessor, object[] arguments)
 {
     if (arguments != null)
     {
         for (int i = 0; i < arguments.Length; i++)
         {
             CommandParameterInternal internal2;
             string arg = arguments[i] as string;
             if (ArgumentLooksLikeParameter(arg))
             {
                 int index = arg.IndexOf(':');
                 if ((index != -1) && (index != (arg.Length - 1)))
                 {
                     internal2 = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, arg.Substring(1, index - 1), arg, PositionUtilities.EmptyExtent, arg.Substring(index + 1).Trim(), false);
                 }
                 else if ((i == (arguments.Length - 1)) || (arg[arg.Length - 1] != ':'))
                 {
                     internal2 = CommandParameterInternal.CreateParameter(PositionUtilities.EmptyExtent, arg.Substring(1), arg);
                 }
                 else
                 {
                     internal2 = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, arg.Substring(1, arg.Length - 2), arg, PositionUtilities.EmptyExtent, arguments[i + 1], false);
                     i++;
                 }
             }
             else
             {
                 internal2 = CommandParameterInternal.CreateArgument(PositionUtilities.EmptyExtent, arguments[i], false);
             }
             commandProcessor.AddParameter(internal2);
         }
     }
 }
Example #6
0
        internal static CommandProcessorBase CreateGetHelpCommandProcessor(ExecutionContext context, string helpTarget, HelpCategory helpCategory)
        {
            if (context == null)
            {
                throw PSTraceSource.NewArgumentNullException("context");
            }
            if (string.IsNullOrEmpty(helpTarget))
            {
                throw PSTraceSource.NewArgumentNullException("helpTarget");
            }
            CommandProcessorBase     base2     = context.CreateCommand("get-help", false);
            CommandParameterInternal parameter = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, "Name", "-Name:", PositionUtilities.EmptyExtent, helpTarget, false);

            base2.AddParameter(parameter);
            parameter = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, "Category", "-Category:", PositionUtilities.EmptyExtent, helpCategory.ToString(), false);
            base2.AddParameter(parameter);
            return(base2);
        }
Example #7
0
        internal Pipe GetRedirectionPipe(ExecutionContext context, System.Management.Automation.Internal.PipelineProcessor parentPipelineProcessor)
        {
            if (string.IsNullOrWhiteSpace(this.File))
            {
                return(new Pipe {
                    NullPipe = true
                });
            }
            CommandProcessorBase     commandProcessor = context.CreateCommand("out-file", false);
            CommandParameterInternal parameter        = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, "Encoding", "-Encoding:", PositionUtilities.EmptyExtent, "Unicode", false);

            commandProcessor.AddParameter(parameter);
            if (this.Appending)
            {
                parameter = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, "Append", "-Append:", PositionUtilities.EmptyExtent, true, false);
                commandProcessor.AddParameter(parameter);
            }
            parameter = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, "Filepath", "-Filepath:", PositionUtilities.EmptyExtent, this.File, false);
            commandProcessor.AddParameter(parameter);
            this.PipelineProcessor = new System.Management.Automation.Internal.PipelineProcessor();
            this.PipelineProcessor.Add(commandProcessor);
            try
            {
                this.PipelineProcessor.StartStepping(true);
            }
            catch (RuntimeException exception)
            {
                if (exception.ErrorRecord.Exception is ArgumentException)
                {
                    throw InterpreterError.NewInterpreterExceptionWithInnerException(null, typeof(RuntimeException), null, "RedirectionFailed", ParserStrings.RedirectionFailed, exception.ErrorRecord.Exception, new object[] { this.File, exception.ErrorRecord.Exception.Message });
                }
                throw;
            }
            if (parentPipelineProcessor != null)
            {
                parentPipelineProcessor.AddRedirectionPipe(this.PipelineProcessor);
            }
            return(new Pipe(context, this.PipelineProcessor));
        }
        /// <summary>
        /// Reparses the arguments specified in the object[] and generates CommandParameterInternal instances
        /// based on whether the arguments look like parameters. The CommandParameterInternal instances then
        /// get added to the specified command processor.
        /// </summary>
        /// 
        /// <param name="commandProcessor">
        /// The command processor instance to add the reparsed parameters to.
        /// </param>
        /// 
        /// <param name="arguments">
        /// The arguments that require reparsing.
        /// </param>
        /// 
        internal static void AddArgumentsToCommandProcessor(CommandProcessorBase commandProcessor, object[] arguments)
        {
            if ((arguments != null) && (arguments.Length > 0))
            {
                PSBoundParametersDictionary boundParameters = arguments[0] as PSBoundParametersDictionary;
                if ((boundParameters != null) && (arguments.Length == 1))
                {
                    // If they are supplying a dictionary of parameters, use those directly
                    foreach (KeyValuePair<string, object> boundParameter in boundParameters)
                    {
                        CommandParameterInternal param = CommandParameterInternal.CreateParameterWithArgument(
                            PositionUtilities.EmptyExtent, boundParameter.Key, boundParameter.Key,
                            PositionUtilities.EmptyExtent, boundParameter.Value, false);
                        commandProcessor.AddParameter(param);
                    }
                }
                else
                {
                    // Otherwise, we need to parse them ourselves
                    for (int argIndex = 0; argIndex < arguments.Length; ++argIndex)
                    {
                        CommandParameterInternal param;
                        string paramText = arguments[argIndex] as string;
                        if (ArgumentLooksLikeParameter(paramText))
                        {
                            // The argument looks like a parameter.
                            // Create a parameter with argument if the paramText is like this: -Path:c:\windows
                            // Combine it with the next argument if there is an argument, and the parameter ends in ':'.

                            var colonIndex = paramText.IndexOf(':');
                            if (colonIndex != -1 && colonIndex != paramText.Length - 1)
                            {
                                param = CommandParameterInternal.CreateParameterWithArgument(
                                    PositionUtilities.EmptyExtent, paramText.Substring(1, colonIndex - 1), paramText,
                                    PositionUtilities.EmptyExtent, paramText.Substring(colonIndex + 1).Trim(),
                                    false);
                            }
                            else if (argIndex == arguments.Length - 1 || paramText[paramText.Length - 1] != ':')
                            {
                                param = CommandParameterInternal.CreateParameter(
                                    PositionUtilities.EmptyExtent, paramText.Substring(1), paramText);
                            }
                            else
                            {
                                param = CommandParameterInternal.CreateParameterWithArgument(
                                    PositionUtilities.EmptyExtent, paramText.Substring(1, paramText.Length - 2), paramText,
                                    PositionUtilities.EmptyExtent, arguments[argIndex + 1],
                                    false);
                                argIndex++;
                            }
                        }
                        else
                        {
                            param = CommandParameterInternal.CreateArgument(
                                PositionUtilities.EmptyExtent, arguments[argIndex]);
                        }
                        commandProcessor.AddParameter(param);
                    }
                }
            }
        }