Title: Request: partial names of operators work how partial names of parameters work - if the text is long enough to be unique, it works, otherwise it fails · Issue #4730 · PowerShell/PowerShell · GitHub
Open Graph Title: Request: partial names of operators work how partial names of parameters work - if the text is long enough to be unique, it works, otherwise it fails · Issue #4730 · PowerShell/PowerShell
X Title: Request: partial names of operators work how partial names of parameters work - if the text is long enough to be unique, it works, otherwise it fails · Issue #4730 · PowerShell/PowerShell
Description: When running a cmdlet, the parameter names don't have to be written out in full as long as the text is enough to uniquely identify one parameter, e.g. Group-Object -NoElement Group-Object -N # only -NoElement begins with N Export-Csv -no...
Open Graph Description: When running a cmdlet, the parameter names don't have to be written out in full as long as the text is enough to uniquely identify one parameter, e.g. Group-Object -NoElement Group-Object -N # only...
X Description: When running a cmdlet, the parameter names don't have to be written out in full as long as the text is enough to uniquely identify one parameter, e.g. Group-Object -NoElement Group-Object -N # ...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/4730
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Request: partial names of operators work how partial names of parameters work - if the text is long enough to be unique, it works, otherwise it fails","articleBody":"When running a cmdlet, the parameter names don't have to be written out in full as long as the text is enough to uniquely identify one parameter, e.g.\r\n\r\n```powershell\r\nGroup-Object -NoElement\r\nGroup-Object -N # only -NoElement begins with N\r\n\r\nExport-Csv -notype # binds to -NoTypeInformation\r\n\r\nGet-ChildItem -P # not enough text to identify one parameter, throws an exception\r\nGet-ChildItem : Parameter cannot be processed because the parameter name 'P' is \r\nambiguous. Possible matches include: -Path -PipelineVariable -LiteralPath.\r\n```\r\n\r\nI would like that behaviour for operators as well. e.g.\r\n\r\n```powershell\r\n$text -replace 'a', 'b'\r\n$text -r 'a', 'b' # -r would be -replace\r\n$text -m 'abc' # -m would be -match\r\n\r\n$text -j '.' # -j would be -join\r\n\r\n$text -s ',' # -s is not enough to uniquely identify anything\r\n# currently it throws an exception\r\nUnexpected token '-s' in expression or statement.\r\n# It could stay the same, or instead throw a similar message to the parameter matching:\r\nUnexpected token '-s' in expression or statement. (Possible operator matches include: -shl, -shr, -split)\r\n```\r\n\r\nThe intent would be like aliases - for interactive / shell use, not recommended in longer scripts.\r\n\r\nThere is already a precedent for a single character operator of this form with `-f`, although this change would not necessarily mean \"single characters\", just \"short enough to be unique\".\r\n\r\nCurrently, writing a partial operator name is an error, so initial thoughts are that this change would not clash with any existing feature, or break any existing code. It would mean new code written with variable length operator names could limit the scope for adding future operators without clashes. The big wins of this change are `-replace`, `-join`, `-split`, `-match` and there is little benefit in writing `-o` instead of `-or` and similar. Because of this, another possible version of this request would be to make explicit operator overloads `-r`, `-j`, `-sp`, `-m` which shorten just those operators, and no other variations are possible (`-repl` would fail). This version would reduce possible clashes with future operators. However in the full change, the case sensitive/insensitive variants also map nicely to `-ir, -cr, -isp, -csp, -im, -cm` and with fixed overloads they wouldn't. \r\n\r\nThe change to implement might be as simple as [changing the way operators are looked up in the tokenizer](https://github.com/PowerShell/PowerShell/blob/27e47f9ee1aa0592bffe17c8f979cfa6513887a4/src/System.Management.Automation/engine/parser/tokenizer.cs#L2921) `scanparameter()` method ('// Scan either a parameter or an operator') so that instead of using only `TryGetValue()` to look for a match, a failed lookup would test all the dictionary keys with a `.startswith` string test (I don't think wildcard matching would be relevant). Pseudocode:\r\n\r\n```csharp\r\nif (s_operatorTable.TryGetValue(str, out operatorKind)) \r\n{ \r\nreturn NewToken(operatorKind); \r\n} \r\n\r\n//becomes\r\n\r\nif (s_operatorTable.TryGetValue(str, out operatorKind)) \r\n{ \r\nreturn NewToken(operatorKind); \r\n} \r\nelse\r\n{\r\n KeyValuePair\u003cstring, tokenKind> partialOperator = s_operatorTable.SingleOrDefault( x => x.Key.StartsWith(str, [case insensitive startswith]));\r\n if (partialOperator is not null)\r\n {\r\n return NewToken(partialOperator.Value);\r\n }\r\n}\r\n```\r\n\r\nGenerating the helpful error message with possible matches would be harder, but keeping with the same `Unexpected token: -s` would be still valid. In this way the normal full operator name lookup would be the same speed, there would only be extra work scanning the dictionary names if that lookup failed.\r\n\r\nYou already cannot write `Get-Help -split`, so that would not change for `Get-Help -sp`, it could be added to about_Operators as a mention of partial matching, or added specifically to about_Comparison_Operators, about_Split and about_Join.\r\n\r\nLonger term, the most annoying part is `$testMessage = $testMessage -replace 'a', 'b'` having the variable name doubled and the long operator. I would like to see a shorter way of writing this pattern:\r\n\r\n```powershell\r\n$testMessage = $testMessage -replace 'a', 'b'\r\n\r\n# this change, syntactic sugar for partial operator name matching:\r\n$testMessage = $testMessage -r 'a', 'b' # -r operator maps to -replace\r\n\r\n\r\n# future change, pattern of $x += 1, $x *= 1 generalised to $x -operator= ___\r\n\r\n$testMessage -replace= 'a', 'b' #-replace= acts exactly like the pattern += \r\n # and saves writing the variable/expression twice\r\n\r\n$testMessage -r= 'a', 'b' # combination of both -r and -operator= makes -r= possible \r\n\r\n# interactively\r\n$x -r= ' ' is a much shorter \"remove spaces\" than $x = $x -replace ' '\r\n```\r\n","author":{"url":"https://github.com/HumanEquivalentUnit","@type":"Person","name":"HumanEquivalentUnit"},"datePublished":"2017-08-31T19:44:07Z","url":"https://github.com/PowerShell/PowerShell/issues/4730"}
| 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:10c80ae3-8c91-0369-7dd9-68714bb9007a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C9B8:35BEEA:42297A:5D8963:6AFDB7F1 |
| html-safe-nonce | 8813a954cc2afb83b38a4f8948cc5c44ab7f2e6da8c3b4ff3f05175824313e88 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOUI4OjM1QkVFQTo0MjI5N0E6NUQ4OTYzOjZBRkRCN0YxIiwidmlzaXRvcl9pZCI6Ijg4MTUzNTU3MTA3ODI2NDgxNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5780226cd4eeff8f9a1deb664eb3375e64662dd739bb1b61980a6d40d4beb44f |
| hovercard-subject-tag | issue:254446658 |
| 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/4730/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3841954b282fd2a8ecb8d10bf3a0130ba89cefb6cca0d5b8a4d4e420a3646d2a/PowerShell/PowerShell/issues/4730 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3841954b282fd2a8ecb8d10bf3a0130ba89cefb6cca0d5b8a4d4e420a3646d2a/PowerShell/PowerShell/issues/4730 |
| og:image:alt | When running a cmdlet, the parameter names don't have to be written out in full as long as the text is enough to uniquely identify one parameter, e.g. Group-Object -NoElement Group-Object -N # only... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | HumanEquivalentUnit |
| hostname | github.com |
| expected-hostname | github.com |
| None | 8309f07eb60a4237a657068c83d0bb57be4c7e02937f5e9933126f4bd596aac4 |
| 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 | 2f7154d847f1ddc5baa83b109a3e1d924605e2a3 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width