2018-05-16 20:04:48 +02:00
#!/usr/bin/env bash
2018-11-13 12:30:06 +01:00
2018-05-16 20:04:48 +02:00
set -e
2018-11-13 12:30:06 +01:00
main( ) {
declare argv
argv = $( getopt -o a: --long args: -- " $@ " ) || return
eval " set -- $argv "
declare args
declare files
for argv; do
case $argv in
2020-01-21 11:54:13 +01:00
-a | --args)
2018-11-13 12:30:06 +01:00
shift
args = " $1 "
shift
; ;
2020-01-21 11:54:13 +01:00
--)
2018-11-13 12:30:06 +01:00
shift
files = " $@ "
break
; ;
esac
done
2020-01-21 05:19:46 -05:00
local hack_terraform_docs
2020-02-21 13:47:04 +01:00
hack_terraform_docs = $( terraform version | head -1 | grep -c 0.12) || true
2019-06-17 12:47:06 +02:00
2020-01-21 05:19:46 -05:00
if [ [ ! $( command -v terraform-docs) ] ] ; then
echo "ERROR: terraform-docs is required by terraform_docs pre-commit hook but is not installed or in the system's PATH."
exit 1
fi
local is_old_terraform_docs
is_old_terraform_docs = $( terraform-docs version | grep -o "v0.[1-7]" | tail -1)
2020-01-21 11:54:13 +01:00
if [ [ -z " $is_old_terraform_docs " ] ] ; then # Using terraform-docs 0.8+ (preferred)
2020-01-21 05:19:46 -05:00
terraform_docs "0" " $args " " $files "
elif [ [ " $hack_terraform_docs " = = "1" ] ] ; then # Using awk script because terraform-docs is older than 0.8 and terraform 0.12 is used
if [ [ ! $( command -v awk) ] ] ; then
echo "ERROR: awk is required for terraform-docs hack to work with Terraform 0.12."
exit 1
fi
2019-06-17 12:47:06 +02:00
2019-06-18 21:17:57 +02:00
tmp_file_awk = $( mktemp " ${ TMPDIR :- /tmp } /terraform-docs-XXXXXXXXXX " )
2019-06-18 13:58:49 +02:00
terraform_docs_awk " $tmp_file_awk "
terraform_docs " $tmp_file_awk " " $args " " $files "
rm -f " $tmp_file_awk "
2020-01-21 05:19:46 -05:00
else # Using terraform 0.11 and no awk script is needed for that
2019-06-17 12:47:06 +02:00
terraform_docs "0" " $args " " $files "
2020-01-21 05:19:46 -05:00
fi
2018-11-13 12:30:06 +01:00
}
terraform_docs( ) {
2019-06-17 12:47:06 +02:00
readonly terraform_docs_awk_file = " $1 "
readonly args = " $2 "
readonly files = " $3 "
2018-11-13 12:30:06 +01:00
declare -a paths
declare -a tfvars_files
index = 0
for file_with_path in $files ; do
file_with_path = " ${ file_with_path // /__REPLACED__SPACE__ } "
2018-05-16 20:04:48 +02:00
2018-11-13 12:30:06 +01:00
paths[ index] = $( dirname " $file_with_path " )
2018-05-16 20:04:48 +02:00
2018-11-13 12:30:06 +01:00
if [ [ " $file_with_path " = = *".tfvars" ] ] ; then
tfvars_files += ( " $file_with_path " )
fi
2018-05-16 20:04:48 +02:00
2020-01-21 11:54:13 +01:00
( ( index += 1) )
2018-11-13 12:30:06 +01:00
done
2018-05-16 20:04:48 +02:00
2018-11-13 12:30:06 +01:00
readonly tmp_file = $( mktemp)
readonly text_file = "README.md"
2018-05-16 20:04:48 +02:00
2018-11-13 12:30:06 +01:00
for path_uniq in $( echo " ${ paths [*] } " | tr ' ' '\n' | sort -u) ; do
path_uniq = " ${ path_uniq //__REPLACED__SPACE__/ } "
2018-05-16 20:04:48 +02:00
2018-11-13 12:30:06 +01:00
pushd " $path_uniq " > /dev/null
2018-05-16 20:04:48 +02:00
2018-11-13 12:30:06 +01:00
if [ [ ! -f " $text_file " ] ] ; then
popd > /dev/null
continue
fi
2018-05-16 20:04:48 +02:00
2020-01-21 11:54:13 +01:00
if [ [ " $terraform_docs_awk_file " = = "0" ] ] ; then
2020-04-04 06:55:01 +01:00
# shellcheck disable=SC2086
terraform-docs md $args ./ > " $tmp_file "
2020-01-21 11:54:13 +01:00
else
# Can't append extension for mktemp, so renaming instead
tmp_file_docs = $( mktemp " ${ TMPDIR :- /tmp } /terraform-docs-XXXXXXXXXX " )
mv " $tmp_file_docs " " $tmp_file_docs .tf "
tmp_file_docs_tf = " $tmp_file_docs .tf "
awk -f " $terraform_docs_awk_file " ./*.tf > " $tmp_file_docs_tf "
2020-04-04 06:55:01 +01:00
# shellcheck disable=SC2086
terraform-docs md $args " $tmp_file_docs_tf " > " $tmp_file "
2020-01-21 11:54:13 +01:00
rm -f " $tmp_file_docs_tf "
fi
2018-11-13 12:30:06 +01:00
# Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834
perl -i -ne 'if (/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/../END OF PRE-COMMIT-TERRAFORM DOCS HOOK/) { print $_ if /BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/; print "I_WANT_TO_BE_REPLACED\n$_" if /END OF PRE-COMMIT-TERRAFORM DOCS HOOK/;} else { print $_ }' " $text_file "
# Replace placeholder with the content of the file
perl -i -e 'open(F, "' " $tmp_file " '"); $f = join "", <F>; while(<>){if (/I_WANT_TO_BE_REPLACED/) {print $f} else {print $_};}' " $text_file "
rm -f " $tmp_file "
2018-05-16 20:04:48 +02:00
2018-05-16 21:58:40 +02:00
popd > /dev/null
2018-11-13 12:30:06 +01:00
done
}
2019-06-17 12:47:06 +02:00
terraform_docs_awk( ) {
readonly output_file = $1
2020-01-21 11:54:13 +01:00
cat << "EOF" > " $output_file "
2019-06-17 12:47:06 +02:00
# This script converts Terraform 0.12 variables/outputs to something suitable for `terraform-docs`
# As of terraform-docs v0.6.0, HCL2 is not supported. This script is a *dirty hack* to get around it.
# https://github.com/segmentio/terraform-docs/
# https://github.com/segmentio/terraform-docs/issues/62
2019-06-18 13:58:49 +02:00
# Script was originally found here: https://github.com/cloudposse/build-harness/blob/master/bin/terraform-docs.awk
2019-06-17 12:47:06 +02:00
{
2019-06-18 13:58:49 +02:00
if ( $0 ~ /\{ / ) {
2019-06-17 12:47:06 +02:00
braceCnt++
}
2019-06-18 13:58:49 +02:00
if ( $0 ~ /\} / ) {
2019-06-17 12:47:06 +02:00
braceCnt--
}
2019-11-02 12:15:33 +01:00
# ----------------------------------------------------------------------------------------------
# variable|output "..." {
# ----------------------------------------------------------------------------------------------
# [END] variable/output block
if ( blockCnt > 0 && blockTypeCnt = = 0 && blockDefaultCnt = = 0) {
if ( braceCnt = = 0 && blockCnt > 0) {
blockCnt--
print $0
}
}
2019-06-17 12:47:06 +02:00
# [START] variable or output block started
2019-06-18 13:58:49 +02:00
if ( $0 ~ /^[ [ :space:] ] *( variable| output) [ [ :space:] ] [ [ :space:] ] *"(.*?)" /) {
2019-11-02 12:15:33 +01:00
# Normalize the braceCnt and block (should be 1 now)
2019-06-18 13:58:49 +02:00
braceCnt = 1
2019-11-02 12:15:33 +01:00
blockCnt = 1
# [CLOSE] "default" and "type" block
blockDefaultCnt = 0
blockTypeCnt = 0
# Print variable|output line
2019-06-17 12:47:06 +02:00
print $0
}
2019-11-02 12:15:33 +01:00
# ----------------------------------------------------------------------------------------------
# default = ...
# ----------------------------------------------------------------------------------------------
# [END] multiline "default" continues/ends
if ( blockCnt > 0 && blockTypeCnt = = 0 && blockDefaultCnt > 0) {
print $0
# Count opening blocks
blockDefaultCnt += gsub( /\( /, "" )
blockDefaultCnt += gsub( /\[ /, "" )
blockDefaultCnt += gsub( /\{ /, "" )
# Count closing blocks
blockDefaultCnt -= gsub( /\) /, "" )
blockDefaultCnt -= gsub( /\] /, "" )
blockDefaultCnt -= gsub( /\} /, "" )
}
# [START] multiline "default" statement started
if ( blockCnt > 0 && blockTypeCnt = = 0 && blockDefaultCnt = = 0) {
2019-06-18 13:58:49 +02:00
if ( $0 ~ /^[ [ :space:] ] [ [ :space:] ] *( default) [ [ :space:] ] [ [ :space:] ] *= /) {
if ( $3 ~ "null" ) {
print " default = \"null\""
} else {
print $0
2019-11-02 12:15:33 +01:00
# Count opening blocks
blockDefaultCnt += gsub( /\( /, "" )
blockDefaultCnt += gsub( /\[ /, "" )
blockDefaultCnt += gsub( /\{ /, "" )
# Count closing blocks
blockDefaultCnt -= gsub( /\) /, "" )
blockDefaultCnt -= gsub( /\] /, "" )
blockDefaultCnt -= gsub( /\} /, "" )
2019-06-17 12:47:06 +02:00
}
}
}
2019-11-02 12:15:33 +01:00
# ----------------------------------------------------------------------------------------------
# type = ...
# ----------------------------------------------------------------------------------------------
# [END] multiline "type" continues/ends
if ( blockCnt > 0 && blockTypeCnt > 0 && blockDefaultCnt = = 0) {
# The following 'print $0' would print multiline type definitions
#print $0
# Count opening blocks
blockTypeCnt += gsub( /\( /, "" )
blockTypeCnt += gsub( /\[ /, "" )
blockTypeCnt += gsub( /\{ /, "" )
# Count closing blocks
blockTypeCnt -= gsub( /\) /, "" )
blockTypeCnt -= gsub( /\] /, "" )
blockTypeCnt -= gsub( /\} /, "" )
}
# [START] multiline "type" statement started
if ( blockCnt > 0 && blockTypeCnt = = 0 && blockDefaultCnt = = 0) {
if ( $0 ~ /^[ [ :space:] ] [ [ :space:] ] *( type ) [ [ :space:] ] [ [ :space:] ] *= / ) {
if ( $3 ~ "object" ) {
2019-06-17 12:47:06 +02:00
print " type = \"object\""
} else {
2019-11-02 12:15:33 +01:00
# Convert multiline stuff into single line
if ( $3 ~ /^[ [ :space:] ] *list[ [ :space:] ] *\( [ [ :space:] ] *$/) {
type = "list"
} else if ( $3 ~ /^[ [ :space:] ] *string[ [ :space:] ] *\( [ [ :space:] ] *$/) {
type = "string"
} else if ( $3 ~ /^[ [ :space:] ] *map[ [ :space:] ] *\( [ [ :space:] ] *$/) {
type = "map"
} else {
type = $3
}
2019-06-25 14:34:46 +02:00
# legacy quoted types: "string", "list", and "map"
2019-11-02 12:15:33 +01:00
if ( type ~ /^[ [ :space:] ] *"(.*?)" [ [ :space:] ] *$/) {
print " type = " type
2019-06-25 14:34:46 +02:00
} else {
2019-11-02 12:15:33 +01:00
print " type = \"" type "\""
2019-06-25 14:34:46 +02:00
}
2019-06-17 12:47:06 +02:00
}
2019-11-02 12:15:33 +01:00
# Count opening blocks
blockTypeCnt += gsub( /\( /, "" )
blockTypeCnt += gsub( /\[ /, "" )
blockTypeCnt += gsub( /\{ /, "" )
# Count closing blocks
blockTypeCnt -= gsub( /\) /, "" )
blockTypeCnt -= gsub( /\] /, "" )
blockTypeCnt -= gsub( /\} /, "" )
2019-06-17 12:47:06 +02:00
}
}
2019-11-02 12:15:33 +01:00
# ----------------------------------------------------------------------------------------------
# description = ...
# ----------------------------------------------------------------------------------------------
# [PRINT] single line "description"
if ( blockCnt > 0 && blockTypeCnt = = 0 && blockDefaultCnt = = 0) {
if ( $0 ~ /^[ [ :space:] ] [ [ :space:] ] *description[ [ :space:] ] [ [ :space:] ] *= /) {
2019-06-17 12:47:06 +02:00
print $0
}
}
2019-11-02 12:15:33 +01:00
# ----------------------------------------------------------------------------------------------
# value = ...
# ----------------------------------------------------------------------------------------------
## [PRINT] single line "value"
#if (blockCnt > 0 && blockTypeCnt == 0 && blockDefaultCnt == 0) {
# if ($0 ~ /^[[:space:]][[:space:]]*value[[:space:]][[:space:]]*=/) {
# print $0
# }
#}
# ----------------------------------------------------------------------------------------------
# Newlines, comments, everything else
# ----------------------------------------------------------------------------------------------
#if (blockTypeCnt == 0 && blockDefaultCnt == 0) {
# Comments with '#'
if ( $0 ~ /^[ [ :space:] ] *#/) {
print $0
}
# Comments with '//'
if ( $0 ~ /^[ [ :space:] ] *\/ \/ /) {
print $0
}
# Newlines
if ( $0 ~ /^[ [ :space:] ] *$/) {
print $0
2019-06-17 12:47:06 +02:00
}
2019-11-02 12:15:33 +01:00
#}
2019-06-17 12:47:06 +02:00
}
EOF
}
2018-11-13 12:30:06 +01:00
getopt( ) {
# pure-getopt, a drop-in replacement for GNU getopt in pure Bash.
# version 1.4.3
#
# Copyright 2012-2018 Aron Griffis <aron@scampersand.com>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
_getopt_main( ) {
# Returns one of the following statuses:
# 0 success
# 1 error parsing parameters
# 2 error in getopt invocation
# 3 internal error
# 4 reserved for -T
#
# For statuses 0 and 1, generates normalized and shell-quoted
# "options -- parameters" on stdout.
declare parsed status
declare short long name flags
declare have_short = false
# Synopsis from getopt man-page:
#
# getopt optstring parameters
# getopt [options] [--] optstring parameters
# getopt [options] -o|--options optstring [options] [--] parameters
#
# The first form can be normalized to the third form which
# _getopt_parse() understands. The second form can be recognized after
# first parse when $short hasn't been set.
if [ [ -n ${ GETOPT_COMPATIBLE +isset } || $1 = = [ ^-] * ] ] ; then
# Enable compatibility mode
flags = c$flags
# Normalize first to third synopsis form
set -- -o " $1 " -- " ${ @ : 2 } "
fi
# First parse always uses flags=p since getopt always parses its own
# arguments effectively in this mode.
parsed = $( _getopt_parse getopt ahl:n:o:qQs:TuV \
alternative,help,longoptions:,name:,options:,quiet,quiet-output,shell:,test,version \
p " $@ " )
status = $?
if [ [ $status != 0 ] ] ; then
if [ [ $status = = 1 ] ] ; then
echo "Try \`getopt --help' for more information." >& 2
# Since this is the first parse, convert status 1 to 2
status = 2
fi
return $status
fi
eval " set -- $parsed "
while [ [ $# -gt 0 ] ] ; do
case $1 in
2020-01-21 11:54:13 +01:00
-a | --alternative)
flags = a$flags
; ;
2018-11-13 12:30:06 +01:00
2020-01-21 11:54:13 +01:00
-h | --help)
2018-11-13 12:30:06 +01:00
_getopt_help
2020-01-21 11:54:13 +01:00
return 2 # as does GNU getopt
2018-11-13 12:30:06 +01:00
; ;
2020-01-21 11:54:13 +01:00
-l | --longoptions)
2018-11-13 12:30:06 +01:00
long = " $long ${ long : +, } $2 "
2020-01-21 11:54:13 +01:00
shift
; ;
2018-11-13 12:30:06 +01:00
2020-01-21 11:54:13 +01:00
-n | --name)
2018-11-13 12:30:06 +01:00
name = $2
2020-01-21 11:54:13 +01:00
shift
; ;
2018-11-13 12:30:06 +01:00
2020-01-21 11:54:13 +01:00
-o | --options)
2018-11-13 12:30:06 +01:00
short = $2
have_short = true
2020-01-21 11:54:13 +01:00
shift
; ;
2018-11-13 12:30:06 +01:00
2020-01-21 11:54:13 +01:00
-q | --quiet)
flags = q$flags
; ;
2018-11-13 12:30:06 +01:00
2020-01-21 11:54:13 +01:00
-Q | --quiet-output)
flags = Q$flags
; ;
2018-11-13 12:30:06 +01:00
2020-01-21 11:54:13 +01:00
-s | --shell)
2018-11-13 12:30:06 +01:00
case $2 in
2020-01-21 11:54:13 +01:00
sh | bash)
flags = ${ flags //t/ }
; ;
csh | tcsh)
flags = t$flags
; ;
*)
2018-11-13 12:30:06 +01:00
echo 'getopt: unknown shell after -s or --shell argument' >& 2
echo "Try \`getopt --help' for more information." >& 2
2020-01-21 11:54:13 +01:00
return 2
; ;
2018-11-13 12:30:06 +01:00
esac
2020-01-21 11:54:13 +01:00
shift
; ;
2018-11-13 12:30:06 +01:00
2020-01-21 11:54:13 +01:00
-u | --unquoted)
flags = u$flags
; ;
2018-11-13 12:30:06 +01:00
2020-01-21 11:54:13 +01:00
-T | --test)
return 4
; ;
2018-11-13 12:30:06 +01:00
2020-01-21 11:54:13 +01:00
-V | --version)
2018-11-13 12:30:06 +01:00
echo "pure-getopt 1.4.3"
2020-01-21 11:54:13 +01:00
return 0
; ;
2018-11-13 12:30:06 +01:00
2020-01-21 11:54:13 +01:00
--)
2018-11-13 12:30:06 +01:00
shift
2020-01-21 11:54:13 +01:00
break
; ;
2018-11-13 12:30:06 +01:00
esac
shift
done
if ! $have_short ; then
# $short was declared but never set, not even to an empty string.
# This implies the second form in the synopsis.
if [ [ $# = = 0 ] ] ; then
echo 'getopt: missing optstring argument' >& 2
echo "Try \`getopt --help' for more information." >& 2
return 2
fi
short = $1
have_short = true
shift
fi
if [ [ $short = = -* ] ] ; then
# Leading dash means generate output in place rather than reordering,
# unless we're already in compatibility mode.
[ [ $flags = = *c* ] ] || flags = i$flags
short = ${ short #? }
elif [ [ $short = = +* ] ] ; then
# Leading plus means POSIXLY_CORRECT, unless we're already in
# compatibility mode.
[ [ $flags = = *c* ] ] || flags = p$flags
short = ${ short #? }
fi
# This should fire if POSIXLY_CORRECT is in the environment, even if
# it's an empty string. That's the difference between :+ and +
flags = ${ POSIXLY_CORRECT +p } $flags
_getopt_parse " ${ name :- getopt } " " $short " " $long " " $flags " " $@ "
}
_getopt_parse( ) {
# Inner getopt parser, used for both first parse and second parse.
# Returns 0 for success, 1 for error parsing, 3 for internal error.
# In the case of status 1, still generates stdout with whatever could
# be parsed.
#
# $flags is a string of characters with the following meanings:
# a - alternative parsing mode
# c - GETOPT_COMPATIBLE
# i - generate output in place rather than reordering
# p - POSIXLY_CORRECT
# q - disable error reporting
# Q - disable normal output
# t - quote for csh/tcsh
# u - unquoted output
declare name = " $1 " short = " $2 " long = " $3 " flags = " $4 "
shift 4
# Split $long on commas, prepend double-dashes, strip colons;
# for use with _getopt_resolve_abbrev
declare -a longarr
_getopt_split longarr " $long "
2020-01-21 11:54:13 +01:00
longarr = ( " ${ longarr [@]/#/-- } " )
longarr = ( " ${ longarr [@]% : } " )
longarr = ( " ${ longarr [@]% : } " )
2018-11-13 12:30:06 +01:00
# Parse and collect options and parameters
declare -a opts params
declare o alt_recycled = false error = 0
while [ [ $# -gt 0 ] ] ; do
case $1 in
2020-01-21 11:54:13 +01:00
--)
params = ( " ${ params [@] } " " ${ @ : 2 } " )
break
; ;
2018-11-13 12:30:06 +01:00
2020-01-21 11:54:13 +01:00
--*= *)
2018-11-13 12:30:06 +01:00
o = ${ 1 %%=* }
if ! o = $( _getopt_resolve_abbrev " $o " " ${ longarr [@] } " ) ; then
error = 1
elif [ [ ," $long " , = = *," ${ o #-- } " ::,* ] ] ; then
2020-01-21 11:54:13 +01:00
opts = ( " ${ opts [@] } " " $o " " ${ 1 #*= } " )
2018-11-13 12:30:06 +01:00
elif [ [ ," $long " , = = *," ${ o #-- } " :,* ] ] ; then
2020-01-21 11:54:13 +01:00
opts = ( " ${ opts [@] } " " $o " " ${ 1 #*= } " )
2018-11-13 12:30:06 +01:00
elif [ [ ," $long " , = = *," ${ o #-- } " ,* ] ] ; then
if $alt_recycled ; then o = ${ o #- } ; fi
_getopt_err " $name : option ' $o ' doesn't allow an argument "
error = 1
else
echo "getopt: assertion failed (1)" >& 2
return 3
fi
alt_recycled = false
; ;
2020-01-21 11:54:13 +01:00
--?*)
2018-11-13 12:30:06 +01:00
o = $1
if ! o = $( _getopt_resolve_abbrev " $o " " ${ longarr [@] } " ) ; then
error = 1
elif [ [ ," $long " , = = *," ${ o #-- } " ,* ] ] ; then
2020-01-21 11:54:13 +01:00
opts = ( " ${ opts [@] } " " $o " )
2018-11-13 12:30:06 +01:00
elif [ [ ," $long " , = = *," ${ o #-- } :: " ,* ] ] ; then
2020-01-21 11:54:13 +01:00
opts = ( " ${ opts [@] } " " $o " '' )
2018-11-13 12:30:06 +01:00
elif [ [ ," $long " , = = *," ${ o #-- } : " ,* ] ] ; then
if [ [ $# -ge 2 ] ] ; then
shift
2020-01-21 11:54:13 +01:00
opts = ( " ${ opts [@] } " " $o " " $1 " )
2018-11-13 12:30:06 +01:00
else
if $alt_recycled ; then o = ${ o #- } ; fi
_getopt_err " $name : option ' $o ' requires an argument "
error = 1
fi
else
echo "getopt: assertion failed (2)" >& 2
return 3
fi
alt_recycled = false
; ;
2020-01-21 11:54:13 +01:00
-*)
2018-11-13 12:30:06 +01:00
if [ [ $flags = = *a* ] ] ; then
# Alternative parsing mode!
# Try to handle as a long option if any of the following apply:
# 1. There's an equals sign in the mix -x=3 or -xy=3
# 2. There's 2+ letters and an abbreviated long match -xy
# 3. There's a single letter and an exact long match
# 4. There's a single letter and no short match
o = ${ 1 : : 2 } # temp for testing #4
if [ [ $1 = = *= * || $1 = = -?? || \
2020-01-21 11:54:13 +01:00
,$long , = = *," ${ 1 #- } " [ :,] * || \
,$short , != *," ${ o #- } " [ :,] * ] ] ; then
o = $( _getopt_resolve_abbrev " ${ 1 %%=* } " " ${ longarr [@] } " 2> /dev/null)
2018-11-13 12:30:06 +01:00
case $? in
2020-01-21 11:54:13 +01:00
0)
2018-11-13 12:30:06 +01:00
# Unambiguous match. Let the long options parser handle
# it, with a flag to get the right error message.
set -- " - $1 " " ${ @ : 2 } "
alt_recycled = true
2020-01-21 11:54:13 +01:00
continue
; ;
1)
2018-11-13 12:30:06 +01:00
# Ambiguous match, generate error and continue.
2020-01-21 11:54:13 +01:00
_getopt_resolve_abbrev " ${ 1 %%=* } " " ${ longarr [@] } " > /dev/null
2018-11-13 12:30:06 +01:00
error = 1
shift
2020-01-21 11:54:13 +01:00
continue
; ;
2)
2018-11-13 12:30:06 +01:00
# No match, fall through to single-character check.
2020-01-21 11:54:13 +01:00
true
; ;
*)
2018-11-13 12:30:06 +01:00
echo "getopt: assertion failed (3)" >& 2
2020-01-21 11:54:13 +01:00
return 3
; ;
2018-11-13 12:30:06 +01:00
esac
fi
fi
o = ${ 1 : : 2 }
if [ [ " $short " = = *" ${ o #- } " ::* ] ] ; then
if [ [ ${# 1 } -gt 2 ] ] ; then
2020-01-21 11:54:13 +01:00
opts = ( " ${ opts [@] } " " $o " " ${ 1 : 2 } " )
2018-11-13 12:30:06 +01:00
else
2020-01-21 11:54:13 +01:00
opts = ( " ${ opts [@] } " " $o " '' )
2018-11-13 12:30:06 +01:00
fi
elif [ [ " $short " = = *" ${ o #- } " :* ] ] ; then
if [ [ ${# 1 } -gt 2 ] ] ; then
2020-01-21 11:54:13 +01:00
opts = ( " ${ opts [@] } " " $o " " ${ 1 : 2 } " )
2018-11-13 12:30:06 +01:00
elif [ [ $# -ge 2 ] ] ; then
shift
2020-01-21 11:54:13 +01:00
opts = ( " ${ opts [@] } " " $o " " $1 " )
2018-11-13 12:30:06 +01:00
else
_getopt_err " $name : option requires an argument -- ' ${ o #- } ' "
error = 1
fi
elif [ [ " $short " = = *" ${ o #- } " * ] ] ; then
2020-01-21 11:54:13 +01:00
opts = ( " ${ opts [@] } " " $o " )
2018-11-13 12:30:06 +01:00
if [ [ ${# 1 } -gt 2 ] ] ; then
set -- " $o " " - ${ 1 : 2 } " " ${ @ : 2 } "
fi
else
if [ [ $flags = = *a* ] ] ; then
# Alternative parsing mode! Report on the entire failed
# option. GNU includes =value but we omit it for sanity with
# very long values.
_getopt_err " $name : unrecognized option ' ${ 1 %%=* } ' "
else
_getopt_err " $name : invalid option -- ' ${ o #- } ' "
if [ [ ${# 1 } -gt 2 ] ] ; then
set -- " $o " " - ${ 1 : 2 } " " ${ @ : 2 } "
fi
fi
error = 1
2020-01-21 11:54:13 +01:00
fi
; ;
2018-11-13 12:30:06 +01:00
2020-01-21 11:54:13 +01:00
*)
2018-11-13 12:30:06 +01:00
# GNU getopt in-place mode (leading dash on short options)
# overrides POSIXLY_CORRECT
if [ [ $flags = = *i* ] ] ; then
2020-01-21 11:54:13 +01:00
opts = ( " ${ opts [@] } " " $1 " )
2018-11-13 12:30:06 +01:00
elif [ [ $flags = = *p* ] ] ; then
2020-01-21 11:54:13 +01:00
params = ( " ${ params [@] } " " $@ " )
2018-11-13 12:30:06 +01:00
break
else
2020-01-21 11:54:13 +01:00
params = ( " ${ params [@] } " " $1 " )
2018-11-13 12:30:06 +01:00
fi
2020-01-21 11:54:13 +01:00
; ;
2018-11-13 12:30:06 +01:00
esac
shift
done
if [ [ $flags = = *Q* ] ] ; then
2020-01-21 11:54:13 +01:00
true # generate no output
2018-11-13 12:30:06 +01:00
else
echo -n ' '
if [ [ $flags = = *[ cu] * ] ] ; then
printf '%s -- %s' " ${ opts [*] } " " ${ params [*] } "
else
if [ [ $flags = = *t* ] ] ; then
_getopt_quote_csh " ${ opts [@] } " -- " ${ params [@] } "
else
_getopt_quote " ${ opts [@] } " -- " ${ params [@] } "
fi
fi
echo
fi
return $error
}
_getopt_err( ) {
if [ [ $flags != *q* ] ] ; then
printf '%s\n' " $1 " >& 2
fi
}
_getopt_resolve_abbrev( ) {
# Resolves an abbrevation from a list of possibilities.
# If the abbreviation is unambiguous, echoes the expansion on stdout
# and returns 0. If the abbreviation is ambiguous, prints a message on
# stderr and returns 1. (For first parse this should convert to exit
# status 2.) If there is no match at all, prints a message on stderr
# and returns 2.
declare a q = " $1 "
declare -a matches
shift
for a; do
if [ [ $q = = " $a " ] ] ; then
# Exact match. Squash any other partial matches.
2020-01-21 11:54:13 +01:00
matches = ( " $a " )
2018-11-13 12:30:06 +01:00
break
elif [ [ $flags = = *a* && $q = = -[ ^-] * && $a = = -" $q " ] ] ; then
# Exact alternative match. Squash any other partial matches.
2020-01-21 11:54:13 +01:00
matches = ( " $a " )
2018-11-13 12:30:06 +01:00
break
elif [ [ $a = = " $q " * ] ] ; then
# Abbreviated match.
2020-01-21 11:54:13 +01:00
matches = ( " ${ matches [@] } " " $a " )
2018-11-13 12:30:06 +01:00
elif [ [ $flags = = *a* && $q = = -[ ^-] * && $a = = -" $q " * ] ] ; then
# Abbreviated alternative match.
2020-01-21 11:54:13 +01:00
matches = ( " ${ matches [@] } " " ${ a #- } " )
2018-11-13 12:30:06 +01:00
fi
done
case ${# matches [@] } in
2020-01-21 11:54:13 +01:00
0)
[ [ $flags = = *q* ] ] ||
printf " $name : unrecognized option %s\\n " \
" $( _getopt_quote " $q " ) " >& 2
return 2
; ;
1)
printf '%s' " ${ matches [0] } "
return 0
; ;
*)
[ [ $flags = = *q* ] ] ||
printf " $name : option %s is ambiguous; possibilities: %s\\n " \
" $( _getopt_quote " $q " ) " " $( _getopt_quote " ${ matches [@] } " ) " >& 2
return 1
; ;
2018-11-13 12:30:06 +01:00
esac
}
_getopt_split( ) {
# Splits $2 at commas to build array specified by $1
declare IFS = ,
eval " $1 =( \$2 ) "
}
_getopt_quote( ) {
# Quotes arguments with single quotes, escaping inner single quotes
declare s space q = \'
for s; do
printf " $space '%s' " " ${ s // $q / $q \\ $q $q } "
space = ' '
done
}
_getopt_quote_csh( ) {
# Quotes arguments with single quotes, escaping inner single quotes,
# bangs, backslashes and newlines
declare s i c space
for s; do
echo -n " $space ' "
2020-01-21 11:54:13 +01:00
for ( ( i = 0; i < ${# s } ; i++) ) ; do
2018-11-13 12:30:06 +01:00
c = ${ s : i : 1 }
case $c in
2020-01-21 11:54:13 +01:00
\\ | \' | !)
echo -n " '\\ $c ' "
; ;
$'\n' )
echo -n " \\ $c "
; ;
*)
echo -n " $c "
; ;
2018-11-13 12:30:06 +01:00
esac
done
echo -n \'
space = ' '
done
}
_getopt_help( ) {
2020-01-21 11:54:13 +01:00
cat <<- EOT >& 2
2018-11-13 12:30:06 +01:00
Usage:
getopt <optstring> <parameters>
getopt [ options] [ --] <optstring> <parameters>
getopt [ options] -o| --options <optstring> [ options] [ --] <parameters>
Parse command options.
Options:
-a, --alternative allow long options starting with single -
-l, --longoptions <longopts> the long options to be recognized
-n, --name <progname> the name under which errors are reported
-o, --options <optstring> the short options to be recognized
-q, --quiet disable error reporting by getopt( 3)
-Q, --quiet-output no normal output
-s, --shell <shell> set quoting conventions to those of <shell>
-T, --test test for getopt( 1) version
-u, --unquoted do not quote the output
-h, --help display this help and exit
-V, --version output version information and exit
For more details see getopt( 1) .
EOT
}
2018-05-16 21:57:49 +02:00
2018-11-13 12:30:06 +01:00
_getopt_version_check( ) {
if [ [ -z $BASH_VERSION ] ] ; then
echo "getopt: unknown version of bash might not be compatible" >& 2
return 1
fi
2018-05-16 20:04:48 +02:00
2018-11-13 12:30:06 +01:00
# This is a lexical comparison that should be sufficient forever.
if [ [ $BASH_VERSION < 2.05b ] ] ; then
echo " getopt: bash $BASH_VERSION might not be compatible " >& 2
return 1
fi
2018-05-16 20:04:48 +02:00
2018-11-13 12:30:06 +01:00
return 0
}
2018-05-16 20:04:48 +02:00
2018-11-13 12:30:06 +01:00
_getopt_version_check
_getopt_main " $@ "
declare status = $?
unset -f _getopt_main _getopt_err _getopt_parse _getopt_quote \
_getopt_quote_csh _getopt_resolve_abbrev _getopt_split _getopt_help \
_getopt_version_check
return $status
}
2018-05-16 20:04:48 +02:00
2018-11-13 12:30:06 +01:00
[ [ $BASH_SOURCE != " $0 " ] ] || main " $@ "