EliziumNet/Loopz

Param Set Tool: DryRunner.resolve is too strict

Closed this issue · 0 comments

The whole point of this feature was to show up the ambiguities of a particular set of parameters when invoking a command. However the following code is too strict:

class DryRunner {

  [System.Management.Automation.CommandParameterSetInfo[]] Resolve([string[]]$params) {
    [System.Management.Automation.CommandParameterSetInfo[]]$candidateSets = `
      $this.CommandInfo.ParameterSets | Where-Object {
      $(
        (Test-ContainsAll $_.Parameters.Name $params) -and
        (Test-ContainsAll $params ($_.Parameters | Where-Object { $_.IsMandatory }).Name)
      )
    };

    return $candidateSets;
  } # Resolve
} # DryRunner

in particular this clause:

(Test-ContainsAll $params ($.Parameters | Where-Object { $.IsMandatory }).Name)

This will only include parameter sets that whose mandatory parameters are all included in the parameters provided. In a way this makes sense, because if the supplied parameters don't include a particular mandatory item, then that parameter set can't be considered a candiate. However, this also means that if we have some params that a particular parameter set may include all of, it won't be considered if some its mandatories are missing. Perhaps we can have another method 'Strict' which uses this existing logic where it considers the mandatory parameters and another Weak, which ignores the mandatory set.

The resolve functionality usd by unit tests can the strict version, but a query command can have the option, which one to use. So Show-InvokeReport should have another flag, Weak, which would invoke the Weak version, and if it's missing it will invoke the strict version:

class DryRunner {

  [System.Management.Automation.CommandParameterSetInfo[]] Weak([string[]]$params) {
    [System.Management.Automation.CommandParameterSetInfo[]]$candidateSets = `
      $this.CommandInfo.ParameterSets | Where-Object {
      $(
        (Test-ContainsAll $_.Parameters.Name $params)
      )
    };

    return $candidateSets;
  } # Resolve

  [System.Management.Automation.CommandParameterSetInfo[]] Strict([string[]]$params) {
    [System.Management.Automation.CommandParameterSetInfo[]]$candidateSets = `
      $this.CommandInfo.ParameterSets | Where-Object {
      $(
        (Test-ContainsAll $_.Parameters.Name $params) -and
        (Test-ContainsAll $params ($_.Parameters | Where-Object { $_.IsMandatory }).Name)
      )
    };

    return $candidateSets;
  } # Strict
} # DryRunner