/// <summary> /// Create a new instance of <see cref="CommandLine"/> from <see cref="PredictiveCommand" />. /// </summary> /// <param name="predictiveCommand">The command information.</param> /// <param name="azContext">The current PowerShell conext.</param> public CommandLine(PredictiveCommand predictiveCommand, IAzContext azContext = null) { Validation.CheckArgument(predictiveCommand, $"{nameof(predictiveCommand)} cannot be null."); var predictionText = CommandLineUtilities.EscapePredictionText(predictiveCommand.Command); var commandAst = CommandLineUtilities.GetCommandAst(predictionText); var commandName = commandAst?.GetCommandName(); Validation.CheckInvariant <CommandLineException>(!string.IsNullOrWhiteSpace(commandName), $"Cannot get the command name from the model {predictiveCommand.Command}"); var parameterSet = new ParameterSet(commandAst, azContext); Name = commandName; Description = predictiveCommand.Description; ParameterSet = parameterSet; SourceText = predictiveCommand.Command; }
/// <summary> /// Create a new instance of <see cref="CommandLine"/> from <see cref="PredictiveCommand" />. /// </summary> /// <param name="predictiveCommand">The command information.</param> /// <param name="azContext">The current PowerShell conext.</param> public CommandLine(PredictiveCommand predictiveCommand, IAzContext azContext = null) { Validation.CheckArgument(predictiveCommand, $"{nameof(predictiveCommand)} cannot be null."); var predictionText = CommandLineUtilities.EscapePredictionText(predictiveCommand.Command); Ast ast = Parser.ParseInput(predictionText, out Token[] tokens, out _); var commandAst = ast.Find((ast) => ast is CommandAst, searchNestedScriptBlocks: false) as CommandAst; var commandName = commandAst?.GetCommandName(); Validation.CheckInvariant <CommandLineException>(!string.IsNullOrWhiteSpace(commandName), $"Cannot get the command name from the model {predictiveCommand.Command}"); var parameterSet = new ParameterSet(commandAst, azContext); Name = commandName; Description = predictiveCommand.Description; ParameterSet = parameterSet; SourceText = predictiveCommand.Command; }