in src/FunctionInfo.cs [161:212]
private Dictionary<string, PSScriptParamInfo> GetParameters(string scriptFile, string entryPoint, out ScriptBlockAst scriptAst)
{
scriptAst = Parser.ParseFile(scriptFile, out _, out ParseError[] errors);
if (errors != null && errors.Length > 0)
{
var stringBuilder = new StringBuilder(15);
foreach (var error in errors)
{
stringBuilder.AppendLine(error.Message);
}
string errorMsg = stringBuilder.ToString();
throw new ArgumentException(string.Format(PowerShellWorkerStrings.FailToParseScript, scriptFile, errorMsg));
}
ReadOnlyCollection<ParameterAst> paramAsts = null;
if (string.IsNullOrEmpty(entryPoint))
{
paramAsts = scriptAst.ParamBlock?.Parameters;
}
else
{
var asts = scriptAst.FindAll(
ast => ast is FunctionDefinitionAst func && entryPoint.Equals(func.Name, StringComparison.OrdinalIgnoreCase),
searchNestedScriptBlocks: false).ToList();
if (asts.Count == 1)
{
var funcAst = (FunctionDefinitionAst) asts[0];
paramAsts = funcAst.Parameters ?? funcAst.Body.ParamBlock?.Parameters;
}
else
{
string errorMsg = asts.Count == 0
? string.Format(PowerShellWorkerStrings.CannotFindEntryPoint, entryPoint, scriptFile)
: string.Format(PowerShellWorkerStrings.MultipleEntryPointFound, entryPoint, scriptFile);
throw new ArgumentException(errorMsg);
}
}
var parameters = new Dictionary<string, PSScriptParamInfo>(StringComparer.OrdinalIgnoreCase);
if (paramAsts != null)
{
foreach (var paramAst in paramAsts)
{
var psParamInfo = new PSScriptParamInfo(paramAst);
parameters.Add(psParamInfo.ParamName, psParamInfo);
}
}
return parameters;
}