diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2014-11-27 22:58:50 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2014-11-27 23:03:50 -0500 |
commit | ed4d8acb493dc972764485d67662e57121ef4f2f (patch) | |
tree | e08ce8638bcc35ee7d7f74057bbd9c53e810ecf6 /chardiff.sh | |
parent | 30ad556bfdbd5fefc0fa2451b9e8268b132e39c6 (diff) |
Add proper option parsing to chardiff
Diffstat (limited to 'chardiff.sh')
-rw-r--r-- | chardiff.sh | 68 |
1 files changed, 64 insertions, 4 deletions
diff --git a/chardiff.sh b/chardiff.sh index 7d885b9..c79a102 100644 --- a/chardiff.sh +++ b/chardiff.sh @@ -1,7 +1,67 @@ #!/bin/bash -file1=${1?usage: chardiff FILE1 FILE2 [FLAGS]} -file2=${2?usage: chardiff FILE1 FILE2 [FLAGS]} -shift 2 +# Basically run getopt(1) with arguments that reflect wdiff(1)'s usage +# Except for -d|--diff-input, chardiff doesn't support that. +wdiff_getopt() { + declare ifs="$IFS" + IFS=$'\n' -wdiff "$@" <(chardiff_pre < "$file1") <(chardiff_pre < "$file2") | chardiff_post + declare -a wdiff_flags + wdiff_flags=($( + LC_ALL=C wdiff --help | + sed -rn 's/^ (-.*\S)\s\s.*/\1/p' | + grep -v diff-input | + sed -r \ + -e '/=/{ s/, /:\n/g; s/=.*/:/ }' \ + -e '/^[^=]*$/{ s/, /\n/g }')) + declare -a flags_o flags_l + flags_o=($(printf '%s\n' "${widff_flags[@]}"|sed -n 's/^-[^-]//p')) + flags_l=($(printf '%s\n' "${widff_flags[@]}"|sed -n 's/^--//p')) + + declare o l + IFS='' o="${flags_o[*]}" + IFS=',' l="${flags_l[*]}" + + IFS=$ifs + + declare args + args="$(getopt -n "$0" -o "$o" -l "$l" -- "$@")" || return 1 + + # Check the number of file arguments + eval set -- "$args" + while true; do + case "$1" in + --) shift; break;; + *) shift;; + esac + done + if [[ $# -lt 2 ]]; then + printf '%s: %s\n' "$0" "$(gettext 'missing file arguments')" >&2 + return 1 + elif [[ $# -gt 2 ]]; then + printf '%s: %s\n' "$0" "$(gettext 'too many file arguments')" >&2 + return 1 + fi + + # Return the result + printf '%s' "$args" +} + +main() { + # Normalize the arguments + declare flags + flags="$(wdiff_getopt "$@")" || { wdiff --help >&2; return 1; } + eval set -- "$flags" + + # Run wdiff with our filters + declare -a args=("$@") + declare file2=${args[-1]}; unset args[-1] + declare file1=${args[-1]}; unset args[-1] + set -o pipefail + wdiff "${args[@]}" \ + <(chardiff_pre <"$file1") \ + <(chardiff_pre <"$file2") \ + | chardiff_post +} + +main "$@" |