I currently have the following powershell function:
function d { doppler run -- $args }
However, I would like to run something like d -Config dev ...
and have that translate to
doppler run --config dev -- ...
How can I accomplish this?
CodePudding user response:
If you want to add an optional parameter to your function you would no longer be able to use $args
the same way you're currently using it. By adding a new parameter to your non-advanced function the same would be always bound positionally (-Config
would be always bound hence wouldn't be optional).
What you could do instead to replace its functionality is turn your function into an advanced one and have a parameter that takes ValueFromRemainingArguments
.
I haven't tested it but I believe this should do the trick.
function d {
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter(Position = 0, ValueFromRemainingArguments)]
[string[]] $Arguments,
[Parameter()]
[string] $Config
)
end {
doppler @(
'run'
if($PSBoundParameters.ContainsKey('Config')) {
'--config', $Config
}
'--'
$Arguments
)
}
}
Then both options should be available, with and without -Config
:
PS ..\> d some arguments here
PS ..\> d -Config dev some arguments here
CodePudding user response:
The function needs to declare a parameter.
function d ($config)
{
doppler run --config $config
}
See documentation for more details -> https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7.3#functions-with-parameters