Title: Parameters Should Support Mutual Exclusion Natively · Issue #5175 · PowerShell/PowerShell · GitHub
Open Graph Title: Parameters Should Support Mutual Exclusion Natively · Issue #5175 · PowerShell/PowerShell
X Title: Parameters Should Support Mutual Exclusion Natively · Issue #5175 · PowerShell/PowerShell
Description: Problem Parameter sets are great for grouping parameters into sets that work together. This can, to some degree, also be used to make parameters exclusive. The problem arises when you have mutually exclusive parameters that need to exist...
Open Graph Description: Problem Parameter sets are great for grouping parameters into sets that work together. This can, to some degree, also be used to make parameters exclusive. The problem arises when you have mutually...
X Description: Problem Parameter sets are great for grouping parameters into sets that work together. This can, to some degree, also be used to make parameters exclusive. The problem arises when you have mutually...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/5175
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Parameters Should Support Mutual Exclusion Natively","articleBody":"# Problem\r\n\r\nParameter sets are great for grouping parameters into sets that work together. This can, to some degree, also be used to make parameters exclusive. The problem arises when you have mutually exclusive parameters that need to exist in all parameter sets. The current implementation would require creating additional parameter sets to accommodate the actual parameter sets and the mutually exclusive parameters.\r\n\r\nFor very simplistic demonstration, lets say you have a `New-Widget`:\r\n\r\n```powershell\r\nfunction New-Widget {\r\n [CmdletBinding()]\r\n param (\r\n [Parameter(ParameterSetName=\"Size\")]\r\n [int]$Height,\r\n [Parameter(ParameterSetName=\"Size\")]\r\n [int]$Width,\r\n [Parameter(ParameterSetName=\"Size\")]\r\n [int]$Length,\r\n\r\n [Parameter(ParameterSetName=\"Color\")]\r\n [int]$Red,\r\n [Parameter(ParameterSetName=\"Color\")]\r\n [int]$Green,\r\n [Parameter(ParameterSetName=\"Color\")]\r\n [int]$Blue\r\n )\r\n}\r\n``` \r\n\r\nWidgets can be defined either by color or size. Now, let say that all widgets, regardless of begin defined by size or color, can either have a flavor or a sound but not both. Currently, to accommodate this you would need to do the following:\r\n\r\n```powershell\r\nfunction New-Widget {\r\n [CmdletBinding()]\r\n param (\r\n [Parameter(ParameterSetName=\"SizeFlavor\")]\r\n [Parameter(ParameterSetName=\"SizeSound\")]\r\n [int]$Height,\r\n [Parameter(ParameterSetName=\"SizeFlavor\")]\r\n [Parameter(ParameterSetName=\"SizeSound\")]\r\n [int]$Width,\r\n [Parameter(ParameterSetName=\"SizeSound\")]\r\n [Parameter(ParameterSetName=\"SizeFlavor\")]\r\n [int]$Length,\r\n\r\n [Parameter(ParameterSetName=\"ColorFlavor\")]\r\n [Parameter(ParameterSetName=\"ColorSound\")]\r\n [int]$Red,\r\n [Parameter(ParameterSetName=\"ColorSound\")]\r\n [Parameter(ParameterSetName=\"ColorFlavor\")]\r\n [int]$Green,\r\n [Parameter(ParameterSetName=\"ColorSound\")]\r\n [Parameter(ParameterSetName=\"ColorFlavor\")]\r\n [int]$Blue,\r\n\r\n [Parameter(ParameterSetName=\"SizeFlavor\")]\r\n [Parameter(ParameterSetName=\"ColorFlavor\")]\r\n [string]$Flavor,\r\n\r\n [Parameter(ParameterSetName=\"SizeSound\")]\r\n [Parameter(ParameterSetName=\"ColorSound\")]\r\n [string]$Sound\r\n )\r\n}\r\n```\r\n\r\nThis can get wildly out of hand if you have a third or fourth set of mutually exclusive parameters. This becomes difficult to maintain and to add additional parameters. Another option is to have logic in the body of the function to check for mutually exclusive Parameters at the cost of the mutual exclusion not being as discoverable.\r\n\r\nIn some instances you may be able to refactor into additional cmdlets or make use of custom types. But that doesn't always work for command line utilities meant to ease user burden at the cost of code complexity. \r\n\r\n# Proposal\r\nI propose that parameters should support mutual exclusion natively. I don't have a solid implementation plan, but one idea would be to add a Parameter Attribute property like `ParameterMutexName`. Using the example above it would work like this:\r\n\r\n```powershell\r\nfunction New-Widget {\r\n [CmdletBinding()]\r\n param (\r\n [Parameter(ParameterSetName=\"Size\")]\r\n [int]$Height,\r\n [Parameter(ParameterSetName=\"Size\")]\r\n [int]$Width,\r\n [Parameter(ParameterSetName=\"Size\")]\r\n [int]$Length,\r\n\r\n [Parameter(ParameterSetName=\"Color\")]\r\n [int]$Red,\r\n [Parameter(ParameterSetName=\"Color\")]\r\n [int]$Green,\r\n [Parameter(ParameterSetName=\"Color\")]\r\n [int]$Blue,\r\n\r\n [Parameter(ParameterSetName=\"Size\", ParameterMutexName=\"FlavorOrSound\")]\r\n [Parameter(ParameterSetName=\"Color\", ParameterMutexName=\"FlavorOrSound\")]\r\n [string]$Flavor,\r\n [Parameter(ParameterSetName=\"Size\", ParameterMutexName=\"FlavorOrSound\")]\r\n [Parameter(ParameterSetName=\"Color\", ParameterMutexName=\"FlavorOrSound\")]\r\n [string]$Sound\r\n )\r\n}\r\n```\r\n\r\nParameter binding logic would then check if more than one parameter is supplied in a `ParameterMutexName` and throw. This is flexible enough to accommodate parameters being exclusive in one set while not exclusive in other sets. This could also accommodate multiple Mutex sets within a parameter set.","author":{"url":"https://github.com/markekraus","@type":"Person","name":"markekraus"},"datePublished":"2017-10-19T22:49:17Z","url":"https://github.com/PowerShell/PowerShell/issues/5175"}
| 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:7de8fe29-8504-7a48-a811-85f5f1180e5e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CB6A:7AB3A:1CEEBB:2841A2:6AFD8096 |
| html-safe-nonce | 5336c523b7e3b301c45fa7a1eb4cd9b19055ac04ff0593c9cb548c5d8290278a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjZBOjdBQjNBOjFDRUVCQjoyODQxQTI6NkFGRDgwOTYiLCJ2aXNpdG9yX2lkIjoiMjg4NjMwNjUzNTM5MzAzNDM5MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4c2f5d265e4152234b355565d7c81f5a8b358cc88250e5557379976114561fe6 |
| hovercard-subject-tag | issue:267013199 |
| 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/5175/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bcf64be6fef1cb8f15f5184ef31c3efae488a28adcddb48d798e475b267d9577/PowerShell/PowerShell/issues/5175 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bcf64be6fef1cb8f15f5184ef31c3efae488a28adcddb48d798e475b267d9577/PowerShell/PowerShell/issues/5175 |
| og:image:alt | Problem Parameter sets are great for grouping parameters into sets that work together. This can, to some degree, also be used to make parameters exclusive. The problem arises when you have mutually... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | markekraus |
| hostname | github.com |
| expected-hostname | github.com |
| None | 42d4967e3c8afca51ccac31f419a705af777226a95f025ea73387d87a306b326 |
| 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 | 2329ceed0bc2c7b7c3b9f5f2db6abfc7b6fc3a50 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width