Skip to content

Instantly share code, notes, and snippets.

@markusfisch
Last active January 15, 2016 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markusfisch/8953161 to your computer and use it in GitHub Desktop.
Save markusfisch/8953161 to your computer and use it in GitHub Desktop.
Automatically generate help output from function comments in bash scripts

Auto-help output for bash scripts

Use the attached help() function to automatically generate a help/usage output from function comments.

Example:

$ ./sample.sh
blondie - Call Blondie
help - Print help; use "help NAME" to show details

You may also specify commands:

$ ./sample.sh help blondie
blondie is a function
blondie ()
{
	echo 'call me, on the line, call me, call me, call me, anytime, call me'
}
#!/usr/bin/env bash
# Call Blondie
blondie()
{
echo 'call me, on the line, call me, call me, call me, anytime, call me'
}
# Print help; use "help NAME" to show details
help()
{
(( $# > 0 )) && {
type "$@"
return $?
}
local DESC=''
while read -r
do
case $REPLY in
*\(\))
[ "$DESC" ] || continue
echo "${REPLY%(*} - $DESC"
DESC=
;;
\#\ *)
[ "$DESC" ] && continue
DESC=${REPLY#*#}
;;
esac
done < "$0"
}
if [ "${BASH_SOURCE[0]}" == "$0" ]
then
"${@:-help}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment