-
Notifications
You must be signed in to change notification settings - Fork 7.7k
CLI: -File arguments don't recognize [bool] parameter values when passed as separate arguments #10838
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
Comments
Should we escape $ in $true? |
Good point, but it makes no difference here, because the target Note that the tests do succeed - both with literal # OK: literal '$true'
pwsh -noprofile -file ./test.ps1 -foo:`$true
# OK: literal 'True'
pwsh -noprofile -file ./test.ps1 -foo:$true |
It seems the same as: > [bool]'false'
True |
No, it's not the same: we're talking about the CLI here, where all arguments are of necessity initially strings, and you need some way to pass a Boolean when you use The inability to pass a Boolean with |
I see two way:
|
We don't need new parameter-parsing functionality, we just need to amend (fix) a previous fix: That fix was to allow literal strings This now works in principle, except in particular syntax forms, which should obviously not be treated any different than its equivalent forms. As stated, Note that @SteveL-MSFT, you implemented the original fix in #4178 - does this make sense to you? |
If somebody run ps1 file in pwsh session and then do the same in command line (copy-paster) different behavior may surprise the user. |
It may, but it shouldn't: If you want the same behavior as inside PowerShell, invoke the CLI with It was a deliberate design decision to treat all This doesn't preclude later conversions when binding to the script's parameters, such as from string to If you disagree with this design, I suggest you create a new issue. An exception to the design was deliberately put in place to allow binding strings The issue at hand is that this exception wasn't implemented comprehensively. |
However, there is one problematic aspect about how the exception was implemented, @SteveL-MSFT, though it may not matter much in practice: The Boolean-like strings are unconditionally converted to The right behavior is to retain them as strings in that event, just like a "number string" such as |
Conditional conversion means a binder enhancement which is likely unjustified complexity. |
@mklement0 seems like this is becoming a slippery slope. At some point, it seems that using |
I'm personally fine with leaving the unconditional conversion mentioned in my previous comment be, but I don't think there's anything slippery about the issue as original reported: Supporting a feature in only one syntax form, but not in others that should always result in the same behavior is a baffling inconsistency that is well worth fixing. |
@mklement0 Passing commands to an executable - any executable - requires that the arguments be passed as literal strings because that's what the operating systems support. All type information is lost so trying to get the exact semantics of calling a PowerShell function from within PowerShell is tilting at windmills. As @SteveL-MSFT says, after a while you have to accept good enough. If there's something that just doesn't work, then it needs to get fixed (encoding scriptblocks, Steve's fix) but otherwise it just leads to playing wack-a-mole with bugs. |
This is still an important issue. I hit it today. It still has not been addressed. I request that it be re-opened. Using powershell 7.4.5 from bash prompt on Ubuntu 22.04:
|
Uh oh!
There was an error while loading. Please reload this page.
In response to #4036, passing Booleans to
[switch]
parameters was fixed.However, the problem persists for
[bool]
parameters if you pass the value as a separate argument (-param value
), which is the typical syntax; by contrast,-param:value
(the form that you must use with[switch]
parameters) works fine (but using:
and whitespace --param: value
- is also broken).While
[bool]
parameters are rare, there's no reason for them not to be supported.Additionally, the
-param: value
syntax form (:
separator and whitespace) also doesn't work with[switch]
parameters, only-param:value
does (:
, but no whitespace).Also, given that
[bool]
parameters (but not[switch]
parameters) also accepts numbers inside PowerShell - notably1
for$true
and0
for$false
(e.g.,& { param([bool] $p) $p } 1
yields$true
) - passing numbers should be supported too.Steps to reproduce
Expected behavior
The test should pass.
Actual behavior
All tests except the ones with syntax form
-foo:$true
fail:Environment data
The text was updated successfully, but these errors were encountered: