#!/bin/bash # Copyright (C) 2011-2014 Luke Shumaker # 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' 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' "${wdiff_flags[@]}"|sed -rn 's/^-([^-])/\1/p')) flags_l=($(printf -- '%s\n' "${wdiff_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 "$@"