-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
$PSBoundParameters for [switch] integers #19580
Comments
from Windows PowerShell Language Specification Version 3.0
The first parameter's symbol must be a char.
|
I know. |
To provide context:
Given that switches are always named (with an implied value), workarounds are limited and cumbersome. The only - surprising - exception is when calling via |
This works in my specific case. This workaround is for that. This issue is for improvement, not for discussion, it's a desired goal. I just hope that someday these wishes can be realized. Consider this a note. |
I understand what you're trying to do. I was providing context for your feature request - which is important when it comes to deciding whether or not the request should or can be implemented. Consider this a note too. As for the workaround: Yes, it is incidental to the proposal, but it is still of interest to those who'd also like to see your feature request implemented - either for use in the interim or indefinitely, if the feature request gets declined.
You're right - I missed that you're actively relying on positional parameter binding, irrespective of what parameter the arguments bind to (I've removed my claim that it doesn't work from my previous comment). If you're happy with the workaround despite its limitations (discussed below), you can stop reading. Just to note the constraints of this workaround:
The following addresses these constraints, but is hampered by the fact that the auxiliary parameter used cannot currently be hidden from the syntax diagram (you can only exclude it from tab-completion, via param (
[switch] $8, # These are now here just for the syntax diagram.
[switch] $16,
[switch] $256,
# Collect all (unbound) positional arguments expected to contain
# the switch "names" (negative numbers)
[Parameter(ValueFromRemainingArguments, DontShow)]
[string[]] ${(ignore)}
)
switch (${(ignore)})
{
-8 { 8 }
-16 { 16 }
-256 { 256 }
default {
if ($null -ne $_) { throw "Unknown switch: $_" }
}
} |
P.S., @237dmitry: I suggest updating the title of this issue to be more descriptive, along the lines of "Consider allowing (switch) parameter names that start with a digit / look like numbers" |
So if I declare Function foo {
param {
$a,
[switch]$1
)
# etc
} In I'm curious to see a piece of code where having numbers as switch names is a good user experience. As in #17767 it would be better if script writers were told not to do this. As a style point it is not good things which are possible values for the same thing as different switches (e.g. |
I combined three scripts -- colors-256, colors-16 and colors-8. They output the color palettes of the console. I thought it would be better (prettier) to switch between functions with |
It's a different world - standard Unix utilities - but options that are numbers are not uncommon there; e.g.:
But in general I agree that consistent resolution one way or the other is called for:
|
So really it is I wrote about this :-) https://jhoneill.github.io/powershell/2019/12/31/Data-in-the-data.html possibly the parameter sets thing at the start of that was from batting things back and forth with @mklement0 my memory on that is hazy - but not having a ✔️ for each possible value whether that's boolean DB columns or PowerShell switches is still a good design tip. But as that explains code often evolves that way.
|
Friends, you are taking this too seriously. I wrote that this is not a requirement, but only a point from (to) the wishlist. Maybe someday there will be a question about reworking the system of parameters and arguments, then the developers might pay attention to what ordinary users want. And in pwsh-20.4.0-preview.3 it will be possible to combine alphabetic parameters, numbers, and so on.
ping and ssh for example. PowerShell is a shell first and foremost, and its syntax should be short. All aliases are difficult to remember, and parameters are much more difficult. Compare:
|
I agree that at least allowing switches to have numeric names would be beneficial. I also agree that concision and interactive convenience are important, and that the parameter-restructuring discussion is incidental:
|
This is where the trouble lies. When a combine is made from one сmdlet. Yes, this is not Unix-way, which, in fairness, has also moved away from the one-command-one-action paradigm a long time ago. |
@237dmitry, it sounds like you're talking about something else. My point was: We shouldn't focus on how number-named switches may be avoided by different parameter design, but on whether it's useful to allow them in principle, given that a different parameter design is sometimes the wrong solution, and at other times not the only solution. |
I think yes it would be useful, but currently it's not possible due to the very concept of parameter handling. But why not add a parameter and argument parser that will determine which is which? For example, if no arguments are specified for a named parameter, then it may well have any name without delimiters in it. It's just that this name is compared with the declared one.
|
Really those are opposite sides of the same coin. If someone wrote a command with switches -ForegroundRed -ForegroundGreen -ForegroundBlue -ForegroundYellow etc, we'd say WOAH! Stop! have a -Foreground parameter with values. A switch should be "select behaviour" although sometimes it is "Set value to one of two options". So for example in the importexcel module we have -Bold -Italic -Underline and those set properties, and -Bold:$false removes bold. (No switch leaves bold as it is). Encouraging people to design commands well usually avoids the need for names that start with numbers. (That's avoidance through different design). If the initial design had allowed numbers people writing style guides would say "Don't do that". Is that a reason to say "On principle parameters beginning with numbers should never be made possible" ? Actually I don't think so if bad code is convenient people should write it. But if the language makes people write better code as a happy accident, then that probably should not be undone. For Executables which do their own command-line parsing (linux tools and Windows ones) can process arguments any way they please, but this has given rise to some dreadful user experiences. -0 says nul is a delimiter in one command ... OK but other characters can be delimiters and But of all things PowerShell developers could spend time on where would we rank changing argument parsing to work with parameters which start with a digit? I'd say it's too low to ever get time assigned to doing it. |
Maybe someday they will be rewrite powershell concepts and then will be changed rules of parameters and arguments. Once more time, I do not demand it right now, I only express hope for the future. In a year, after ten years. Why are you discussing something that you think will never happen? The developers themselves say that the declarative agreement on backward compatibility with Windows PowerShell is ending, which means that the syntax and rules will inevitably change. Already, there are thousands of differences, and in fact only visibility remains from compatibility. |
|
Yes, the request is only for |
Actually, there is another exception, and I'm surprised no one else has noticed this: parameter splatting. function Test-IntParam {
param( [switch]$2 )
$PSBoundParameters
}
$Params = @{
2 = $true
}
Test-IntParam @Params Implementing this elsewhere seems like it would break something fundamental with its parsing of negative numbers as integers. What I would recommend advocating for instead is in-line splatting, so you could do something like
Honestly, I'm bothered that the ruling only touched on it in the context of explicit parameters overriding splatted hashtables. It feels like its other merits were lost in the weeds. Might be worth bringing up again, and not just because it would allow for what you're wanting, but it would be a nice bonus. |
Good point, re splatting - I had never tried that. # Create sample script with a switch named -1
'[CmdletBinding()] param([switch] $1) "-1 value: $1"' > t.ps1
# OK: CLI call with -File
# -> '-1 value: True'
pwsh -NoProfile -File t.ps1 -1
# OK: splatting
# -> '-1 value: True'
$namedArgs = @{ 1 = $true }
./t.ps1 @namedArgs
# !! BROKEN: direct invocation
# -> "A positional parameter cannot be found that accepts argument '-1'"
./t.ps1 -1 Two out of three ain't bad! To me, the splatting discussion is a separate one (I liked @KirkMunro's proposal - see PowerShell/PowerShell-RFC#179 - which was an alternative to splatting without an intermediate variable, but it was declined), because a parameter that can solely be bound via splatting (in-session) is not worth having.
Not if you limit support to To narrow my previous summary down to what I think are the only sensible options:
Either way, documentation is needed - at the very least to document the current inconsistencies / limitations. |
Here's another way to work around this limitation - accept the negative integer values as-is:
Outputs:
|
True, but unfortunately you neither get tab completion that way nor do the permitted switches show up in the syntax diagram. Hence my attempt above, with nominal switches to support tab-completion and for display in the syntax diagram - with the blemish that you can't hide the aux. |
You can get sorta-janky tab completion with a bit of help from
I recognize it's not quite what's being requested here, but I'm far from convinced we have to choose between disallowing digit-only variable path parameters or make a special case for |
Fair enough, but that still leaves the syntax-diagram problem.
I'm unclear on how you think external applications factor into this, given that the concept of parameters and named arguments doesn't apply there. |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
1 similar comment
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |
Summary of the new feature / enhancement
Add possibility for integers as switch parameters.
For example I want to use switch parameters like
-8
,-16
,-256
in script.But I get the error:
That is,
-8
is interpreted as an argumentThe workaround:
But it would be better if the names of switch (and not only) parameters of this kind were perceived as they are. Literally.
Proposed technical implementation details (optional)
No response
The text was updated successfully, but these errors were encountered: