Powershell: `-Parent` and `-Leaf` on `Split-Path` is not consistent

Created on 27 Feb 2018  路  9Comments  路  Source: PowerShell/PowerShell

Consider the following example:

Split-Path -Parent \\server\path

vs

Split-Path -Leaf \\server\path

this is really not consistent.

it should be -Parent and -Child or -Root and -Leaf

since we don't want to cause breaking changes... we should have a couple of parameter aliases:
-Root is an alias of -Parent and -Child is an alias of -Leaf

Most helpful comment

The original parameters Parent\Leaf should remain with root\child as appropriate aliases. Changing the original parameters will cause more problems than its worth

All 9 comments

@SigFyg since you're now kinda familiar with this portion of the code, would you like to give this a try? We're here to help!

Here are some docs on parameter aliases... should be a simple addition:
https://msdn.microsoft.com/en-us/library/dd878292(v=vs.85).aspx

@SigFyg is a student I met at HackIllinois.

I'd prefer -Root/-Leaf as parameters and -Parent/-Child as aliases.

The original parameters Parent\Leaf should remain with root\child as appropriate aliases. Changing the original parameters will cause more problems than its worth

+1, I don't think that fixing slight inconsistency worth the hustle of even adding the aliases, not to mention rename of the parameter with all the documentation implications. Another thing to consider is the knowledge base accumulated other the years on the internet - people will continue to use the old names because this is what google shows them on StackOverflow and such.

@vors certainly true for existing users. For new users (or even some existing users...) it's very natural to assume that if -Parent works, the natural counter-part is -Child, not -Leaf.

I can see the appeal of the strong correlation between "parent/child" but for me, -Leaf makes a stronger statement i.e. a "leaf node" has no children whereas a "child node" can have children.

@rkeithhill that's a good point and maybe explains the current behavior:

split-path a/b/c -parent
a/b
split-path a/b/c -leaf
c

If we follow this behavior, then -root should return a and -child should return b/c I guess? At this point, I'm tending to agree that parent/child != root/leaf

I agree that -Root is not a correct term to use here at all. The root directory of the path C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe isn't C:\WINDOWS\System32\WindowsPowerShell\v1.0\. It's C:\. The root of a path is the highest level, not one level higher.

Test-Path's parameters use -Container and -Leaf, but that doesn't make sense for Split-Path, either. What does "the container of C:\WINDOWS\System32\WindowsPowerShell\v1.0\" mean? For that matter, what does "the leaf of `C:\WINDOWS\System32\WindowsPowerShell\v1.0\" mean? Technically, v1.0 isn't a leaf. -Parent and -Child is the only thing that really makes sense here.

@AikenBM:

Re -Root:

I agree that -Root is inappropriate, for the reasons you state.

Re -Leaf:

Technically, v1.0 isn't a leaf.

It is, just in a different sense than used by Test-Path:

  • With Split-Path, a leaf is a property of an _abstract path_: It is a path's _last component_, irrespective of what that path represents in the real world (and whether it can have "children" or not). As such, _every_ path has a leaf.

  • By contrast, Test-Path examines precisely what _item type_ a path refers to and tests its _intrinsic (non-)ability_ to have child items. In this sense, there are providers that have no "leaf" items at all, such as the registry provider, whose items are (just) registry _keys_, and all keys can have child keys.

This conflation can be confusing, but you could argue that it is Test-Path's use of the term that is less intuitive.

Re -Child:

There is no need for symmetry of _terms_ here, because there is no symmetry of _concepts_:

The starting point is a given path, whose components Split-Path extracts.

All you need are terms for the _components_ you want to extract, and _parent_ and _leaf_ are sensible names for the components in question, from a tree/path viewpoint:

  • -Leaf is the input path's last component.
  • -Parent is the input path's parent path (the path you get when you drop the leaf component)

There is no "child" here - only the input path _itself_ - and its components.


In short: I don't see a need for a change here.

Was this page helpful?
0 / 5 - 0 ratings