Search code examples
powershellchocolatey

Should I rewrite Chocolatey's warning messages in my wrapper script?


I've bundled a Chocolatey installer into PowerShell: My script calls a function for the installation process. The user is supposed to run .\install.ps1 in PowerShell. If the package already is installed the output is similar to:

<Packagename> already installed.
Use --force to reinstall, specify a version to install, or try upgrade.

Ok, so the user should think that .\install.ps1 --force will do the trick. Unfortunately, I have found no way for PowerShell to accept double dashes (--), so I'm thinking about rewriting the warning message from Chocolatey so it outputs -force instead of --force:

<Packagename> already installed.
Use -force to reinstall, specify a version to install, or try upgrade.

My setup.ps1 file is similar to:

Install-App <Packagename + parameters>

The function my script is calling is similar to:

function Install-App
{
    //..code ommited..
    $chocoCommand = "choco install <Packagename + parameters>"
    iex $chocoCommand
}

I've been thinking about try/catch, but haven't figured it out quite yet.

Any suggestions?

Best regards


Solution

  • I solved this by doing a foreach on $args:

    foreach($arg in $args)
    {
        if($arg -eq "--force" -Or $arg -eq "-force")
        {
            $forceParameter = "--force"
        }
    }
    

    And further passing this to my PowerShell command

    MyCustomCommand -forceParameter $forceParameter
    

    Thanks for the help!