Skip to content

Instantly share code, notes, and snippets.

@beaujackson
Created December 8, 2016 19:26
Show Gist options
  • Save beaujackson/3ade7506837907c9b8a8e386cbe554a9 to your computer and use it in GitHub Desktop.
Save beaujackson/3ade7506837907c9b8a8e386cbe554a9 to your computer and use it in GitHub Desktop.
Using command line parameters within chocolateyInstall.ps1
<#
The parsePackageParameters function allows you to call choco install like this:
choco install MyChocolateyPackage --params '/myParam: ""some parameter with spaces in it""'
#>
function parsePackageParameters($packageParameters) {
$arguments = @{}
if ($packageParameters) {
# The input string is something like this, and we will use a Regular Expression to parse the values
# /Port:81 /Edition:LicenseKey /AdditionalTools
$match_pattern = "\/(?<option>([a-zA-Z]+)):(?<value>([`"'])?([a-zA-Z0-9- _\\:\.]+)([`"'])?)|\/(?<option>([a-zA-Z]+))"
$option_name = 'option'
$value_name = 'value'
if ($packageParameters -match $match_pattern) {
$results = $packageParameters | Select-String $match_pattern -AllMatches
$results.matches | % {
$arguments.Add(
$_.Groups[$option_name].Value.Trim(),
$_.Groups[$value_name].Value.Trim())
}
} else {
Write-Host "Parameters were invalid (REGEX Failure)"
}
} else {
Write-Host "No parameters given"
}
return $arguments
}
#$packageParameters is the variable you end up with if you call choco with the --params switch
$arguments = parsePackageParameters($packageParameters)
Write-Host $arguments["myParameter"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment