如何确定是否指定了 PowerShell Cmdlet 参数值?

发布于 2024-07-30 11:23:26 字数 748 浏览 9 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

指尖上的星空 2024-08-06 11:23:27

在这种情况下,我将在枚举类型周围使用可为空的包装器,例如

[Parameter(Mandatory = false)]
public MyEnum? IsEnabled { get; set; }

注意 ? MyEnum 上的修饰符。 然后你可以测试是否设置如下:

if (this.IsEnabled.HasValue) { ... }

In this case, I would use a nullable wrapper around the enum type e.g.

[Parameter(Mandatory = false)]
public MyEnum? IsEnabled { get; set; }

Note the ? modifier on MyEnum. Then you can test if it is set like so:

if (this.IsEnabled.HasValue) { ... }
酒儿 2024-08-06 11:23:27

抛开 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.

  • Oisin
葬心 2024-08-06 11:23:27

作为一个选项,您可以使用 $PSBoundParameters 集合来检查是否通过到方法。

假设我们有一个如下所示的 json 文件 { "p1": "value1", "p2": "value2" } 并且我们想要创建一个接受参数 p1 的函数> 和 p2 并更新文件中 p1p2 的值(如果它们传递给函数)。 假设这些值可以为 null,并且将这些值设置为 null 并不等同于不传递它们。

例如,Update-Values -p1 $null 应将 p1 更新为 null,并且不应更改 p2

为此,我们应该能够检测参数是否已传递给方法。

示例 - 对于可以接受 null 作为值的可选参数,如何检测是否传递了参数?

Function Update-Values ($p1, $p2) {
    If($PSBoundParameters.ContainsKey("p1")) {
        Write-Host "p1 passed"
    }
    else {
        Write-Host "p1 not passed"
    }
    If($PSBoundParameters.ContainsKey("p2")) {
        Write-Host "p2 passed"
    }
    else {
        Write-Host "p2 not passed"
    }
}

然后,如果您使用以下参数运行该函数:

Update-Values -p1 $null
# Update-Values -p1 "something"

结果您将看到:

p1 passed
p2 not passed

您可以阅读博客文章在这里: 如何确定参数是否传递给 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 parameter p1 and p2 and update value of p1 and p2 in the file, if they are passed to the function. Let's say those values can be null and and having those values as null is not equivalent to not passing them.

For example Update-Values -p1 $null should update p1 to null and should not change p2.

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?

Function Update-Values ($p1, $p2) {
    If($PSBoundParameters.ContainsKey("p1")) {
        Write-Host "p1 passed"
    }
    else {
        Write-Host "p1 not passed"
    }
    If($PSBoundParameters.ContainsKey("p2")) {
        Write-Host "p2 passed"
    }
    else {
        Write-Host "p2 not passed"
    }
}

Then if you run the function using the following parameter:

Update-Values -p1 $null
# Update-Values -p1 "something"

As a result you will see:

p1 passed
p2 not passed

You can read the blog post here: How to determine if a parameter is passed to a Powershell Cmdlet.

猫九 2024-08-06 11:23:27

我知道这个线程现在有点旧了,但最好的方法是使用 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.

酒解孤独 2024-08-06 11:23:27

我唯一能看到的是修改你的枚举,使值 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.

且行且努力 2024-08-06 11:23:27
bool wasSpecified = MyInvocation.BoundParameters.ContainsKey("IsEnabled");
bool wasSpecified = MyInvocation.BoundParameters.ContainsKey("IsEnabled");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文