1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#!/usr/bin/env bash
SCRIPT_FILE="${1:-install-linux.sh}"
extract_usage_options() {
awk '
BEGIN { in_block=0 }
/^# ===+/ { in_block = !in_block; next }
in_block && /^# +--/ {
if (option && desc) {
gsub(/</, "\\<", desc)
gsub(/>/, "\\>", desc)
gsub(/^[[:space:]]+/, "", desc)
printf " `%s`\n\n %s\n\n", option, desc
}
option = $2 # Preserve the -- prefix
desc = ""
next
}
in_block && /^# +/ {
line = substr($0, 3)
if (option) {
if (desc) desc = desc " " line
else desc = line
}
}
END {
if (option && desc) {
gsub(/^[[:space:]]+/, "", desc)
printf "- `%s`: %s\n\n", option, desc
}
}
' "$SCRIPT_FILE"
}
extract_global_variables() {
grep -E '^[[:space:]]*[A-Z_]+=.*\$\{OPKSSH_[A-Z_]+:-[^}]+\}' "$SCRIPT_FILE" | while IFS= read -r line; do
# Get variable name before '='
varname=$(echo "$line" | sed -E 's/^\s*([A-Z_]+)=.*/\1/')
# Extract default value inside ${...:-default}
default=$(echo "$line" | sed -nE 's/.*\$\{OPKSSH_[A-Z_]+:-([^}]+)\}.*/\1/p')
# Extract referenced environment variable name
env_var=$(echo "$line" | sed -nE 's/.*\$\{(OPKSSH_[A-Z_]+):-[^}]+\}.*/\1/p')
echo "| **$varname** | \`$default\` | $env_var |"
done
}
format_doc_block() {
local doc="$1"
local in_args=0
local in_returns=0
local in_outputs=0
local in_example=0
while IFS= read -r line; do
case "$line" in
Arguments:*)
echo "**Arguments:**"
in_args=1; in_returns=0; in_outputs=0; in_example=0
;;
Returns:*)
echo; echo "**Returns:**"
in_args=0; in_returns=1; in_outputs=0; in_example=0
;;
Outputs:*)
echo; echo "**Outputs:**"
in_args=0; in_returns=0; in_outputs=1; in_example=0
;;
Example:*)
echo; echo "**Example:**"; echo '```bash'
in_args=0; in_returns=0; in_outputs=0; in_example=1
;;
shellcheck*)
;;
"")
if [[ $in_example -eq 1 ]]; then
echo '```';
fi
in_args=0; in_returns=0; in_outputs=0; in_example=0
echo
;;
*)
if [[ $in_args -eq 1 || $in_returns -eq 1 || $in_outputs -eq 1 ]]; then
echo "- ${line}"
elif [[ $in_example -eq 1 ]]; then
echo "$line"
else
echo "$line"
fi
;;
esac
done <<< "$doc"
}
# main
echo "## Command-Line Arguments"
echo
echo "Usage: \`$SCRIPT_FILE [OPTIONS]\`"
echo
echo "Options:"
echo
extract_usage_options
echo "## Environment Variables"
echo
echo "| Variable name | Default value | System env override |"
echo "|---------------|---------------|---------------------|"
extract_global_variables
echo
echo "# Script Function Documentation"
echo
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" =~ ^#[[:space:]]?(.*)$ ]]; then
doc_block+="${BASH_REMATCH[1]}"$'\n'
in_doc=1
elif [[ $in_doc -eq 1 && "$line" =~ ^([a-zA-Z_][a-zA-Z0-9_]*)\(\)[[:space:]]*\{ ]]; then
function_name="${BASH_REMATCH[1]}"
echo "## \`$function_name\`"
echo
format_doc_block "$doc_block"
echo
doc_block=""
in_doc=0
else
doc_block=""
in_doc=0
fi
done < "$SCRIPT_FILE"
|