Powershell: Would it be possible to have --help as an alias of -?

Created on 15 Mar 2018  路  5Comments  路  Source: PowerShell/PowerShell

In linux, obviously --help (or -h) is more common than -? as a way to get help

Since it doesn't currently seem possible to name a parameter such that this would collide, it seems like a change like this wouldn't break anything, right?

Any reason this _should not_ be done?

Issue-Discussion Issue-Enhancement

Most helpful comment

It would be great to get this via CmdletBinding().

All 5 comments

It would be great to get this via CmdletBinding().

@Jaykul It is possible to define a script/function parameter --help as in function foo ( ${-help} ) { ... }. I bet no one actually does this but maybe we should check around.

Any reason this should not be done?

The usual I suppose. It's work (open-source somewhat mitigates that). It clutters up the code with another special case in command processing (internally 'foo -?' gets rewritten as 'get-help foo'). Do we think it will bring sufficient benefit to warrant the code changes, that kind of thing.

No @BrucePay, the reason I said it doesn't seem possible is _that it won't work_:

function Test-Help {
[CmdletBinding()]
param(
[switch]${-help}
)
$PSBoundParameters
}

```
C:PS> Get-Command Test-Help -Syntax

Test-Help [--help] []

C:PS> Test-Help --Help
Test-Help : A positional parameter cannot be found that accepts argument '--Help'.
At line:1 char:1

  • Test-Help --Help
  • ~~~~

    • CategoryInfo : InvalidArgument: (:) [Test-Help], ParameterBindingException

    • FullyQualifiedErrorId : PositionalParameterNotFound,Test-Help

You can pass `--Help` to a **non-advanced** function (and look for it in `$Args`), but you can't actually **use** `-Help` as a parameter name as far as I can tell (I suppose you can't use - as the first character of a parameter anymore).

So if you define:
```posh
function Test-Help ([switch]${-help}) {
$PSBoundParameters
Write-Host "Help? ${-Help}"
Write-Host "Unbound args: $Args"
}

Note that the parameter isn't set, and "--Help" showed up in unbound $Args

C:\PS> Test-Help --Help
Help? False
Unbound args: --Help

@Jaykul Yes - there definitely seems to be a bug in the parameter binder here.

Yeah, so ... obviously PowerShell cannot emulate Linux-stype parameter binding, because in linux executables, --long parameters require double dashes because they're completely separate from the single-character versions, which can be _combined_ so e.g.: docker run -i -t is _the same_ as docker run -it.

In order to pull that off, you have to name the parameters with that in mind. Clearly we can't just implement that in PowerShell. It's out of the question (first because we don't _want_ to do that), because the syntax we have now is well known to thousands of people, and because most PowerShell commands have multiple parameters that start with the same letter, like:

Get-ChildItem -Filter *.png -Force -FollowSymlink -File

However, there are a couple of exceptions

  1. Help for commands, --help could be like -? -- a special parameter that could be intercepted by the shell and converted to Get-Help <command>. It's certainly not necessary to do this, but we know it won't clash with any existing parameters, because it's not currently possible to write a function that takes --help as a parameter (but it is possible to write one that takes -help, so it's probably not a good idea to use that). At the end of the day, it seems like it would be a simple tweak that would enable thousands of people's automatic instincts to get them help.
  2. pwsh itself. Unlike commands _within_ PowerShell, the pwsh command is obviously actually a linux command, that has to work _in linux_ and behave properly. Now, perhaps we can't quite fix all the parameter handling so it's similar to normal linux parameter binding (since we need to keep backwards compatibility), but we _could_ easily add --help ...
Was this page helpful?
0 / 5 - 0 ratings