当前位置: 首页>>代码示例>>C#>>正文


C# ExecutionContext.CreateCommand方法代码示例

本文整理汇总了C#中ExecutionContext.CreateCommand方法的典型用法代码示例。如果您正苦于以下问题:C# ExecutionContext.CreateCommand方法的具体用法?C# ExecutionContext.CreateCommand怎么用?C# ExecutionContext.CreateCommand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ExecutionContext的用法示例。


在下文中一共展示了ExecutionContext.CreateCommand方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateGetHelpCommandProcessor

        /// <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(
                PositionUtilities.EmptyExtent, "Name", "-Name:",
                PositionUtilities.EmptyExtent, helpTarget,
                false);
            helpCommandProcessor.AddParameter(cpi);
            cpi = CommandParameterInternal.CreateParameterWithArgument(
                PositionUtilities.EmptyExtent, "Category", "-Category:",
                PositionUtilities.EmptyExtent, helpCategory.ToString(),
                false);
            helpCommandProcessor.AddParameter(cpi);
            return helpCommandProcessor;
        }
开发者ID:dfinke,项目名称:powershell,代码行数:34,代码来源:CommandProcessorBase.cs

示例2: PrepareCommandElements

 private bool PrepareCommandElements(ExecutionContext context)
 {
     int num = 0;
     bool dotSource = this._commandAst.InvocationOperator == TokenKind.Dot;
     CommandProcessorBase base2 = null;
     string resolvedCommandName = null;
     bool flag2 = false;
     try
     {
         base2 = this.PrepareFromAst(context, out resolvedCommandName) ?? context.CreateCommand(resolvedCommandName, dotSource);
     }
     catch (RuntimeException exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
         if ((this._commandAst.IsInWorkflow() && (resolvedCommandName != null)) && CompletionCompleters.PseudoWorkflowCommands.Contains<string>(resolvedCommandName, StringComparer.OrdinalIgnoreCase))
         {
             flag2 = true;
         }
         else
         {
             return false;
         }
     }
     CommandProcessor commandProcessor = base2 as CommandProcessor;
     ScriptCommandProcessorBase base3 = base2 as ScriptCommandProcessorBase;
     bool flag3 = (commandProcessor != null) && commandProcessor.CommandInfo.ImplementsDynamicParameters;
     List<object> list = flag3 ? new List<object>(this._commandElements.Count) : null;
     if (((commandProcessor != null) || (base3 != null)) || flag2)
     {
         num++;
         while (num < this._commandElements.Count)
         {
             CommandParameterAst parameterAst = this._commandElements[num] as CommandParameterAst;
             if (parameterAst != null)
             {
                 if (list != null)
                 {
                     list.Add(parameterAst.Extent.Text);
                 }
                 AstPair item = (parameterAst.Argument != null) ? new AstPair(parameterAst, parameterAst.Argument) : new AstPair(parameterAst);
                 this._arguments.Add(item);
             }
             else
             {
                 StringConstantExpressionAst ast2 = this._commandElements[num] as StringConstantExpressionAst;
                 if ((ast2 == null) || !ast2.Value.Trim().Equals("-", StringComparison.OrdinalIgnoreCase))
                 {
                     ExpressionAst argumentAst = this._commandElements[num] as ExpressionAst;
                     if (argumentAst != null)
                     {
                         if (list != null)
                         {
                             list.Add(argumentAst.Extent.Text);
                         }
                         this._arguments.Add(new AstPair(null, argumentAst));
                     }
                 }
             }
             num++;
         }
     }
     if (commandProcessor != null)
     {
         this._function = false;
         if (flag3)
         {
             ParameterBinderController.AddArgumentsToCommandProcessor(commandProcessor, list.ToArray());
             bool flag4 = false;
             bool flag5 = false;
             do
             {
                 CommandProcessorBase currentCommandProcessor = context.CurrentCommandProcessor;
                 try
                 {
                     context.CurrentCommandProcessor = commandProcessor;
                     commandProcessor.SetCurrentScopeToExecutionScope();
                     if (!flag4)
                     {
                         commandProcessor.CmdletParameterBinderController.BindCommandLineParametersNoValidation(commandProcessor.arguments);
                     }
                     else
                     {
                         flag5 = true;
                         commandProcessor.CmdletParameterBinderController.ClearUnboundArguments();
                         commandProcessor.CmdletParameterBinderController.BindCommandLineParametersNoValidation(new Collection<CommandParameterInternal>());
                     }
                 }
                 catch (ParameterBindingException exception2)
                 {
                     if ((exception2.ErrorId == "MissingArgument") || (exception2.ErrorId == "AmbiguousParameter"))
                     {
                         flag4 = true;
                     }
                 }
                 catch (Exception exception3)
                 {
                     CommandProcessorBase.CheckForSevereException(exception3);
                 }
                 finally
                 {
//.........这里部分代码省略.........
开发者ID:nickchal,项目名称:pash,代码行数:101,代码来源:PseudoParameterBinder.cs

示例3: PrepareCommandElements

        private bool PrepareCommandElements(ExecutionContext context)
        {
            int commandIndex = 0;
            bool dotSource = _commandAst.InvocationOperator == TokenKind.Dot;

            CommandProcessorBase processor = null;
            string commandName = null;
            bool psuedoWorkflowCommand = false;
            try
            {
                processor = PrepareFromAst(context, out commandName) ?? context.CreateCommand(commandName, dotSource);
            }
            catch (RuntimeException rte)
            {
                // Failed to create the CommandProcessor;
                CommandProcessorBase.CheckForSevereException(rte);

                if (_commandAst.IsInWorkflow() &&
                    commandName != null &&
                    CompletionCompleters.PseudoWorkflowCommands.Contains(commandName, StringComparer.OrdinalIgnoreCase))
                {
                    psuedoWorkflowCommand = true;
                }
                else
                {
                    return false;
                }
            }

            var commandProcessor = processor as CommandProcessor;
            var scriptProcessor = processor as ScriptCommandProcessorBase;
            bool implementsDynamicParameters = commandProcessor != null &&
                                               commandProcessor.CommandInfo.ImplementsDynamicParameters;

            var argumentsToGetDynamicParameters = implementsDynamicParameters
                                                      ? new List<object>(_commandElements.Count)
                                                      : null;
            if (commandProcessor != null || scriptProcessor != null || psuedoWorkflowCommand)
            {
                // Pre-processing the arguments -- command arguments
                for (commandIndex++; commandIndex < _commandElements.Count; commandIndex++)
                {
                    var parameter = _commandElements[commandIndex] as CommandParameterAst;
                    if (parameter != null)
                    {
                        if (argumentsToGetDynamicParameters != null)
                        {
                            argumentsToGetDynamicParameters.Add(parameter.Extent.Text);
                        }

                        AstPair parameterArg = parameter.Argument != null
                            ? new AstPair(parameter)
                            : new AstPair(parameter, (ExpressionAst)null);

                        _arguments.Add(parameterArg);
                    }
                    else
                    {
                        var dash = _commandElements[commandIndex] as StringConstantExpressionAst;
                        if (dash != null && dash.Value.Trim().Equals("-", StringComparison.OrdinalIgnoreCase))
                        {
                            // "-" is represented by StringConstantExpressionAst. Most likely the user type a tab here,
                            // and we don't want it be treated as an argument
                            continue;
                        }

                        var expressionArgument = _commandElements[commandIndex] as ExpressionAst;
                        if (expressionArgument != null)
                        {
                            if (argumentsToGetDynamicParameters != null)
                            {
                                argumentsToGetDynamicParameters.Add(expressionArgument.Extent.Text);
                            }

                            _arguments.Add(new AstPair(null, expressionArgument));
                        }
                    }
                }
            }

            if (commandProcessor != null)
            {
                _function = false;
                if (implementsDynamicParameters)
                {
                    ParameterBinderController.AddArgumentsToCommandProcessor(commandProcessor, argumentsToGetDynamicParameters.ToArray());
                    bool retryWithNoArgs = false, alreadyRetried = false;

                    do
                    {
                        CommandProcessorBase oldCurrentCommandProcessor = context.CurrentCommandProcessor;
                        try
                        {
                            context.CurrentCommandProcessor = commandProcessor;
                            commandProcessor.SetCurrentScopeToExecutionScope();
                            // Run method "BindCommandLineParametersNoValidation" to get all available parameters, including the dynamic
                            // parameters (some of them, not necessarilly all. Since we don't do the actual binding, some dynamic parameters
                            // might not be retrieved). 
                            if (!retryWithNoArgs)
                            {
//.........这里部分代码省略.........
开发者ID:dfinke,项目名称:powershell,代码行数:101,代码来源:PseudoParameterBinder.cs

示例4: GetRedirectionPipe

 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);
 }
开发者ID:nickchal,项目名称:pash,代码行数:36,代码来源:FileRedirection.cs


注:本文中的ExecutionContext.CreateCommand方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。