Title: Inconsistent, unpredictable behavior of "-"-prefixed barewords in argument mode · Issue #4624 · PowerShell/PowerShell · GitHub
Open Graph Title: Inconsistent, unpredictable behavior of "-"-prefixed barewords in argument mode · Issue #4624 · PowerShell/PowerShell
X Title: Inconsistent, unpredictable behavior of "-"-prefixed barewords in argument mode · Issue #4624 · PowerShell/PowerShell
Description: Follow-up to #4576 and #4591 Context: Note: By bareword I mean a run of non-whitespace characters not single- or double-quoted as a whole (e.g., foo), used in argument mode, and normally interpreted as an expandable string (e.g., $HOME/b...
Open Graph Description: Follow-up to #4576 and #4591 Context: Note: By bareword I mean a run of non-whitespace characters not single- or double-quoted as a whole (e.g., foo), used in argument mode, and normally interprete...
X Description: Follow-up to #4576 and #4591 Context: Note: By bareword I mean a run of non-whitespace characters not single- or double-quoted as a whole (e.g., foo), used in argument mode, and normally interprete...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/4624
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inconsistent, unpredictable behavior of \"-\"-prefixed barewords in argument mode","articleBody":"\u003csup>Follow-up to #4576 and #4591\u003c/sup>\r\n\r\n### Context:\r\n\r\nNote: By _bareword_ I mean a run of non-whitespace characters not single- or double-quoted as a whole (e.g., `foo`), used in argument mode, and normally interpreted as an expandable string (e.g., `$HOME/boy`).\r\n\r\nWhile `-`-prefixed barewords _look_ like _parameter names_ (e.g.,`-foo`), they _sometimes_ act as _arguments_ (_values_ to bind to parameters), and the ability to use them as such is important in two contexts:\r\n\r\n* In the context of the external CLI, when passing the name of a script file that happens to start with `-`; e.g., `powershell -File -foo.ps1`\r\n\r\n* When passing arguments _through_ that themselves happen to be command line or command-line fragments, which is enabled by cmdlets/advanced functions that declare a `ValueFromRemainingArguments` parameter; e.g., \r\n`Get-Command -Syntax Get-ChildItem -Path cert:` passes `-Path cert:` through (the `-Path` bareword is _not_ interpreted as `Get-Command`'s parameter) in order to discover the certificate provider's dynamic parameters.\r\n\r\nNote that, except when using the external CLI, you can always use _quoting_ to explicitly mark a bareword as an _argument_.\r\n\r\nGenerally, though, if there's no ambiguity, not having to quote tokens that don't strictly need it is a matter of convenience.\r\n\r\n### The problem:\r\n\r\nThere are two major problems with how `-`-prefixed barewords are currently parsed and interpreted:\r\n\r\n* Pragmatically speaking, **their syntactic role (parameter name vs. argument) is virtually unpredictable by the end user**. \r\n\r\n * E.g., the `-a` in `Get-Item -LiteralPath -a` is an _argument_, whereas the `-c` in `Get-Item -LiteralPath -c` is _parameter name_`-Credential`.\r\n\r\n* They are **not parsed and expanded in the same way as barewords that do _not_ start with `-`**.\r\n\r\n * E.g., bareword `a$HOME` expands `$HOME`, while `-a$HOME` does not.\r\n\r\nMore examples below.\r\n\r\n### Solution options:\r\n\r\n* Leave things as they are and _document_ the current behavior, recommending that _quoting_ be used for predictability.\r\n\r\n* Change the parameter binding as proposed in #4576: Once a parameter has been (unambiguously) identified, its specific definition - switch vs. parameter-with-argument- _alone_ determines whether the next token is its argument or a different parameter:\r\n\r\n * E.g., in `Get-Item -LiteralPath -p`, `-p` would _unambiguously_ be an _argument_, because `-LiteralPath` _syntactically requires_ an argument.\r\n\r\n * An argument can be made that in cases where the _user intent_ was likely different - e.g., `Get-Item -LiteralPath -Path` - that _unconditional_ interpretation of `-`-prefixed barewords as _parameter names_ is preferable in terms of the user experience (`Missing an argument for parameter 'LiteralPath'` as opposed to `Cannot find path '-Path' because it does not exist`); a point that [@markekraus has made](https://github.com/PowerShell/PowerShell/issues/4576#issuecomment-322574632).\r\n\r\n * That said, I think the conceptual simplicity and clarity of the let-the-identified-parameter-determine-the-next-token's-syntactic-role approach is more beneficial in the grand scheme of things.\r\n\r\n * Note: [@lzybkr has already discounted this option](https://github.com/PowerShell/PowerShell/issues/4576#issuecomment-322572999) based on implementation cost, but I'm resubmitting it for consideration now that we have a fuller picture of all the issues involved: \r\n> I can't see adding more complexity to the parameter binder to change this behavior.\r\n\r\n\r\n* Disallow `-`-prefixed barewords altogether, as proposed in #4591: In order for a `-`-prefixed tokens to be recognized as an _argument_, it would have to be _single- or double-quoted_ .\r\n\r\n * E.g., `Get-Item -LiteralPath -a` would then _break_, because `Get-Item` has no `-a*` parameter, and `Get-Item -LiteralPath '-a'` would have to used consistently. (But, in contrast, PowerShell's _outside_ CLI would still need to accept something like `-File -a`.)\r\n\r\n * The convenience of being able to specify `-`-prefixed tokens _as arguments_ is lost, but at least the behavior is _predictable_.\r\n\r\n * That said, the waters would still be muddy with respect to commands that have a `ValueFromRemainingArguments` parameter, where, based on the current behavior, a `-`-prefixed barewords _if it happens not to match a regular parameter_ would still be interpreted as an _argument_.\r\n\r\n### Examples:\r\n\r\n```powershell\r\n### Virtually unpredictable syntactic role\r\n\r\n# BREAKS\r\n# -c, because it happens to match parameter name -Credential, is interpreted as a *parameter name*.\r\n# Note that, purely based on -LiteralPath being a parameter that *requires an argument*, there is\r\n# no good reason to parse -c as a parameter name. The syntax of Get-Item unambigously implies that\r\n# the token following -LiteralPath must be an *argument*.\r\nGet-Item -LiteralPath -c\r\n\r\n# SUCCEEDS\r\n# -a, because it happens not to match any of Get-Item's parameters, is interpreted as an *argument*\r\nGet-Item -LiteralPath -a\r\n\r\n\r\n# Even though a \"-\"-prefixed token as the 1st argument is normally *invariably* interpreted as \r\n# a *parameter name*, that is not the case for cmdlets with a ValueFromRemainingArguments parameter,\r\n# such as Write-Output:\r\n\r\n# BREAKS\r\n# -n happens to match parameter (switch) -NoEnumeration, and the call is therefore missing\r\n# arguments to output (-InputObject)\r\nWrite-Output -n\r\n# Solution: Use -- to signal the start of all positional arguments.\r\nWrite-Output -- -n\r\n\r\n# SUCCEEDS, because -z happens not to match a parameter name and is therefore bound to -InputObject.\r\nWrite-Output -z\r\n\r\n### Parsing / expansion inconsistencies\r\n\r\n# As part of an array, a \"-\"-prefixed bareword doesn't work as the 1st element.\r\nWrite-Output -a, b\t# BREAKS\r\nWrite-Output a, -b # SUCCEEDS\r\n\r\n\r\n# \"-\"-prefixed barewords are non-expanding\r\nWrite-Output a$HOME # expands $HOME\r\nWrite-Output -a$HOME # doesn't expand $HOME\r\n\r\n\r\n# A \"-\"-prefixed bareword containing a \".\"-prefixed suffix is parsed as *2* arguments\r\nGet-Item -LiteralPath a.ps1 # SUCCEEDS\r\nGet-Item -LiteralPath -a.ps1 # BREAKS: Get-Item receives *2* arguments: '-a' and '.ps1'\r\n```\r\n\r\nEnvironment data\r\n----------------\r\n\r\n```powershell\r\nPowerShell Core v6.0.0-beta.5 on macOS 10.12.6\r\nPowerShell Core v6.0.0-beta.5 on Ubuntu 16.04.3 LTS\r\nPowerShell Core v6.0.0-beta.5 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)\r\nWindows PowerShell v5.1.15063.483 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2017-08-20T16:40:15Z","url":"https://github.com/PowerShell/PowerShell/issues/4624"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f8a2ec22-58f4-14da-e1b3-8acfa12763db |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8012:11BFC:285E003:384D612:6AFD52D0 |
| html-safe-nonce | eb18d0e6f8b9f76050e18ca059b064dde039aa3650a8b0e5b511e85dc939b195 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MDEyOjExQkZDOjI4NUUwMDM6Mzg0RDYxMjo2QUZENTJEMCIsInZpc2l0b3JfaWQiOiIxMzM3NDg5MzA3ODExNTMzNTIwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 766a16f5ff167ba7d9ead3ee04bbde78a6689c14e9bfb7c21cce01340f2b8b85 |
| hovercard-subject-tag | issue:251499818 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/4624/issue_layout |
| twitter:image | https://opengraph.githubassets.com/dbce60dc286c01d928380eddcfe79613878163a812ff7c50752fe629c0e320cc/PowerShell/PowerShell/issues/4624 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/dbce60dc286c01d928380eddcfe79613878163a812ff7c50752fe629c0e320cc/PowerShell/PowerShell/issues/4624 |
| og:image:alt | Follow-up to #4576 and #4591 Context: Note: By bareword I mean a run of non-whitespace characters not single- or double-quoted as a whole (e.g., foo), used in argument mode, and normally interprete... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 66653c2e55a6fff2930be1bdf07ee4f8aa137e3956d056c822563be6f878755e |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | b6f01f7cb9f35f8c09949773241a3d49dd04b340 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width