Skip to content

Commit 6a847f5

Browse files
committed
Simplify parameter sets
1 parent 02ccd84 commit 6a847f5

File tree

2 files changed

+46
-63
lines changed

2 files changed

+46
-63
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
2121
/// </summary>
2222
[Cmdlet(VerbsLifecycle.Invoke,
2323
"ScriptAnalyzer",
24-
DefaultParameterSetName = ParameterSet_Path_SuppressedOnly,
24+
DefaultParameterSetName = ParameterSet_Path,
2525
SupportsShouldProcess = true,
2626
HelpUri = "https://go.microsoft.com/fwlink/?LinkId=525914")]
2727
[OutputType(typeof(DiagnosticRecord), typeof(SuppressedRecord))]
2828
public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter
2929
{
30-
private const string ParameterSet_Path_SuppressedOnly = nameof(Path) + "_" + nameof(SuppressedOnly);
31-
private const string ParameterSet_Path_IncludeSuppressed = nameof(Path) + "_" + nameof(IncludeSuppressed);
32-
private const string ParameterSet_ScriptDefinition_SuppressedOnly = nameof(ScriptDefinition) + "_" + nameof(SuppressedOnly);
33-
private const string ParameterSet_ScriptDefinition_IncludeSuppressed = nameof(ScriptDefinition) + "_" + nameof(IncludeSuppressed);
30+
private const string ParameterSet_Path = "File";
31+
private const string ParameterSet_ScriptDefinition = nameof(ScriptDefinition);
3432

3533
#region Private variables
3634
List<string> processedPaths;
@@ -41,12 +39,7 @@ public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter
4139
/// Path: The path to the file or folder to invoke PSScriptAnalyzer on.
4240
/// </summary>
4341
[Parameter(Position = 0,
44-
ParameterSetName = ParameterSet_Path_IncludeSuppressed,
45-
Mandatory = true,
46-
ValueFromPipeline = true,
47-
ValueFromPipelineByPropertyName = true)]
48-
[Parameter(Position = 0,
49-
ParameterSetName = ParameterSet_Path_SuppressedOnly,
42+
ParameterSetName = ParameterSet_Path,
5043
Mandatory = true,
5144
ValueFromPipeline = true,
5245
ValueFromPipelineByPropertyName = true)]
@@ -63,12 +56,7 @@ public string Path
6356
/// ScriptDefinition: a script definition in the form of a string to run rules on.
6457
/// </summary>
6558
[Parameter(Position = 0,
66-
ParameterSetName = ParameterSet_ScriptDefinition_IncludeSuppressed,
67-
Mandatory = true,
68-
ValueFromPipeline = true,
69-
ValueFromPipelineByPropertyName = true)]
70-
[Parameter(Position = 0,
71-
ParameterSetName = ParameterSet_ScriptDefinition_SuppressedOnly,
59+
ParameterSetName = ParameterSet_ScriptDefinition,
7260
Mandatory = true,
7361
ValueFromPipeline = true,
7462
ValueFromPipelineByPropertyName = true)]
@@ -155,10 +143,8 @@ public string[] Severity
155143
}
156144
private string[] severity;
157145

158-
// TODO: This should be only in the Path parameter sets, and is ignored otherwise,
159-
// but we already have a test that depends on it being otherwise
160-
//[Parameter(ParameterSetName = ParameterSet_Path_IncludeSuppressed)]
161-
//[Parameter(ParameterSetName = ParameterSet_Path_SuppressedOnly)]
146+
// TODO: This should be only in the Path parameter set, and is ignored otherwise,
147+
// but we already have a test that depends on it being available with ScriptDefinition
162148
//
163149
/// <summary>
164150
/// Recurse: Apply to all files within subfolders under the path
@@ -174,22 +160,19 @@ public SwitchParameter Recurse
174160
/// <summary>
175161
/// ShowSuppressed: Show the suppressed message
176162
/// </summary>
177-
[Parameter(ParameterSetName = ParameterSet_Path_SuppressedOnly)]
178-
[Parameter(ParameterSetName = ParameterSet_ScriptDefinition_SuppressedOnly)]
163+
[Parameter]
179164
public SwitchParameter SuppressedOnly { get; set; }
180165

181166
/// <summary>
182167
/// Include suppressed diagnostics in the output.
183168
/// </summary>
184-
[Parameter(ParameterSetName = ParameterSet_Path_IncludeSuppressed, Mandatory = true)]
185-
[Parameter(ParameterSetName = ParameterSet_ScriptDefinition_IncludeSuppressed, Mandatory = true)]
169+
[Parameter]
186170
public SwitchParameter IncludeSuppressed { get; set; }
187171

188172
/// <summary>
189173
/// Resolves rule violations automatically where possible.
190174
/// </summary>
191-
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_Path_IncludeSuppressed)]
192-
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_Path_SuppressedOnly)]
175+
[Parameter(ParameterSetName = ParameterSet_Path)]
193176
public SwitchParameter Fix
194177
{
195178
get { return fix; }
@@ -284,6 +267,18 @@ protected override void BeginProcessing()
284267
}
285268
}
286269
#endif
270+
271+
if (IncludeSuppressed && SuppressedOnly)
272+
{
273+
ThrowTerminatingError(
274+
new ErrorRecord(
275+
new InvalidOperationException($"The {nameof(IncludeSuppressed)} and {nameof(SuppressedOnly)} parameters are mutually exclusive"),
276+
"SuppressionSwitchParametersInvalid",
277+
ErrorCategory.InvalidArgument,
278+
targetObject: null));
279+
return;
280+
}
281+
287282
Helper.Instance = new Helper(
288283
SessionState.InvokeCommand,
289284
this);

docs/markdown/Invoke-ScriptAnalyzer.md

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,20 @@ Evaluates a script or module based on selected best practice rules
1111

1212
## SYNTAX
1313

14-
### Path_SuppressedOnly (Default)
14+
### File (Default)
1515
```
1616
Invoke-ScriptAnalyzer [-Path] <String> [-CustomRulePath <String[]>] [-RecurseCustomRulePath]
1717
[-IncludeDefaultRules] [-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>] [-Recurse]
18-
[-SuppressedOnly] [-Fix] [-EnableExit] [-Settings <Object>] [-SaveDscDependency] [-ReportSummary] [-WhatIf]
19-
[-Confirm] [<CommonParameters>]
18+
[-SuppressedOnly] [-IncludeSuppressed] [-Fix] [-EnableExit] [-Settings <Object>] [-SaveDscDependency]
19+
[-ReportSummary] [-WhatIf] [-Confirm] [<CommonParameters>]
2020
```
2121

22-
### Path_IncludeSuppressed
23-
```
24-
Invoke-ScriptAnalyzer [-Path] <String> [-CustomRulePath <String[]>] [-RecurseCustomRulePath]
25-
[-IncludeDefaultRules] [-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>] [-Recurse]
26-
[-IncludeSuppressed] [-Fix] [-EnableExit] [-Settings <Object>] [-SaveDscDependency] [-ReportSummary] [-WhatIf]
27-
[-Confirm] [<CommonParameters>]
28-
```
29-
30-
### ScriptDefinition_IncludeSuppressed
22+
### ScriptDefinition
3123
```
3224
Invoke-ScriptAnalyzer [-ScriptDefinition] <String> [-CustomRulePath <String[]>] [-RecurseCustomRulePath]
33-
[-IncludeDefaultRules] [-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>]
34-
[-IncludeSuppressed] [-EnableExit] [-Settings <Object>] [-SaveDscDependency] [-ReportSummary] [-WhatIf]
35-
[-Confirm] [<CommonParameters>]
36-
```
37-
38-
### ScriptDefinition_SuppressedOnly
39-
```
40-
Invoke-ScriptAnalyzer [-ScriptDefinition] <String> [-CustomRulePath <String[]>] [-RecurseCustomRulePath]
41-
[-IncludeDefaultRules] [-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>]
42-
[-SuppressedOnly] [-EnableExit] [-Settings <Object>] [-SaveDscDependency] [-ReportSummary] [-WhatIf]
43-
[-Confirm] [<CommonParameters>]
25+
[-IncludeDefaultRules] [-ExcludeRule <String[]>] [-IncludeRule <String[]>] [-Severity <String[]>] [-Recurse]
26+
[-SuppressedOnly] [-IncludeSuppressed] [-EnableExit] [-Settings <Object>] [-SaveDscDependency]
27+
[-ReportSummary] [-WhatIf] [-Confirm] [<CommonParameters>]
4428
```
4529

4630
## DESCRIPTION
@@ -57,6 +41,8 @@ want to include and/or exclude from the evaluation.
5741
You can also include a rule in the analysis, but suppress the output of that rule for selected functions or scripts.
5842
This feature should be used only when absolutely necessary.
5943
To get rules that were suppressed, run Invoke-ScriptAnalyzer with the -SuppressedOnly parameter.
44+
To get include suppressed diagnostics in output with other diagnostics, use the -IncludeSuppressed parameter.
45+
Diagnostics have the boolean `IsSuppressed` property to allow differentiation.
6046
For instructions on suppressing a rule, see the description of the SuppressedOnly parameter.
6147

6248
For usage in CI systems, the -EnableExit exits the shell with an exit code equal to the number of error records.
@@ -216,7 +202,7 @@ To analyze files that are not in the root directory of the specified path, use a
216202

217203
```yaml
218204
Type: String
219-
Parameter Sets: Path_SuppressedOnly, Path_IncludeSuppressed
205+
Parameter Sets: File
220206
Aliases: PSPath
221207

222208
Required: True
@@ -368,7 +354,7 @@ To search the CustomRulePath recursively, use the RecurseCustomRulePath paramete
368354
369355
```yaml
370356
Type: SwitchParameter
371-
Parameter Sets: Path_SuppressedOnly, Path_IncludeSuppressed
357+
Parameter Sets: (All)
372358
Aliases:
373359

374360
Required: False
@@ -380,6 +366,7 @@ Accept wildcard characters: False
380366
381367
### -SuppressedOnly
382368
Returns rules that are suppressed, instead of analyzing the files in the path.
369+
Cannot be used with the -IncludeSuppressed parameter.
383370
384371
When you used SuppressedOnly, Invoke-ScriptAnalyzer returns a SuppressedRecord object
385372
(Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.SuppressedRecord).
@@ -389,7 +376,7 @@ For help, see the examples.
389376
390377
```yaml
391378
Type: SwitchParameter
392-
Parameter Sets: Path_SuppressedOnly, ScriptDefinition_SuppressedOnly
379+
Parameter Sets: (All)
393380
Aliases:
394381

395382
Required: False
@@ -408,7 +395,7 @@ It tries to preserve the file encoding but there are still some cases where the
408395
409396
```yaml
410397
Type: SwitchParameter
411-
Parameter Sets: Path_SuppressedOnly, Path_IncludeSuppressed
398+
Parameter Sets: File
412399
Aliases:
413400

414401
Required: False
@@ -509,7 +496,7 @@ Unlike ScriptBlock parameters, the ScriptDefinition parameter requires a string
509496
510497
```yaml
511498
Type: String
512-
Parameter Sets: ScriptDefinition_IncludeSuppressed, ScriptDefinition_SuppressedOnly
499+
Parameter Sets: ScriptDefinition
513500
Aliases:
514501

515502
Required: True
@@ -551,28 +538,29 @@ Accept pipeline input: False
551538
Accept wildcard characters: False
552539
```
553540
554-
### -IncludeSuppressed
555-
Include suppressed diagnostics in output.
541+
### -WhatIf
542+
Shows what would happen if the cmdlet runs. The cmdlet is not run.
556543
557544
```yaml
558545
Type: SwitchParameter
559-
Parameter Sets: Path_IncludeSuppressed, ScriptDefinition_IncludeSuppressed
560-
Aliases:
546+
Parameter Sets: (All)
547+
Aliases: wi
561548

562-
Required: True
549+
Required: False
563550
Position: Named
564551
Default value: None
565552
Accept pipeline input: False
566553
Accept wildcard characters: False
567554
```
568555
569-
### -WhatIf
570-
Shows what would happen if the cmdlet runs. The cmdlet is not run.
556+
### -IncludeSuppressed
557+
Include suppressed diagnostics in the output.
558+
Cannot be used with `-SuppressedOnly`.
571559

572560
```yaml
573561
Type: SwitchParameter
574562
Parameter Sets: (All)
575-
Aliases: wi
563+
Aliases:
576564
577565
Required: False
578566
Position: Named

0 commit comments

Comments
 (0)