Last active
December 10, 2015 12:58
-
-
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
This file contains hidden or 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
# Print the current shell script’s initial comment block, sans leading `# ` | |
sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0" |
This file contains hidden or 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
# 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