Last active
June 7, 2016 17:11
-
-
Save toonetown/7b10f70daf0b647d671c08cb2e2740b6 to your computer and use it in GitHub Desktop.
A script which performs some code style checking (line length, trailing spaces, etc.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
####### | |
# A script which finds various code violations (such as line spacing or trailing spaces) | |
# | |
_SCRIPT="${0}" | |
_QUIET=""; _TRAIL_ONLY=""; _EMPTY=""; _LENGTH_ONLY=""; _MAX_LENGTH=120; _PATHS=(); _ERR="" | |
_err () { "${_SCRIPT}" __XXX__ "$@"; exit $?; } | |
while [ $# -gt 0 ]; do | |
case "${1}" in | |
"-h"|"--help"|"__XXX__") | |
if [ "${1}" == "__XXX__" ]; then | |
_O=/dev/stderr; _R=1; _MSGS=("$@") | |
if [ ${#_MSGS[@]} -gt 1 ]; then | |
echo "Error:" >${_O} | |
for ((i = 1; i < ${#_MSGS[@]}; i++)); do echo " ${_MSGS[${i}]}" >${_O}; done | |
echo "" >${_O} | |
fi | |
else | |
_O=/dev/stdout; _R=0 | |
fi | |
echo "Usage: ${0} [options...] <path> [path...]" >${_O} | |
echo "" >${_O} | |
echo "Options:" >${_O} | |
echo " -h, --help Shows this help" >${_O} | |
echo " -q, --quiet Show less output" >${_O} | |
echo " -t, --trailing-only Only run the trailing space check" >${_O} | |
echo " -e, --match-empty Matches all whitespace (empty) lines as trailing space" >${_O} | |
echo " -l, --length-only Only run the line length check" >${_O} | |
echo " -m, --max-length <length> Use <length> as the line length max (default 120)" >${_O} | |
echo "" >${_O} | |
exit 1; | |
;; | |
"-q"|"--quiet") | |
_QUIET="y" | |
;; | |
"-t"|"--trailing-only") | |
[ -z "${_LENGTH_ONLY}" ] || _err "Cannot set -t and -l together" | |
_TRAIL_ONLY="y" | |
;; | |
"-e"|"--match-empty") | |
_EMPTY="y" | |
;; | |
"-l"|"--length-only") | |
[ -z "${_TRAIL_ONLY}" ] || _err "Cannot set -t and -l together" | |
_LENGTH_ONLY="y" | |
;; | |
"-m"|"--max-length") | |
shift; [ "${1}" -eq "${1}" ] 2>/dev/null || _err "Invalid max-length '${1}'" | |
_MAX_LENGTH=${1} | |
;; | |
*) | |
[ -e "${1}" ] || _err "Path '${1}' does not exist" | |
_PATHS+=("${1}") | |
;; | |
esac | |
shift | |
done | |
if [ ${#_PATHS[@]} -eq 0 ]; then _err "At least one path should be set"; fi | |
if [ -z "${_LENGTH_ONLY}" ]; then | |
[ -z "${_QUIET}" ] && { | |
echo "===================================================" | |
echo "Lines with trailing spaces:" | |
echo "----------------------------" | |
} | |
if [ -n "${_EMPTY}" ]; then | |
grep --color -Irn '^.* \+$' ${_PATHS[@]} && _ERR="y" || { | |
[ -z "${_QUIET}" ] && echo "No violations"; | |
} | |
else | |
grep --color -Irn '^.*[^ ] \+$' ${_PATHS[@]} && _ERR="y" || { | |
[ -z "${_QUIET}" ] && echo "No violations"; | |
} | |
fi | |
[ -z "${_QUIET}" ] && echo "" | |
fi | |
if [ -z "${_TRAIL_ONLY}" ]; then | |
[ -z "${_QUIET}" ] && { | |
echo "===================================================" | |
echo "Lines with length > ${_MAX_LENGTH}:" | |
echo "----------------------------" | |
} | |
grep --color -Irn "^.\{${_MAX_LENGTH},\}\$" ${_PATHS[@]} && _ERR="y" || { | |
[ -z "${_QUIET}" ] && echo "No violations"; | |
} | |
[ -z "${_QUIET}" ] && echo "" | |
fi | |
[ -z "${_ERR}" ] || { | |
echo "!!! Found violations !!!" >&2 | |
echo "" >&2 | |
exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment