简体   繁体   English

使用参数集约束多个互斥依赖关系

[英]Using parametersets to constrain more than one mutually exclusive dependency

When a script's signature is differentiated (singularly) by an argument, parameter sets make sense to me. 当脚本的签名通过参数区分(单个)时,参数集对我来说很有意义。

Example: 例:

.\myscript.ps1 -InputFile [-Optional1] [-Optional2]...
.\myscript.ps1 -ArrayOfNames [-Optional1] [-Optional2]...

My question is: Are parameter sets the logical choice when you wish to support parallel (or multiple) dependencies as explained below? 我的问题是:当您希望支持并行(或多个)依赖关系时,参数设置是否是逻辑选择,如下所述?

Here is my current scenario. 这是我目前的情况。 I'm adding support for an existing script that queries logs containing time stamps. 我正在添加对查询包含时间戳记的日志的现有脚本的支持。 The script should accept a csv file or an array of smtp addresses to identify which users to query. 该脚本应接受一个csv文件一个smtp地址数组,以标识要查询的用户。

The script should also support begin and end date parameters or an integer value to facilitate reporting n number of days in the past, calculated from the current date. 该脚本应该支持开始和结束日期参数一个整数值,以方便在过去,从当前日期计算天的报告n个。

The outcome I wish to support is: 我希望支持的结果是:

.\myScript -InputFile -StartDate -EndDate [-Optional1] [-Optional2]...
.\myScript -InputFile -LastNumDays [-Optional1] [-Optional2]...
.\myScript -Smtp -StartDate -EndDate [-Optional1] [-Optional2]...
.\myScript -Smtp -LastNumDays [-Optional1] [-Optional2]...

Either of the following two parameter definitions work well if I don't attempt to combine my two requirements: 如果我不尝试合并两个要求,则以下两个参数定义都可以正常工作:

[Parameter(Mandatory=$true, ParameterSetName="Input")]
[ValidateScript({Test-Path -Path $_ -PathType Leaf})][string] $InputFile,

[Parameter(Mandatory=$true, ParameterSetName="NoInput")]
[ValidateNotNullOrEmpty()][String[]] $Smtp

Get-Help displays expected usage as: Get-Help将预期用法显示为:

.\myScript.ps1 -InputFile <String> [<CommonParameters>]
.\myScript.ps1 -Smtp <String[]> [<CommonParameters>]

If I configure the following instead: 如果我改为配置以下内容:

[Parameter(Mandatory=$true, ParameterSetName="NotRange")]
[ValidateNotNullOrEmpty()][int] $LastNumDays = 30, # init 30 days default

[Parameter(Mandatory=$true, ParameterSetName="Range")]
[ValidateNotNullOrEmpty()][Alias("Start")] [DateTime] $StartDate,

[Parameter(Mandatory=$true, ParameterSetName="Range")]
[ValidateNotNullOrEmpty()][Alias("End")] [DateTime] $EndDate

Get-Help displays expected usage as: Get-Help将预期用法显示为:

.\myScript.ps1 -LastNumDays <Int32> [<CommonParameters>]
.\myScript.ps1 -StartDate <DateTime> -EndDate <DateTime> [<CommonParameters>]

The problem is that I can't seem to incorporate both my dependencies as described at the beginning of this post. 问题在于,我似乎无法像本文开头所述那样合并我的两个依赖项。 An example of just one of my unsuccessful attempts to combine these two logical dependencies using parameter sets is as follows: 我使用参数集将这两个逻辑依赖项组合在一起的尝试失败了,其中一个示例如下:

[Parameter(Mandatory=$true, ParameterSetName="Input")]
[ValidateScript({Test-Path -Path $_ -PathType Leaf})][string] $InputFile,

[Parameter(Mandatory=$true, ParameterSetName="NoInput")]
[ValidateNotNullOrEmpty()][String[]] $Smtp,

[Parameter(Mandatory=$true, ParameterSetName="NotRange")]
[Parameter(Mandatory=$true, ParameterSetName="Input")]
[Parameter(Mandatory=$true, ParameterSetName="NoInput")]
[ValidateNotNullOrEmpty()][int] $LastNumDays = 30, # init 30 days default

[Parameter(Mandatory=$true, ParameterSetName="Range")]
[Parameter(Mandatory=$true, ParameterSetName="Input")]
[Parameter(Mandatory=$true, ParameterSetName="NoInput")]
[ValidateNotNullOrEmpty()][Alias("Start")] [DateTime] $StartDate,

[Parameter(Mandatory=$true, ParameterSetName="Range")]
[Parameter(Mandatory=$true, ParameterSetName="Input")]
[Parameter(Mandatory=$true, ParameterSetName="NoInput")]
[ValidateNotNullOrEmpty()][Alias("End")] [DateTime] $EndDate

Get-Help results are incorrect b/c the first two usage statements allow LastNumDays and Start/EndDate parameters to be used at the same time: 获取帮助的结果不正确b / c前两个用法语句允许同时使用LastNumDays和Start / EndDate参数:

.\myScript.ps1 -InputFile <String> -LastNumDays <Int32> -StartDate <DateTime> -EndDate <DateTime> [<CommonParameters>]

.\myScript.ps1 -Smtp <String[]> -LastNumDays <Int32> -StartDate <DateTime> -EndDate <DateTime> [<CommonParameters>]

.\myScript.ps1 -LastNumDays <Int32> [<CommonParameters>]
.\myScript.ps1 -StartDate <DateTime> -EndDate <DateTime> [<CommonParameters>]

I've tested different combinations of mandatory true/false and including/omitting my named parameter sets with no success. 我测试了强制性true / false的不同组合,包括/省略了我的命名参数集,但均未成功。

I'm suspecting now that my requirements may not be fitting for the use case parameter sets were intended to support but am left to wonder what pattern and practice I should be using instead. 我现在怀疑我的要求可能不适合用例参数集旨在支持的情况,但是却想知道我应该使用哪种模式和实践。

How can I properly define usage syntax for these two dependencies if not using parameter sets? 如果不使用参数集,如何为这两个依赖项正确定义用法语法? I feel that I must avoid resorting to tests in my code that announce dependencies that are not defined in Get-Help. 我觉得我必须避免在我的代码中诉诸测试来宣布在Get-Help中未定义的依赖项。

Thank you! 谢谢!

You need to make each parameter set unique, so PowerShell can distinguish one from another. 您需要使每个参数集唯一,以便PowerShell可以将另一个参数区分开。 For simplicity reasons I'll name the parameter sets A through D: 为简单起见,我将参数集命名为A到D:

A: -InputFile -StartDate -EndDate 答: -InputFile -StartDate -EndDate
B: -InputFile -LastNumDays B: -InputFile -LastNumDays
C: -Smtp -StartDate -EndDate C: -Smtp -StartDate -EndDate
D: -Smtp -LastNumDays D: -Smtp -LastNumDays

Now associate each parameter with each parameter set it appears in: 现在将每个参数与它出现在每个参数集中相关联:

Param(
  [Parameter(Mandatory=$true, ParameterSetName="A")]
  [Parameter(Mandatory=$true, ParameterSetName="B")]
  [string]$InputFile,

  [Parameter(Mandatory=$true, ParameterSetName="C")]
  [Parameter(Mandatory=$true, ParameterSetName="D")]
  [String[]]$Smtp,

  [Parameter(Mandatory=$true, ParameterSetName="B")]
  [Parameter(Mandatory=$true, ParameterSetName="D")]
  [int]$LastNumDays,

  [Parameter(Mandatory=$true, ParameterSetName="A")]
  [Parameter(Mandatory=$true, ParameterSetName="C")]
  [DateTime]$StartDate,

  [Parameter(Mandatory=$true, ParameterSetName="A")]
  [Parameter(Mandatory=$true, ParameterSetName="C")]
  [DateTime]$EndDate
)

Output: 输出:

PS C:\> .\test.ps1 -?
test.ps1 -InputFile <string> -LastNumDays <int> [<CommonParameters>]
test.ps1 -InputFile <string> -StartDate <datetime> -EndDate <datetime> [<CommonParameters>]
test.ps1 -Smtp <string[]> -LastNumDays <int> [<CommonParameters>]
test.ps1 -Smtp <string[]> -StartDate <datetime> -EndDate <datetime> [<CommonParameters>]

Note that it's pointless to provide a default value for a mandatory parameter ( -LastNumDays ), because you're required to provide a value anyway. 请注意,为强制性参数提供默认值( -LastNumDays )是没有意义的,因为无论如何都需要提供一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM