@@ -16,12 +16,14 @@ namespace Microsoft.PowerShell.Commands
16
16
/// MSH paths that match the glob strings.
17
17
/// </summary>
18
18
[ 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 } ) ]
25
27
public class SplitPathCommand : CoreCommandWithCredentialsBase
26
28
{
27
29
#region Parameters
@@ -36,6 +38,17 @@ public class SplitPathCommand : CoreCommandWithCredentialsBase
36
38
/// </summary>
37
39
private const string leafSet = "LeafSet" ;
38
40
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
+
39
52
/// <summary>
40
53
/// The parameter set name to get the qualifier set.
41
54
/// </summary>
@@ -61,21 +74,12 @@ public class SplitPathCommand : CoreCommandWithCredentialsBase
61
74
/// </summary>
62
75
[ Parameter ( Position = 0 , ParameterSetName = parentSet , Mandatory = true , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true ) ]
63
76
[ 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 ) ]
64
79
[ Parameter ( Position = 0 , ParameterSetName = qualifierSet , Mandatory = true , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true ) ]
65
80
[ Parameter ( Position = 0 , ParameterSetName = noQualifierSet , Mandatory = true , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true ) ]
66
81
[ 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 ; }
79
83
80
84
/// <summary>
81
85
/// Gets or sets the literal path parameter to the command
@@ -86,13 +90,13 @@ public string[] LiteralPath
86
90
{
87
91
get
88
92
{
89
- return _paths ;
93
+ return Path ;
90
94
} // get
91
95
92
96
set
93
97
{
94
98
base . SuppressWildcardExpansion = true ;
95
- _paths = value ;
99
+ Path = value ;
96
100
} // set
97
101
} // LiteralPath
98
102
@@ -107,18 +111,7 @@ public string[] LiteralPath
107
111
/// </value>
108
112
///
109
113
[ 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 ; }
122
115
123
116
/// <summary>
124
117
/// Determines if the qualifier should be returned
@@ -131,19 +124,7 @@ public SwitchParameter Qualifier
131
124
/// </value>
132
125
///
133
126
[ 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 ; }
147
128
148
129
/// <summary>
149
130
/// Determines if the parent path should be returned
@@ -154,18 +135,7 @@ public SwitchParameter NoQualifier
154
135
/// </value>
155
136
///
156
137
[ 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 ;
169
139
170
140
/// <summary>
171
141
/// Determines if the leaf name should be returned
@@ -176,98 +146,47 @@ public SwitchParameter Parent
176
146
/// </value>
177
147
///
178
148
[ 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 ; }
185
150
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 ; }
191
172
192
173
/// <summary>
193
174
/// Determines if the path should be resolved before being parsed.
194
175
/// </summary>
195
176
/// <value></value>
196
177
[ 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 ; }
209
179
210
180
/// <summary>
211
181
/// Determines if the path is an absolute path.
212
182
/// </summary>
213
- ///
214
183
[ Parameter ( ParameterSetName = isAbsoluteSet ) ]
215
- public SwitchParameter IsAbsolute
216
- {
217
- get
218
- {
219
- return _isAbsolute ;
220
- } // get
184
+ public SwitchParameter IsAbsolute { get ; set ; }
221
185
222
- set
223
- {
224
- _isAbsolute = value ;
225
- } //set
226
- }
227
186
#endregion Parameters
228
187
229
188
#region parameter data
230
189
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 ;
271
190
272
191
#endregion parameter data
273
192
@@ -281,15 +200,15 @@ protected override void ProcessRecord()
281
200
{
282
201
StringCollection pathsToParse = new StringCollection ( ) ;
283
202
284
- if ( _resolve )
203
+ if ( Resolve )
285
204
{
286
205
CmdletProviderContext currentContext = CmdletProviderContext ;
287
206
288
- foreach ( string path in _paths )
207
+ foreach ( string path in Path )
289
208
{
290
209
// resolve the paths and then parse each one.
291
210
292
- Collection < PathInfo > resolvedPaths = null ;
211
+ Collection < PathInfo > resolvedPaths ;
293
212
294
213
try
295
214
{
@@ -387,7 +306,7 @@ protected override void ProcessRecord()
387
306
switch ( ParameterSetName )
388
307
{
389
308
case isAbsoluteSet :
390
- string ignored = null ;
309
+ string ignored ;
391
310
bool isPathAbsolute =
392
311
SessionState . Path . IsPSAbsolute ( pathsToParse [ index ] , out ignored ) ;
393
312
@@ -436,10 +355,6 @@ protected override void ProcessRecord()
436
355
437
356
case parentSet :
438
357
case literalPathSet :
439
- bool pathStartsWithRoot =
440
- pathsToParse [ index ] . StartsWith ( "\\ " , StringComparison . CurrentCulture ) ||
441
- pathsToParse [ index ] . StartsWith ( "/" , StringComparison . CurrentCulture ) ;
442
-
443
358
try
444
359
{
445
360
result =
@@ -462,13 +377,24 @@ protected override void ProcessRecord()
462
377
break ;
463
378
464
379
case leafSet :
380
+ case leafBaseSet :
381
+ case extensionSet :
465
382
try
466
383
{
384
+ // default handles leafSet
467
385
result =
468
386
SessionState . Path . ParseChildName (
469
387
pathsToParse [ index ] ,
470
388
CmdletProviderContext ,
471
389
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
+ }
472
398
}
473
399
catch ( PSNotSupportedException )
474
400
{
0 commit comments