public static bool IsParameterPresent(string command, string parameterName) { if (string.IsNullOrWhiteSpace(parameterName)) { throw new ArgumentException("Invalid parameterName passed"); } Token[] array = null; ParseError[] array2 = null; Parser.ParseInput(command, out array, out array2); return(array != null && array.Any((Token token) => CmdletValidator.IsTokenParameter(parameterName, token))); }
private static void AddParameter(string parameterName, List <Token> parameterTokens, CommandParameterCollection parameters) { if (string.IsNullOrWhiteSpace(parameterName)) { return; } if (parameterTokens == null || parameterTokens.Count == 0) { parameters.Add(new CommandParameter(parameterName)); return; } int i = 0; object obj = null; i = CmdletValidator.GetParameterValue(parameterTokens, i, out obj); if (obj != null && i >= parameterTokens.Count) { parameters.Add(new CommandParameter(parameterName, obj)); return; } List <object> list = new List <object>(); if (obj != null) { list.Add(obj); } while (i < parameterTokens.Count) { i = CmdletValidator.GetParameterValue(parameterTokens, i, out obj); if (obj != null) { list.Add(obj); } } parameters.Add(new CommandParameter(parameterName, list.ToArray())); }
public ScriptParseResult ParseCmdletScript(string cmdlet) { ScriptParseResult scriptParseResult = new ScriptParseResult(); Token[] array = null; ParseError[] array2 = null; Parser.ParseInput(cmdlet, out array, out array2); if (array2 != null && array2.Count <ParseError>() > 0) { TaskLogger.Log(Strings.CmdletValidatorParseErrors(string.Join(", ", from error in array2 select error.Message))); return(scriptParseResult); } int num = 0; if (array == null || array[0].TokenFlags != TokenFlags.CommandName) { TaskLogger.Log(Strings.CmdletValidatorParseErrors("No valid command specified")); return(scriptParseResult); } if (array.Any((Token token) => (token.TokenFlags & TokenFlags.SpecialOperator) != TokenFlags.None || (token.TokenFlags & TokenFlags.TypeName) != TokenFlags.None)) { TaskLogger.Log(Strings.CmdletValidatorParseErrors("Special Operator and types not supported")); return(scriptParseResult); } scriptParseResult.Command = array[num].Text; if (!this.AllowedCommands.Contains(scriptParseResult.Command)) { TaskLogger.Log(Strings.CmdletValidatorParseErrors(string.Format("Command {0} not allowed", scriptParseResult.Command))); return(scriptParseResult); } List <Token> list = null; string text = null; int num2 = array.Count <Token>(); try { while (++num < num2 && array[num].Kind != TokenKind.EndOfInput) { if (array[num].Kind == TokenKind.Parameter) { CmdletValidator.AddParameter(text, list, scriptParseResult.Parameters); text = array[num].Text; list = null; } else { if (string.IsNullOrWhiteSpace(text)) { TaskLogger.Log(Strings.CmdletValidatorParseErrors(string.Format("Invalid token {0} of kind {1}, {2} encountered without a parameter", array[num].Text, array[num].Kind, array[num].TokenFlags))); return(scriptParseResult); } if (list == null) { list = new List <Token>(); } list.Add(array[num]); } } CmdletValidator.AddParameter(text, list, scriptParseResult.Parameters); scriptParseResult.IsSuccessful = this.Validate(scriptParseResult.Command, scriptParseResult.Parameters); } catch (InvalidOperationException ex) { TaskLogger.Log(Strings.CmdletValidatorParseErrors(ex.ToString())); } return(scriptParseResult); }
private static int GetParameterValue(List <Token> parameterValueTokens, int tokenIndex, out object parameterValue) { int count = parameterValueTokens.Count; parameterValue = null; if (tokenIndex >= count) { return(tokenIndex); } Token token = parameterValueTokens[tokenIndex]; TokenKind kind = token.Kind; if (kind <= TokenKind.Number) { if (kind != TokenKind.Variable) { if (kind == TokenKind.Number) { parameterValue = ((NumberToken)token).Value; goto IL_23B; } } else { bool flag; if (bool.TryParse(((VariableToken)token).Name, out flag)) { parameterValue = flag; goto IL_23B; } parameterValue = token.Text; goto IL_23B; } } else { switch (kind) { case TokenKind.StringLiteral: parameterValue = ((StringToken)token).Value; goto IL_23B; case TokenKind.StringExpandable: parameterValue = ((StringExpandableToken)token).Value; goto IL_23B; default: switch (kind) { case TokenKind.LBracket: case TokenKind.RBracket: case TokenKind.Comma: parameterValue = null; goto IL_23B; case TokenKind.AtParen: { List <object> list = new List <object>(); tokenIndex++; while (tokenIndex < count && parameterValueTokens[tokenIndex].Kind != TokenKind.RParen) { if (parameterValueTokens[tokenIndex].Kind == TokenKind.Comma) { tokenIndex++; } else { object obj = null; tokenIndex = CmdletValidator.GetParameterValue(parameterValueTokens, tokenIndex, out obj); if (obj != null) { list.Add(obj); } } } parameterValue = list.ToArray(); goto IL_23B; } case TokenKind.AtCurly: { Dictionary <object, object> dictionary = new Dictionary <object, object>(); tokenIndex++; while (tokenIndex < count && parameterValueTokens[tokenIndex].Kind != TokenKind.RCurly) { if (parameterValueTokens[tokenIndex].Kind == TokenKind.Semi) { tokenIndex++; } else { object obj2 = null; tokenIndex = CmdletValidator.GetParameterValue(parameterValueTokens, tokenIndex, out obj2); if (tokenIndex >= count || parameterValueTokens[tokenIndex].Kind != TokenKind.Equals) { throw new InvalidOperationException(string.Format("Cmdlet parsing failed at index {0}. Expected Equals. Actual {1}", tokenIndex, parameterValueTokens[tokenIndex].Text)); } if (++tokenIndex >= count) { throw new InvalidOperationException(string.Format("Cmdlet parsing failed. No value for key {0}", obj2)); } object value = null; tokenIndex = CmdletValidator.GetParameterValue(parameterValueTokens, tokenIndex, out value); dictionary.Add(obj2, value); } } parameterValue = dictionary; goto IL_23B; } case TokenKind.Semi: case TokenKind.Pipe: throw new InvalidOperationException(string.Format("Cmdlet parsing failed at index {0}. Encountered unexpected {1}", tokenIndex, token.Kind)); } break; } } parameterValue = token.Text; IL_23B: return(++tokenIndex); }