8000 Adding -Extension and -LeafBase switches to Split-Path (#2721) · PowerShell/PowerShell@220eaa1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 220eaa1

Browse files
Staffan Gustafssondaxian-dbw
authored andcommitted
Adding -Extension and -LeafBase switches to Split-Path (#2721)
* Refactoring ParsePathCommand.cs (SplitPathCommand) for readability - Using auto properties when no when there is no logic in getter/setter - Removing unused code - Removing redundant qualifiers - Removing Redundant initializers * Add -Extension and -Leafbase switches to Split-Path cmdlet - Extension and LeafBase are specializations of Leaf, and uses System.IO.Path.GetExtension and System.IO.Path.GetFilenameWithoutExtension to extract parts from the Leaf * Adding tests for Split-Path -LeafBase and Split-Path -Extension
1 parent 63ba98f commit 220eaa1

File tree

2 files changed

+83
-140
lines changed

2 files changed

+83
-140
lines changed

src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs

Lines changed: 66 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ namespace Microsoft.PowerShell.Commands
1616
/// MSH paths that match the glob strings.
1717
/// </summary>
1818
[Cmdlet(VerbsCommon.Split, "Path", DefaultParameterSetName = "ParentSet", SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113404")]
19-
[OutputType(typeof(string), ParameterSetName = new string[] { SplitPathCommand.leafSet,
20-
SplitPathCommand.noQualifierSet,
21-
SplitPathCommand.parentSet,
22-
SplitPathCommand.qualifierSet,
23-
SplitPathCommand.literalPathSet})]
24-
[OutputType(typeof(bool), ParameterSetName = new string[] { SplitPathCommand.isAbsoluteSet })]
19+
[OutputType(typeof(string), ParameterSetName = new[] { leafSet,
20+
leafBaseSet,
21+
extensionSet,
22+
noQualifierSet,
23+
parentSet,
24+
qualifierSet,
25+
literalPathSet})]
26+
[OutputType(typeof(bool), ParameterSetName = new[] { isAbsoluteSet })]
2527
public class SplitPathCommand : CoreCommandWithCredentialsBase
2628
{
2729
#region Parameters
@@ -36,6 +38,17 @@ public class SplitPathCommand : CoreCommandWithCredentialsBase
3638
/// </summary>
3739
private const string leafSet = "LeafSet";
3840

41+
/// <summary>
42+
/// The parameter set name to get the leaf base name
43+
/// </summary>
44+
private const string leafBaseSet = "LeafBaseSet";
45+
46+
/// <summary>
47+
/// The parameter set name to get the extension
48+
/// </summary>
49+
private const string extensionSet = "ExtensionSet";
50+
51+
3952
/// <summary>
4053
/// The parameter set name to get the qualifier set.
4154
/// </summary>
@@ -61,21 +74,12 @@ public class SplitPathCommand : CoreCommandWithCredentialsBase
6174
/// </summary>
6275
[Parameter(Position = 0, ParameterSetName = parentSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
6376
[Parameter(Position = 0, ParameterSetName = leafSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
77+
[Parameter(Position = 0, ParameterSetName = leafBaseSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
78+
[Parameter(Position = 0, ParameterSetName = extensionSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
6479
[Parameter(Position = 0, ParameterSetName = qualifierSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
6580
[Parameter(Position = 0, ParameterSetName = noQualifierSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
6681
[Parameter(Position = 0, ParameterSetName = isAbsoluteSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
67-
public string[] Path
68-
{
69-
get
70-
{
71-
return _paths;
72-
} // get
73-
74-
set
75-
{
76-
_paths = value;
77-
} // set
78-
} // Path
82+
public string[] Path { get; 10000 set; }
7983

8084
/// <summary>
8185
/// Gets or sets the literal path parameter to the command
@@ -86,13 +90,13 @@ public string[] LiteralPath
8690
{
8791
get
8892
{
89-
return _paths;
93+
return Path;
9094
} // get
9195

9296
set
9397
{
9498
base.SuppressWildcardExpansion = true;
95-
_paths = value;
99+
Path = value;
96100
} // set
97101
} // LiteralPath
98102

@@ -107,18 +111,7 @@ public string[] LiteralPath
107111
/// </value>
108112
///
109113
[Parameter(Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = qualifierSet, Mandatory = false)]
110-
public SwitchParameter Qualifier
111-
{
112-
get
113-
{
114-
return _qualifier;
115-
} // get
116-
117-
set
118-
{
119-
_qualifier = value;
120-
} //set
121-
} // Qualifier
114+
public SwitchParameter Qualifier { get; set; }
122115

123116
/// <summary>
124117
/// Determines if the qualifier should be returned
@@ -131,19 +124,7 @@ public SwitchParameter Qualifier
131124
/// </value>
132125
///
133126
[Parameter(ParameterSetName = noQualifierSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
134-
public SwitchParameter NoQualifier
135-
{
136-
get
137-
{
138-
return _noqualifier;
139-
} // get
140-
141-
set
142-
{
143-
_noqualifier = value;
144-
} //set
145-
} // NoQualifier
146-
127+
public SwitchParameter NoQualifier { get; set; }
147128

148129
/// <summary>
149130
/// Determines if the parent path should be returned
@@ -154,18 +135,7 @@ public SwitchParameter NoQualifier
154135
/// </value>
155136
///
156137
[Parameter(ParameterSetName = parentSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
157-
public SwitchParameter Parent
158-
{
159-
get
160-
{
161-
return _parent;
162-
} // get
163-
164-
set
165-
{
166-
_parent = value;
167-
} //set
168-
} // Parent
138+
public SwitchParameter Parent { get; set; } = true;
169139

170140
/// <summary>
171141
/// Determines if the leaf name should be returned
@@ -176,98 +146,47 @@ public SwitchParameter Parent
176146
/// </value>
177147
///
178148
[Parameter(ParameterSetName = leafSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
179-
public SwitchParameter Leaf
180-
{
181-
get
182-
{
183-
return _leaf;
184-
} // get
149+
public SwitchParameter Leaf { get; set; }
185150

186-
set
187-
{
188-
_leaf = value;
189-
} //set
190-
} // Leaf
151+
/// <summary>
152+
/// Determines if the leaf base name (name without extension) should be returned
153+
/// </summary>
154+
///
155+
/// <value>
156+
/// If true the leaf base name of the path will be returned.
157+
/// </value>
158+
///
159+
[Parameter(ParameterSetName = leafBaseSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
160+
public SwitchParameter LeafBase { get; set; }
161+
162+
/// <summary>
163+
/// Determines if the extension should be returned
164+
/// </summary>
165+
///
166+
/// <value>
167+
/// If true the extension of the path will be returned.
168+
/// </value>
169+
///
170+
[Parameter(ParameterSetName = extensionSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
171+
public SwitchParameter Extension { get; set; }
191172

192173
/// <summary>
193174
/// Determines if the path should be resolved before being parsed.
194175
/// </summary>
195176
/// <value></value>
196177
[Parameter]
197-
public SwitchParameter Resolve
198-
{
199-
get
200-
{
201-
return _resolve;
202-
} // get
203-
204-
set
205-
{
206-
_resolve = value;
207-
} //set
208-
} // Resolve
178+
public SwitchParameter Resolve { get; set; }
209179

210180
/// <summary>
211181
/// Determines if the path is an absolute path.
212182
/// </summary>
213-
///
214183
[Parameter(ParameterSetName = isAbsoluteSet)]
215-
public SwitchParameter IsAbsolute
216-
{
217-
get
218-
{
219-
return _isAbsolute;
220-
} // get
184+
public SwitchParameter IsAbsolute { get; set; }
221185

222-
set
223-
{
224-
_isAbsolute = value;
225-
} //set
226-
}
227186
#endregion Parameters
228187

229188
#region parameter data
230189

231-
/// <summary>
232-
/// The path to resolve
233-
/// </summary>
234-
private string[] _paths;
235-
236-
/// <summary>
237-
/// Determines if the qualifier of the path should be returned.
238-
/// The qualifier is either the drive name or provider name that
239-
/// is qualifying the path.
240-
/// </summary>
241-
private bool _qualifier;
242-
243-
/// <summary>
244-
/// Determines if the qualifier of the path should be returned.
245-
/// If false, the qualifier will be returned. If true, it will
246-
/// be stripped from the path.
247-
/// The qualifier is either the drive name or provider name that
248-
/// is qualifying the path.
249-
/// </summary>
250-
private bool _noqualifier;
251-
252-
/// <summary>
253-
/// Determines if the parent path of the specified path should be returned.
254-
/// </summary>
255-
private bool _parent = true;
256-
257-
/// <summary>
258-
/// Determines if the leaf name of the specified path should be returned.
259-
/// </summary>
260-
private bool _leaf;
261-
262-
/// <summary>
263-
/// Determines if the path(s) should be resolved before being parsed.
264-
/// </summary>
265-
private bool _resolve;
266-
267-
/// <summary>
268-
/// Determines if the path(s) are absolute paths.
269-
/// </summary>
270-
private bool _isAbsolute;
271190

272191
#endregion parameter data
273192

@@ -281,15 +200,15 @@ protected override void ProcessRecord()
281200
{
282201
StringCollection pathsToParse = new StringCollection();
283202

284-
if (_resolve)
203+
if (Resolve)
285204
{
286205
CmdletProviderContext currentContext = CmdletProviderContext;
287206

288-
foreach (string path in _paths)
207+
foreach (string path in Path)
289208
{
290209
// resolve the paths and then parse each one.
291210

292-
Collection<PathInfo> resolvedPaths = null;
211+
Collection<PathInfo> resolvedPaths;
293212

294213
try
295214
{
@@ -387,7 +306,7 @@ protected override void ProcessRecord()
387306
switch (ParameterSetName)
388307
{
389308
case isAbsoluteSet:
390-
string ignored = null;
309+
string ignored;
391310
bool isPathAbsolute =
392311
SessionState.Path.IsPSAbsolute(pathsToParse[index], out ignored);
393312

@@ -436,10 +355,6 @@ protected override void ProcessRecord()
436355

437356
case parentSet:
438357
case literalPathSet:
439-
bool pathStartsWithRoot =
440-
pathsToParse[index].StartsWith("\\", StringComparison.CurrentCulture) ||
441-
pathsToParse[index].StartsWith("/", StringComparison.CurrentCulture);
442-
443358
try
444359
{
445360
result =
@@ -462,13 +377,24 @@ protected override void ProcessRecord()
462377
break;
463378

464379
case leafSet:
380+
case leafBaseSet:
381+
case extensionSet:
465382
try
466383
{
384+
// default handles leafSet
467385
result =
468386
SessionState.Path.ParseChildName(
469387
pathsToParse[index],
470388
CmdletProviderContext,
471389
true);
390+
if (LeafBase)
391+
{
392+
result = System.IO.Path.GetFileNameWithoutExtension(result);
393+
}
394+
else if (Extension)
395+
{
396+
result = System.IO.Path.GetExtension(result);
397+
}
472398
}
473399
catch (PSNotSupportedException)
474400
{

test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Featur
569569
$level1_0 = "Level1_0"
570570
$level2_0 = "Level2_0"
571571
$level2_1 = "Level2_1"
572+
$fileExt = ".ext"
572573
$root = Join-Path "TestDrive:" "" #adds correct / or \
573574
$level1_0Full = Join-Path $root $level1_0
574575
$level2_0Full = Join-Path $level1_0Full $level2_0
@@ -628,6 +629,22 @@ Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Featur
628629
$result = Split-Path -Path $level1_0Full -Leaf
629630
$result | Should Be $level1_0
630631
}
632+
633+
It 'Validate LeafBase' {
634+
$result = Split-Path -Path "$level2_1Full$fileExt" -LeafBase
635+
$result | Should Be $level2_1
636+
}
637+
638+
It 'Validate LeafBase is not over-zealous' {
639+
640+
$result = Split-Path -Path "$level2_1Full$fileExt$fileExt" -LeafBase
641+
$result | Should Be "$level2_1$fileExt"
642+
}
643+
644+
It 'Validate LeafBase' {
645+
$result = Split-Path -Path "$level2_1Full$fileExt" -Extension
646+
$result | Should Be $fileExt
647+
}
631648

632649
It "Validate NoQualifier" {
633650
$result = Split-Path -Path $level1_0Full -NoQualifier

0 commit comments

Comments
 (0)
0