Open JPRuskin opened 11 months ago
Now with an example branch using splatting in the chocolateyScriptRunner. Turns out it was far easier than I worried, and seems to work back to PowerShell v3 - but that method would be harder to create a compatibility extension for.
Checklist
Is Your Feature Request Related To A Problem? Please describe.
As a PowerShell developer, it would be great if we could support the commonly-used param-block usage seen in functions and scripts.
When writing a PowerShell script, I expect this to work - but there is no nice way to pass arguments into Chocolatey to run scripts like this.
Instead, we have folk using something like this:
And there are many different ways people can write this handling logic.
Describe The Solution. Why is it needed?
Get-PackageParameters
exists, but everyone has to reimplement the handling for parameters in their package, which is less simple than it might be. If we had a first-class handling for param-blocks (or some similar modern method), we could advise folk to handle parameters in a specific way, which would allow us to do potentially interesting things.For instance, by enabling package maintainers to use param blocks:
It could also ease testing of install scripts, though I am not convinced that is particularly useful at this point for various reasons.
Additional Context
I had a play with adding a very basic transformation attribute to parameters within a
chocolateyInstall.ps1
script.This seems to work pretty nicely when added to the
chocolateyInstaller
helper functions, and handles using the default value in a script if a user doesn't pass a matching--package-parameter
(whilst overriding it if they do).As an example, by adding
[PackageParameter("NameOfPackageParameter")]
to a parameter, we can see the default, the help, and easily have a user provide input:It has a few potential disadvantages:
Add-Type
to bring it back to PowerShell 3+, or possibly use a compiled module to make it available everywhere?)Example of Add-Type equivalent:
An alternative to adding an attribute like this would be rewriting the
chocolateyScriptRunner.ps1
to pass in parameters where parameters are found, but that would involve a fair bit of calculation (or a requirement for an ignored parameter with ValueFromRemainingArguments to swallow unwanted splatting on all supporting scripts, perhaps?) and I've quite enjoyed this method so far.Related Issues
No response