Created
February 2, 2023 23:32
-
-
Save quonic/14621798bc1a8385823a2f67679d68f1 to your computer and use it in GitHub Desktop.
Parse getopt from a bash script using PowerShell.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Parameters and aliases | |
SHORT=a,b,g:,d: | |
LONG=alpha,beta,gamma:,delta: | |
# Script Name | |
ScriptName=Example | |
# Parameter values | |
Alpha=0 | |
Beta=0 | |
Gamma="" | |
Delta="" | |
Parameters=$(getopt -a -n $ScriptName --options $SHORT --longoptions $LONG -- "$@") | |
PARSED_ARGUMENTS=$(getopt -a -n alphabet -o abc:d: --long alpha,bravo,charlie:,delta: -- "$@") | |
if ! $?; then | |
exit 1 | |
fi | |
eval set -- "$Parameters" | |
while :; do | |
case "$1" in | |
-a | --alpha) | |
echo "Processing 'alpha' option" | |
Alpha=1 | |
shift | |
;; | |
-b | --beta) | |
echo "Processing 'beta' option" | |
Beta=1 | |
shift | |
;; | |
-g | --gamma) | |
echo "Processing 'gamma' option. Input argument is '$2'" | |
Gamma="$2" | |
shift 2 | |
;; | |
-d | --delta) | |
echo "Processing 'delta' option. Input argument is '$2'" | |
Delta="$2" | |
shift 2 | |
;; | |
--) | |
shift | |
break | |
;; | |
esac | |
done | |
function Begin() { | |
if ((Alpha == 1)); then | |
echo "equal" | |
fi | |
if ((Beta == 1)); then | |
echo "equal" | |
fi | |
} | |
function Process() { | |
if [[ -n "${Gamma}" ]]; then | |
echo "string is not empty" | |
fi | |
if [[ -n "${Delta}" ]]; then | |
echo "string is not empty" | |
fi | |
} | |
function End() { | |
Alpha=0 | |
} | |
Begin | |
Process | |
End |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-BashArguments { | |
[CmdletBinding()] | |
[OutputType([PSObject[]])] | |
param( | |
[Parameter(Mandatory = $true)] | |
# Path to a bash script | |
[string[]] | |
$Path | |
) | |
process { | |
$Path | ForEach-Object { | |
$Script = $_ | |
$Expression = 'getopt -a -n .* (?:-o|--options) (?''short''.+) (?:-l|--longoptions|--long) (?''long''([a-zA-Z0-9]|,|:|\$)+) \-\- \"\$@\"' | |
$RegexGetOpt = [Regex]::new($Expression) | |
Get-Content -Path $Script | ForEach-Object { | |
$MatchesGetopt = $RegexGetOpt.Match($_) | |
if ($MatchesGetopt.Success) { | |
$Options = $MatchesGetopt.Groups | Where-Object { $_.Name -like "short" -or $_.Name -like "long" } | Select-Object Name, Value | |
if ($Options.Value -like "`$*") { | |
$Options.Value | ForEach-Object { | |
$Name = $_ -replace '\$' | |
[PSCustomObject]@{ | |
Name = $Name | |
Value = (Get-Content -Path $Script | Select-String -Pattern "$Name=(.*)$") -replace "$Name=" | |
} | |
} | |
} | |
else { | |
$Options | |
} | |
} | |
} | |
} | |
} | |
} | |
Get-BashArguments -Path .\example.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment