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
For native commands, variables are not expanded if argument begins with -
(-key=$var
, -D$var
)
#21460
Comments
-
(-Dkey=$var
)-
(-key=$var
, -D$var
)
If the language allows people to use variables in parameter names then people will start doing things like: |
@MartinGC94 No, I don't want this for built-in commands, where the parser unambiguously knows what is a parameter name and what is a parameter value. However, for native commands, parser cannot ever reliably know that, and it seems more intuitive to just give up and allow expanding variables everywhere instead of allowing variables in some parameter values but not others. |
@237dmitry I commonly encounter this with compiler pre-processor defines ( |
@MartinGC94 Ad syntax highlighting, it does not help that everything except PSReadline (I tested VS Code, NeoVim and Sublime Text) highlights the variable incorrectly, as if it was expanded. |
You can't make it work like this for native programs without affecting PowerShell commands because the parser won't know whether or not it's a native program. Maybe you could do some trickery at runtime when it knows that it's a native command but then it would be confusing behavior because the syntax highlighting would be inaccurate. |
Hmm, that's a good point, that making the parser depend on knowing if it's parsing a native command or a cmdlet is probably not feasible, and would be a big can of worms even if theoretically possible. Nevertheless, I would still prefer if variables would be expanded everywhere, even in parameter names, in hope that no one in their right mind will actually misuse it in the way you've shown above. While I agree that it would be nice to disallow variables in parameter names, I consider it more important for variable expansion to behave consistently, following the principle of least astonishment – if users encounter |
Note that there's a number of bugs (9 as of this writing) relating to Most of them also affect calls to PowerShell commands, notably cmdlets with In fact, the That is, I can't speak to the difficulty of fixing these problems, but I imagine that it is possible. E.g., given that Also: 7.3 unfortunately, introduced a new problem relating to use of
|
The workaround is to use quotes around the variable: /bin/echo -d"$var"
/bin/echo -d="$var" |
This issue follows up on one aspect of #6467. Many native commands use
-
prefix for named parameters, but either separate the parameter value with=
(-param=value
) or use no separator at all (e.g. GCC's-Ddefine
).Due to PowerShell parsing rules, even for native commands, anything starting with
-
is parsed as a possible parameter name, and PowerShell does not expand variables in suspected parameter names:This behavior surprises me each time I encounter it, especially because it often does not result in any error, just the command silently doing something wrong:
I would argue that it would be much more intuitive to disable special parsing of arguments for native executables and treat tokens starting with
-
as an ordinary argument – native commands don't have a consistent way of parsing arguments, and assuming anything about the way they're parsed (except forargv
splitting rules) will imo always lead to hard-to-understand edge cases.While this would be a breaking change, I strongly suspect that it falls in
Bucket 3: Unlikely Grey Area
, since parameter names or values intentionally containing$
are not common in practice, and I would expect most PowerShell users to defensively escape them anyway.GetCommandLine is the following C program, which just prints the command line exactly as it receives it:
The text was updated successfully, but these errors were encountered: