[英]How to get powershell function source with full header
As seen here https://superuser.com/questions/414036/get-definition-of-function-and-echo-the-code this would get the body of test-function如此处所示https://superuser.com/questions/414036/get-definition-of-function-and-echo-the-code这将获得 test-function 的主体
${function:test-function}
but not the header of test-function with parameters但不是带有参数的测试功能的 header
You'll have to add the missing parts yourself ( function <name> {... }
) in order to form a full function definition:您必须自己添加缺少的部分(
function <name> {... }
)才能形成完整的 function 定义:
$funcDef = "function test-function { ${function:test-function} }"
If you're given the function name in a variable or you want to avoid repeating the function name:如果您在变量中获得了 function 名称,或者您想避免重复 function 名称:
$funcName = 'test-function'
$funcDef = "function $funcName { $(Get-Content function:$funcName) }"
Note that the function body returned does include parameters , in the form of a param(...)
block - even if the original definition used the in-line parameter-declaration form.请注意,返回的 function 主体确实包含参数,以
param(...)
块的形式 - 即使原始定义使用内联参数声明形式。 Eg, both function test-function($foo) { 'foo' }
and function test-function { param($foo) 'foo' }
result in body param($foo) 'foo'
(with variations in whitespace).例如,
function test-function($foo) { 'foo' }
和function test-function { param($foo) 'foo' }
导致正文param($foo) 'foo'
(空格有变化)。
Background:背景:
${function:test-function}
is an instance of namespace variable notation , and therefore only works with literal names. ${function:test-function}
是命名空间变量表示法的一个实例,因此仅适用于文字名称。
It returns the body of the specified function as a script block , which when stringified, results in the block's verbatim source code (without the {
and }
that you use to create a script-block literal .它以脚本块的形式返回指定的 function 的主体,当字符串化时,会产生块的逐字源代码(没有用于创建脚本块文字的
{
和}
。
Get-Content function:test-function
is what the namespace variable notation translates into, except that the cmdlet-based enables use of variables in its arguments. Get-Content function:test-function
是命名空间变量表示法转换成的内容,除了基于 cmdlet 的允许在其 arguments 中使用变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.