PowerTip of the Day-Make Function Parameters Mandatory
原文地址:http://app.en25.com/e/es.aspx?s=1403&e=5441&elq=3d32771b868b437aa97567a6a50b5563
原文:
Mandatory parameters are special because if you do not submit it, PowerShell will automatically ask for it. You can use mandatory parameters in your own functions as well. You should simply mark a function as mandatory by adding the appropriate [Parameter()] attribute like so:
function Test-Me {
param(
[Parameter(Mandatory=$true)]
$name
)
"You entered $name."
}
Test-Me
The parameter attributes used in this tip were introduced with PowerShell v2.
翻译:
Mandatory参数是很特殊的因为这种参数是在你不提交的时候,PowerShell会自动让你输入的。同样可以在你自己的函数里使用Mandatory参数。只需要用标记[Parameter()]属性标记在方法前面就可以了,比如:
function Test-Me {
param(
[Parameter(Mandatory=$true)]
$name
)
"You entered $name."
}
Test-Me
这种参数属性的使用在PowerShell v2中就已经被介绍过了。
笔记:
Mandatory此处对应中文的意思不是很清楚,强迫?义务?强制?
最后一句话看得不是很懂。
---------------------------------------------------------------
aspnetx的BI笔记系列索引:
使用SQL Server Analysis Services数据挖掘的关联规则实现商品推荐功能
---------------------------------------------------------------
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步