Skip to content

Instantly share code, notes, and snippets.

@mislav
Last active December 10, 2015 12:58
Show Gist options
  • Save mislav/4437500 to your computer and use it in GitHub Desktop.
Save mislav/4437500 to your computer and use it in GitHub Desktop.
Self-documenting shell program by Sam Stephenson https://twitter.com/sstephenson/status/285487626570579968
# Print the current shell script’s initial comment block, sans leading `# `
sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0"
# Example: executable that aborts with documentation on stderr when no
# required argument is given, but stdin is associated with a terminal.
# On `-h` or `--help`, puts docs to stdout.
abort=0
[[ -z $1 && -t 0 ]] && abort=1
if [[ $abort -gt 0 || $1 = "-h" || $1 = "--help" ]]; then
sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0" >&$((abort+1))
exit $abort
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment