[英]Powershell: parametersets, require one or the other
Consider this Cmdlet (usings removed): 考虑以下Cmdlet(已删除用法):
public class Foo : Cmdlet
{
[Parameter(Mandatory = true, ParameterSetName = "Foo"]
public string A { get; set; }
[Parameter(Mandatory = true, ParameterSetName = "Bar"]
public string B { get; set; }
[Parameter(Mandatory = true, ParameterSetName = "Bar"]
public string C { get; set; }
protected override void ProcessRecord()
{
/* magic goes here */
}
}
Now the way you can execute this Cmdlet is as follows: 现在,执行此Cmdlet的方式如下:
# like this
Foo
# or
Foo -A "test"
# or
Foo -B "test" -C "test"
Having and A and B doesn't work, neither does only using B (requires C). 拥有和A和B无效,仅使用B也无效(需要C)。
So that's fine, however I want to prevent that you can execute the Cmdlet by just typing Foo
, now I can check that in code, but since Powershell is so awesome, is there a way to enforce this through the runtime? 很好,但是我想防止您只键入Foo
就可以执行Cmdlet,现在我可以在代码中进行检查了,但是由于Powershell非常强大,有没有办法在运行时强制执行此操作?
You may instruct PowerShell to default to a named parameter set, by way of the DefaultParameterSetName
property of the CmdletAttribute
attribute: 您可以通过CmdletAttribute
属性的DefaultParameterSetName
属性,指示PowerShell默认使用命名参数集:
[Cmdlet("Foo", "Bar", DefaultParameterSetName = "Foo")]
public class Foo : Cmdlet
{
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.