| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # A string processing test case copied from bash_completion.
|
| 4 |
|
| 5 | shopt -s extglob # needed for Oil, but not bash
|
| 6 |
|
| 7 | # This function shell-quotes the argument
|
| 8 | quote()
|
| 9 | {
|
| 10 | local quoted=${1//\'/\'\\\'\'}
|
| 11 | printf "'%s'" "$quoted"
|
| 12 | }
|
| 13 |
|
| 14 | # This function shell-dequotes the argument
|
| 15 | dequote()
|
| 16 | {
|
| 17 | eval printf %s "$1" 2> /dev/null
|
| 18 | }
|
| 19 |
|
| 20 | # Helper function for _parse_help and _parse_usage.
|
| 21 | __parse_options()
|
| 22 | {
|
| 23 | local option option2 i IFS=$' \t\n,/|'
|
| 24 |
|
| 25 | # Take first found long option, or first one (short) if not found.
|
| 26 | option=
|
| 27 | local -a array=( $1 ) # relies on word splitting
|
| 28 | for i in "${array[@]}"; do
|
| 29 | case "$i" in
|
| 30 | ---*) break ;;
|
| 31 | --?*) option=$i ; break ;;
|
| 32 | -?*) [[ $option ]] || option=$i ;;
|
| 33 | *) break ;;
|
| 34 | esac
|
| 35 | done
|
| 36 | [[ $option ]] || return
|
| 37 |
|
| 38 | IFS=$' \t\n' # affects parsing of the regexps below...
|
| 39 |
|
| 40 | # Expand --[no]foo to --foo and --nofoo etc
|
| 41 | if [[ $option =~ (\[((no|dont)-?)\]). ]]; then
|
| 42 | option2=${option/"${BASH_REMATCH[1]}"/}
|
| 43 | option2=${option2%%[<{().[]*}
|
| 44 | printf '%s\n' "${option2/=*/=}"
|
| 45 | option=${option/"${BASH_REMATCH[1]}"/"${BASH_REMATCH[2]}"}
|
| 46 | fi
|
| 47 |
|
| 48 | option=${option%%[<{().[]*}
|
| 49 | printf '%s\n' "${option/=*/=}"
|
| 50 | }
|
| 51 |
|
| 52 | # Parse GNU style help output of the given command.
|
| 53 | # @param $1 command; if "-", read from stdin and ignore rest of args
|
| 54 | # @param $2 command options (default: --help)
|
| 55 | #
|
| 56 | _parse_help()
|
| 57 | {
|
| 58 | while read -r line; do
|
| 59 |
|
| 60 | [[ $line == *([[:blank:]])-* ]] || continue
|
| 61 | # transform "-f FOO, --foo=FOO" to "-f , --foo=FOO" etc
|
| 62 | while [[ $line =~ \
|
| 63 | ((^|[^-])-[A-Za-z0-9?][[:space:]]+)\[?[A-Z0-9]+\]? ]]; do
|
| 64 | line=${line/"${BASH_REMATCH[0]}"/"${BASH_REMATCH[1]}"}
|
| 65 | done
|
| 66 | __parse_options "${line// or /, }"
|
| 67 |
|
| 68 | done
|
| 69 | }
|
| 70 |
|
| 71 | # My addition
|
| 72 | parse_help_file() {
|
| 73 | _parse_help - < "$1"
|
| 74 | }
|
| 75 |
|
| 76 | "$@"
|
| 77 |
|