Skip to content

Support user defined parameter aliases, e.g. -F to -Force for Remove-Item #2199

Closed

Description

@be5invis
No description provided.

Activity

  1. changed the title [-]Bind `Remove-Item`'s `-f` to `-Force` only.[/-] [+]Alias parameter -F to -Force for Remove-Item[/+] on Sep 7, 2016
  2. added
    Issue-Enhancement
    WG-Cmdlets
    on Sep 7, 2016
  3. lzybkr commented on Sep 7, 2016

    @lzybkr
    Contributor

    Having Remove-Item -F work just like Remove-Item -Force seems like a good idea.

    There is a small concern - Remove-Item is a cmdlet that is implemented by providers (like the file system, registery, environment, etc) and providers can add dynamic parameters. Providers that add dynamic parameters aren't all that common, but there is a risk that a provider adds a dynamic parameter with an identical alias -F which would be masked by this change.

    In an ideal world, this wouldn't break scripts because they should be using full parameter names, but we don't live in that ideal world.

    It feels like an unlikely breaking change, but we should have data to confirm that assumption before considering this enhancement.

  4. be5invis commented on Sep 7, 2016

    @be5invis
    Author

    Jason Shirk (@lzybkr) The current Remove-Item’s -f is... ambiguous (-Force and -Filter), so scripts cannot use it directly.

  5. lzybkr commented on Sep 7, 2016

    @lzybkr
    Contributor

    That's not necessarily true if, as I previously stated, a provider adds a dynamic parameter with an alias.

  6. be5invis commented on Sep 7, 2016

    @be5invis
    Author

    Jason Shirk (@lzybkr) At least can alias -f for file provider, or other existing providers with only -Force.

  7. lzybkr commented on Sep 7, 2016

    @lzybkr
    Contributor

    Belleve (@be5invis) - in theory, sure, but I think that would be difficult to implement correctly.

    Dynamic parameters don't support adding an alias to an existing parameter and we couldn't move -Force to be dynamic for a specific set of providers because the parameter must exist for all providers.

  8. GeeLaw commented on Sep 7, 2016

    @GeeLaw

    I'm proposing a way to implement this... Allow specifying the precedence of a parameter when ambiguity happens. E.g. [Parameter(Precedence = 1)][switch]$Force, [Parameter(Precedence = 0)][string]$Filter, then -F, if not otherwise resolved, would be -Force.

    For backward compatibility, Precedence should be System.Nullable<System.Int32> or System.Nullable<System.UInt32>. Resolution step regarding precedence (of course full name and aliases should be used to resolve parameters before we come to this step) should be:

    1. If no parameter candidate has Precedence.HasValue, the resolution fails;
    2. Collect all parameter candidates with Precedence.HasValue that has the largest Precedence.Value, say this is collection A;
    3. If the size of A is 1, the parameter resolves to the only element in A;
    4. Otherwise, the resolution fails.

    This won't break current providers that adds a dynamic parameter / an alias of a dynamic parameter -F, and is consistent in all providers -- You don't lose -F as an alias for -Force magically because you are not using built-in providers, you lose it because -F resolves before it reaches -Force and -Filter.

  9. lzybkr commented on Sep 7, 2016

    @lzybkr
    Contributor

    Ji Luo (@GeeLaw) - that might work, but the parameter binder is already too complicated, so it's hard to say if it'd be worth adding this complexity.

  10. iSazonov commented on Oct 6, 2016

    @iSazonov
    Collaborator

    Using aliases degrades readability of scripts.
    Using Foreach-Object better using foreach.
    Using Where-Object better using where.
    Using -Force better using -F.

  11. GeeLaw commented on Oct 6, 2016

    @GeeLaw

    In reply to Ilya (@iSazonov) : If you read carefully enough, you would find that the proposed alias -F for -Force is intended to be used interactively, shortening the type for a often-used switch.

  12. KirkMunro commented on Oct 6, 2016

    @KirkMunro
    Contributor

    I'd vote against building in a shorthand aliases for parameters (such as -F for -Force) because it could lead to accidents.. It's pretty well known that you can abbreviate any parameter using the minimum number of characters required to unambiguously identify that parameter. If -F is explicitly aliased to -Force, and someone forgets about the conflict between -Force and -Filter because there aren't multiple parameters that start with -F on every command that has a -Force parameter, suddenly they may invoke a command with -Force when they really meant to invoke it with -Filter.

    A better solution for this problem would be to unlock parameter aliases in PowerShell (they currently cannot be modified by end users), allowing PowerShell users to add parameter aliases that they want to use in their interactive work to their profile, much like they can add command aliases today. Then if someone specifically wants -F to be an alias for -Force in Remove-Item for ad-hoc work, they can make that decision and change it for themselves without forcing a potentially unwanted change on the rest of the community.

    In this particular scenario, having to type ri -fo vs just ri -f isn't really that bad IMHO.

  13. RichardSiddaway commented on Oct 6, 2016

    @RichardSiddaway

    I agree with Kirk that the present capabilities of abbreviating parameters to the minimum that is unambiguous is sufficient. Add aliases to parameters is an unnecessary complication

  14. iSazonov commented on Oct 6, 2016

    @iSazonov
    Collaborator

    An user can add parameter alias by means of proxy function.

  15. 9 remaining items

  16. KirkMunro commented on Nov 21, 2016

    @KirkMunro
    Contributor

    Hemant Mahawar (@HemantMahawar) To be clear, which part of this did the @PowerShell/powershell-committee agree is useful?

  17. SteveL-MSFT commented on Nov 21, 2016

    @SteveL-MSFT
    Member

    Kirk Munro (@KirkMunro) the committee agreed that user defined parameter aliases would be useful for interactive use (aliases should never be used in scripts and we should have a corresponding rule for this) where rm is aliased to remove-item and -rf is aliased to -recurse -force. seemsl like this should be a RFC to get more feedback.

  18. changed the title [-]Alias parameter -F to -Force for Remove-Item[/-] [+]Support user defined parameter aliases, e.g. -F to -Force for Remove-Item[/+] on Nov 21, 2016
  19. dragonwolf83 commented on Nov 25, 2016

    @dragonwolf83

    Limiting to interactive scenarios would be most important for this to work. I'm not a Bash user. If I inherit a script that someone wrote with Linux aliases, it would make it much harder to understand the script.

    PowerShell needs to maintain consistency with scripts across platforms in order to make it easier for scripters to maintain in the long-term.

  20. added
    Committee-Reviewed
    on May 7, 2017
  21. jdhitsolutions commented on Jun 14, 2021

    @jdhitsolutions
    Collaborator

    I don't see any reason to invest resources into providing this feature. Parameter aliases add an unnecessary layer of complexity. Going back to the original post, there's no reason to run a command like rm -f, when you can just as easily type rm -f<tab> and autocomplete the parameter. I'd make a case for tab-completing the cmdlet name as well. If there are parameter aliases that a user might be expecting to use, that should be well documented.

  22. removed
    Up-for-Grabs
    on Jun 24, 2021
  23. SteveL-MSFT commented on Jun 24, 2021

    @SteveL-MSFT
    Member

    Upon further thinking on this, I don't know if this is worth a feature to enable custom parameter aliases if the primary (only?) use case is rm -rf. So a feature would not be trivial and currently without other real world scenarios, seems potentially little benefit vs the cost. For Remove-Item specifically, I have personally encountered the displeasure of having to use rm -r -fo (since -filter conflicts) and prefer rm -rf for expedient typing when used interactively.

    So one possible alternate solution is to add a new -RecurseForce switch in a new parameterset which has the -rf alias. This should be a pretty small change. I think there is a question whether this sets a bad precedent.

  24. iSazonov commented on Jun 25, 2021

    @iSazonov
    Collaborator

    I think there is a question whether this sets a bad precedent.

    Yes. Users can create any wrappers based on functions in their profiles.

  25. SeeminglyScience commented on Jun 25, 2021

    @SeeminglyScience
    Contributor

    So one possible alternate solution is to add a new -RecurseForce switch in a new parameterset which has the -rf alias. This should be a pretty small change. I think there is a question whether this sets a bad precedent.

    It does, but I think if it solved the problem it would be worth it. I would bet that just about every single person who used PowerShell extensively has been annoyed by this at some point.

    That said, I think the main frustration is "I feel like should be able to do this but that throws, now I gotta do that". I don't think the pain point is specifically the extra character, so I'm not sure how much this would actually help in the end.

  26. vexx32 commented on Jun 25, 2021

    @vexx32
    Collaborator

    It is worth discussing whether the parameter binder should, as a final fallback if nothing matches the given parameter name unambiguously, mimic behaviour from how these tools handle shorthand switches?

    e.g., Remove-Item -rf where -rf is not able to be matched to any singular parameter name, should it enumerate switch parameters and see if it can unambiguously assign multiple parameters, one to each character specified?

  27. joeyaiello commented on Jun 30, 2021

    @joeyaiello
    Contributor

    @PowerShell/wg-powershell-cmdlets has discussed this, and despite the fact that a couple of us have the rm -rf muscle memory, we are rejecting this proposal for the following reasons:

    • it creates an unreasonable expectation of consistency between platforms (e.g. rm -rf on macOS/Linux actually executes /bin/rm while it would execute Remove-Item on Windows)
    • the implementation is complex and rife with potential for regression around the parameter binder

    We also don't want to pursue the -RecurseForce as a one-off for Remove-Item because of the precedent that it sets with creating arguments like that for other cmdlets

    If folks feel very passionately about having this functionality, the community is welcome to create a Gallery module that has well-formed wrapper functions for common scenarios like this. That module could even end up being configurable for folks with slightly different muscle memory requirements

  28. added
    Resolution-Declined
    on Jun 30, 2021
  29. ghost closed this as completedon Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedIssue-EnhancementResolution-DeclinedWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions