设置多个参数集

时间:2018-04-12 14:44:55

标签: powershell

我正在开发一个包含多个参数集的函数,其中一些是强制性的。

我希望以特定方式设置集合,但我设置的组合似乎没有正常工作。

功能要求:

  • 功能可以使用$OrganizationName$IsUserRepo
  • 函数必须使用$AllEvents或者一个或多个CustomEvents列表中的一个或多个。默认值为$AllEvents

我希望能够调用的示例:

Test-Function -RepoName 'name' -WebHookURL 'https://something.com' -AllEvents
Test-Function -RepoName 'name' -OrganizationName 'OrgName' -WebHookURL 'https://something.com' -AllEvents
Test-Function -RepoName 'name' -IsUserRepo -WebHookURL 'https://something.com' -AllEvents
Test-Function -RepoName 'name' -WebHookURL 'https://something.com' -CommitComment -Create (ect...)
Test-Function -RepoName 'name' -OrganizationName 'OrgName' -WebHookURL 'https://something.com'  -CommitComment -Create (ect...)
Test-Function -RepoName 'name' -IsUserRepo -WebHookURL 'https://something.com'  -CommitComment -Create -Delete (ect...)

我一直在测试的参数:

Function Test-Function {
  [CmdletBinding(
    SupportsShouldProcess = $true,
    DefaultParameterSetName = 'OrganizationRepo'
  )]
  param(
    [Parameter(
      Position = 0,
      Mandatory = $true,
      ValueFromPipelineByPropertyName = $true,
      HelpMessage = "Enter the name of the repository")]
    [ValidateNotNullOrEmpty()]
    [string]
    $RepoName,

    [Parameter(
      ParameterSetName = "OrganizationRepo",
      Position = 1,
      ValueFromPipelineByPropertyName = $true,
      HelpMessage = "Enter the name of the organization account where the repository exists. Default: PowerShell. Ex: Windows-Server-Engineering")]
    [string]
    $OrganizationName = 'POWERSHELL',

    [Parameter(
      ParameterSetName = "IsUserRepo",
      Position = 1,
      ValueFromPipelineByPropertyName = $true,
      HelpMessage = "Repository exists under your personal GitHub account. Default: False")]
    [switch]
    $IsUserRepo,

    [Parameter(
      Mandatory = $true,
      ValueFromPipelineByPropertyName = $true,
      HelpMessage = "Enter the URL to which the payloads will be delivered")]
    [string]
    $WebHookURL,

    [Parameter(ParameterSetName = "OrganizationRepo")]
    [Parameter(ParameterSetName = "IsUserRepo")]
    [Parameter(ParameterSetName = 'AllEvents',
      ValueFromPipelineByPropertyName = $true,
      HelpMessage = "All Events. Default: False")]
    [Switch]
    $AllEvents,

    [Parameter(ParameterSetName = "OrganizationRepo")]
    [Parameter(ParameterSetName = "IsUserRepo")]
    [Parameter(ParameterSetName = 'CustomEvents',
      ValueFromPipelineByPropertyName = $true,
      HelpMessage = "Any time a Commit is commented on. Default: False")]
    [Switch]
    $CommitComment,

    [Parameter(ParameterSetName = "OrganizationRepo")]
    [Parameter(ParameterSetName = "IsUserRepo")]
    [Parameter(ParameterSetName = 'CustomEvents',
      ValueFromPipelineByPropertyName = $true,
      HelpMessage = "Any time a Branch or Tag is created. Default: False")]
    [Switch]
    $Create
  )
  # Continuing Custom Event Parameters
}

1 个答案:

答案 0 :(得分:0)

如果我从你的描述中理解,一种方法是使用6个参数集,这些参数集将为函数产生6个可能的调用(为了可读性而插入了额外的空格):

Test-Function [-RepoName] <string>                                                     -WebHookURL <string> [-Create] [-CommitComment]
Test-Function [-RepoName] <string>                            -CustomEvents <string[]> -WebHookURL <string> [-Create] [-CommitComment]
Test-Function [-RepoName] <string> -OrganizationName <string>                          -WebHookURL <string> [-Create] [-CommitComment]
Test-Function [-RepoName] <string> -OrganizationName <string> -CustomEvents <string[]> -WebHookURL <string> [-Create] [-CommitComment]
Test-Function [-RepoName] <string> -IsUserRepo                                         -WebHookURL <string> [-Create] [-CommitComment]
Test-Function [-RepoName] <string> -IsUserRepo                -CustomEvents <string[]> -WebHookURL <string> [-Create] [-CommitComment]

生成此函数原型的代码如下:

[CmdletBinding(DefaultParameterSetName="NoOrgOrUserRepoAllEvents")]
param(
  [parameter(ParameterSetName="NoOrgOrUserRepoAllEvents",   Position=0,Mandatory=$true)]
  [parameter(ParameterSetName="NoOrgOrUserRepoCustomEvents",Position=0,Mandatory=$true)]
  [parameter(ParameterSetName="OrgRepoAllEvents",           Position=0,Mandatory=$true)]
  [parameter(ParameterSetName="OrgRepoCustomEvents",        Position=0,Mandatory=$true)]
  [parameter(ParameterSetName="UserRepoAllEvents",          Position=0,Mandatory=$true)]
  [parameter(ParameterSetName="UserRepoCustomEvents",       Position=0,Mandatory=$true)]
  [ValidateNotNullOrEmpty()]
    [String] $RepoName,
  [parameter(ParameterSetName="NoOrgOrUserRepoCustomEvents",Mandatory=$true)]
  [parameter(ParameterSetName="OrgRepoCustomEvents",        Mandatory=$true)]
  [parameter(ParameterSetName="UserRepoCustomEvents",       Mandatory=$true)]
  [ValidateSet("A","B","C")]
    [String[]] $CustomEvents,
  [parameter(ParameterSetName="OrgRepoAllEvents",   Mandatory=$true)]
  [parameter(ParameterSetName="OrgRepoCustomEvents",Mandatory=$true)]
  [ValidateNotNullOrEmpty()]
    [String] $OrganizationName,
  [parameter(ParameterSetName="UserRepoAllEvents",   Mandatory=$true)]
  [parameter(ParameterSetName="UserRepoCustomEvents",Mandatory=$true)]
    [Switch] $IsUserRepo,
  [parameter(ParameterSetName="NoOrgOrUserRepoAllEvents",   Mandatory=$true)]
  [parameter(ParameterSetName="NoOrgOrUserRepoCustomEvents",Mandatory=$true)]
  [parameter(ParameterSetName="OrgRepoAllEvents",           Mandatory=$true)]
  [parameter(ParameterSetName="OrgRepoCustomEvents",        Mandatory=$true)]
  [parameter(ParameterSetName="UserRepoAllEvents",          Mandatory=$true)]
  [parameter(ParameterSetName="UserRepoCustomEvents",       Mandatory=$true)]
  [ValidateNotNullOrEmpty()]
    [String] $WebHookURL,
  [parameter(ParameterSetName="NoOrgOrUserRepoAllEvents")]
  [parameter(ParameterSetName="NoOrgOrUserRepoCustomEvents")]
  [parameter(ParameterSetName="OrgRepoAllEvents")]
  [parameter(ParameterSetName="OrgRepoCustomEvents")]
  [parameter(ParameterSetName="UserRepoAllEvents")]
  [parameter(ParameterSetName="UserRepoCustomEvents")]
    [Switch] $Create,
  [parameter(ParameterSetName="NoOrgOrUserRepoAllEvents")]
  [parameter(ParameterSetName="NoOrgOrUserRepoCustomEvents")]
  [parameter(ParameterSetName="OrgRepoAllEvents")]
  [parameter(ParameterSetName="OrgRepoCustomEvents")]
  [parameter(ParameterSetName="UserRepoAllEvents")]
  [parameter(ParameterSetName="UserRepoCustomEvents")]
    [Switch] $CommitComment
)

此设计不使用-AllEvents参数,而是-CustomEvents(这意味着&#34;并非所有事件&#34;)。