Created
May 20, 2021 16:19
-
-
Save fabjan/0d6591dfb0d16c7dd869573f7ce74c8d to your computer and use it in GitHub Desktop.
compare documented command usage with a readme
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 | |
# Compare documented usage with actual help output, | |
# to e.g. be used from a Git pre-push hook to keep README's up to date | |
CMD="$1" | |
README="$2" | |
NL="$3" | |
if test -z "$CMD" | |
then | |
echo no command given | |
exit 1 | |
fi | |
if test -z "$README" | |
then | |
echo no README given | |
exit 1 | |
fi | |
if test -z "$NL" | |
then | |
NL="§" | |
fi | |
if test ${#NL} != 1 | |
then | |
echo the newline placeholder must be a single character | |
exit 1 | |
fi | |
# This matches the behaviour of the `flags` package in the Go standard library, | |
# and assumes the readme contains a ```shell``` code block with usage output. | |
documented=$(cat "$README" | tr '\n' "$NL" | grep -o '```shell'"$NL"'Usage of '"$CMD"'[^`]*```' | tr "$NL" '\n') | |
current=$(echo -e '```shell\n'"$("$CMD" -help 2>&1)"'\n```') | |
diff -u <(echo "$documented") <(echo "$current") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment