如何确定是否指定了 PowerShell Cmdlet 参数值?
在 PowerShell 1.0 中,如果我有一个枚举类型的 cmdlet 参数,建议使用什么方法来测试用户是否在 cmdlet 命令行上指定了该参数? 例如:
MyEnum : int { No = 0, Yes = 1, MaybeSo = 2 }
class DoSomethingCommand : PSCmdlet
...
private MyEnum isEnabled;
[Parameter(Mandatory = false)]
public MyEnum IsEnabled
{
get { return isEnabled; }
set { isEnabled = value; }
}
protected override void ProcessRecord()
{
// How do I know if the user passed -IsEnabled <value> to the cmdlet?
}
有没有什么方法可以做到这一点,而不必用虚拟值播种 isEnabled ? 默认情况下它等于 0,我不想为每个参数播种或向我的枚举添加虚拟值。 我可能有很多带有 100 个参数的 cmdlet,必须有更好的方法。 这与这个问题有关,但我正在寻找一种更清洁的方法来做到这一点。 谢谢。
In PowerShell 1.0, if I have a cmdlet parameter of an enum type, what is the recommended method for testing whether the user specified that parameter on the cmdlet command line? For example:
MyEnum : int { No = 0, Yes = 1, MaybeSo = 2 }
class DoSomethingCommand : PSCmdlet
...
private MyEnum isEnabled;
[Parameter(Mandatory = false)]
public MyEnum IsEnabled
{
get { return isEnabled; }
set { isEnabled = value; }
}
protected override void ProcessRecord()
{
// How do I know if the user passed -IsEnabled <value> to the cmdlet?
}
Is there any way to do this without having to seed isEnabled with a dummy value? By default it will equal 0, and I don't want to have to seed every parameter or add a dummy value to my enum. I've potentially got many cmdlets with 100's of parameters, there's got to be a better way. This is related to this question but I was looking for a cleaner way of doing this.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在这种情况下,我将在枚举类型周围使用可为空的包装器,例如
注意 ? MyEnum 上的修饰符。 然后你可以测试是否设置如下:
In this case, I would use a nullable wrapper around the enum type e.g.
Note the ? modifier on MyEnum. Then you can test if it is set like so:
抛开 PowerShell 不谈,以这种方式使用 0 从来都不是好习惯。 您应该始终使用 0 作为最合适的默认值。 在这种情况下,最合适的默认值应该是“取消设置”之类的内容。
最终,这与 PowerShell 无关,而与 .NET 的良好编码实践有关。
PowerShell aside, it's never good style to use 0 in that way. You should always use 0 for the most appropriate default. In this case, the most appropriate default should be something like "Unset."
Ultimately, this is nothing to do with PowerShell and everything to do with good coding practices for .NET.
作为一个选项,您可以使用
$PSBoundParameters
集合来检查是否通过到方法。假设我们有一个如下所示的 json 文件
{ "p1": "value1", "p2": "value2" }
并且我们想要创建一个接受参数p1
的函数> 和p2
并更新文件中p1
和p2
的值(如果它们传递给函数)。 假设这些值可以为 null,并且将这些值设置为 null 并不等同于不传递它们。例如,
Update-Values -p1 $null
应将p1
更新为null
,并且不应更改p2
。为此,我们应该能够检测参数是否已传递给方法。
示例 - 对于可以接受 null 作为值的可选参数,如何检测是否传递了参数?
然后,如果您使用以下参数运行该函数:
结果您将看到:
您可以阅读博客文章在这里: 如何确定参数是否传递给 Powershell Cmdlet。
As an option you can use
$PSBoundParameters
collection to check whether passed to the method.Let’s suppose we have a json file like following
{ "p1": "value1", "p2": "value2" }
and we want to create a function which accepts parameterp1
andp2
and update value ofp1
andp2
in the file, if they are passed to the function. Let's say those values can benull
and and having those values as null is not equivalent to not passing them.For example
Update-Values -p1 $null
should updatep1
tonull
and should not changep2
.To do so, we should be able to detect if the parameter has been passed to the method or not.
Example - How to detect if a parameter is passed, for an optional parameter which can accept null as value?
Then if you run the function using the following parameter:
As a result you will see:
You can read the blog post here: How to determine if a parameter is passed to a Powershell Cmdlet.
我知道这个线程现在有点旧了,但最好的方法是使用 SwitchParameter 类型声明参数。 然后您的用户不必传递 -IsEnabled ,他们只需添加类似 -Enabled 的内容作为参数。
然后,您测试参数的 .IsPresent 属性,以查看调用者是否将 -Enabled 添加到对 cmdlet 的调用中。
I know this thread is a little old now, but the best way to do this is to declare your parameter using the SwitchParameter type. Then your users don't have to pass -IsEnabled , they would just add something like -Enabled as a parameter.
You then test the .IsPresent property of your parameter to see if the caller added -Enabled to the call to the cmdlet.
我唯一能看到的是修改你的枚举,使值 0 被命名为 Unknown 或类似的东西。
问题是枚举只是背景中的整数,而整数是值类型。 不幸的结果是它们需要具有价值,而该价值默认为 0。
Only thing I can see is to modify your enum so that value 0 is named Unknown or something like that.
Problem is that enums are just integers in background and integers are value types. Unfortunate consequence is that they need to have value and that value is 0 by default.