Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Feb 4, 2019. It is now read-only.
/PashPublic archive

Commit30cf305

Browse files
committed
Clear-Variable - Implement Include and Exclude parameters.
The Include and Exclude parameters are used to filter the variablesthat will be cleared.
1 parente873035 commit30cf305

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

‎Source/Microsoft.PowerShell.Commands.Utility/ClearVariableCommand.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@ namespace Microsoft.PowerShell.Commands
1212
publicsealedclassClearVariableCommand:VariableCommandBase
1313
{
1414
[Parameter]
15-
publicstring[]Exclude{get;set;}
15+
publicstring[]Exclude
16+
{
17+
get{returnExcludeFilters;}
18+
set{ExcludeFilters=value;}
19+
}
1620

1721
[Parameter]
1822
publicSwitchParameterForce{get;set;}
1923

2024
[Parameter]
21-
publicstring[]Include{get;set;}
25+
publicstring[]Include
26+
{
27+
get{returnIncludeFilters;}
28+
set{IncludeFilters=value;}
29+
}
2230

2331
[Parameter(Position=0,ValueFromPipelineByPropertyName=true,Mandatory=true)]
2432
publicstring[]Name{get;set;}
@@ -31,9 +39,6 @@ public sealed class ClearVariableCommand : VariableCommandBase
3139

3240
publicClearVariableCommand()
3341
{
34-
// MUST: take these out into the base
35-
Include=newstring[0];
36-
Exclude=newstring[0];
3742
}
3843

3944
protectedoverridevoidProcessRecord()
@@ -43,7 +48,7 @@ protected override void ProcessRecord()
4348
foreach(stringnameinName)
4449
{
4550
foreach(PSVariablevariableinGetVariables(name)
46-
.Where(v=>v.Visibility==SessionStateEntryVisibility.Public))
51+
.Where(v=>!IsExcluded(v)))
4752
{
4853
try
4954
{
@@ -82,6 +87,12 @@ IEnumerable<PSVariable> GetVariables(string name)
8287
}
8388
}
8489

90+
privateboolIsExcluded(PSVariablevariable)
91+
{
92+
returnvariable.Visibility!=SessionStateEntryVisibility.Public||
93+
IsExcluded(variable.Name);
94+
}
95+
8596
privatevoidCheckVariableCanBeChanged(PSVariablevariable)
8697
{
8798
if((variable.ItemOptions.HasFlag(ScopedItemOptions.ReadOnly)&&!Force)||

‎Source/ReferenceTests/Commands/ClearVariableTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,5 +303,44 @@ public void WildcardEscapedToClearVariableWithWildcardInName()
303303

304304
Assert.AreEqual("True"+Environment.NewLine,result);
305305
}
306+
307+
[Test]
308+
publicvoidIncludeNamesByWildcard()
309+
{
310+
stringresult=ReferenceHost.Execute(newstring[]{
311+
"$aa = 'aa'",
312+
"$ba = 'ba'",
313+
"Clear-Variable aa,ba -include b*",
314+
"$aa +\",\" + ($ba -eq $null).ToString()"
315+
});
316+
317+
Assert.AreEqual("aa, True"+Environment.NewLine,result);
318+
}
319+
320+
[Test]
321+
publicvoidExcludeNamesByWildcard()
322+
{
323+
stringresult=ReferenceHost.Execute(newstring[]{
324+
"$aa = 'aa'",
325+
"$ba = 'ba'",
326+
"Clear-Variable aa,ba -exclude b*",
327+
"($aa -eq $null).ToString() +\",\" + $ba"
328+
});
329+
330+
Assert.AreEqual("True, ba"+Environment.NewLine,result);
331+
}
332+
333+
[Test]
334+
publicvoidWildcardAndExcludeOneVariableName()
335+
{
336+
stringresult=ReferenceHost.Execute(newstring[]{
337+
"$aa = 'aa'",
338+
"$ab = 'ab'",
339+
"Clear-Variable a* -exclude aa",
340+
"$aa +\",\" + ($ab -eq $null).ToString()"
341+
});
342+
343+
Assert.AreEqual("aa, True"+Environment.NewLine,result);
344+
}
306345
}
307346
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp