Skip to content

Instantly share code, notes, and snippets.

@ivanistheone
Forked from kovetskiy/samizdat-shell-help.bash
Last active December 17, 2020 22:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanistheone/0454191800c9caad77a52e0e29c8f31f to your computer and use it in GitHub Desktop.
Save ivanistheone/0454191800c9caad77a52e0e29c8f31f to your computer and use it in GitHub Desktop.
help text for bas script based on ### comment + awk command
#!/bin/bash
###
### my-script — does one thing well
###
### Usage:
### my-script <input> <output>
###
### Options:
### <input> Input file to read.
### <output> Output file to write. Use '-' for stdout.
### -h Show this message.
help() {
awk -F'### ' '/^###/ { print $2 }' "$0"
}
if [[ $# == 0 ]] || [[ "$1" == "-h" ]]; then
help
exit 1
fi
echo Hello World
@ivanistheone
Copy link
Author

Credit @kovetskiy for original idea, @benhutchins for making it cross-platform using awk, and @jdugan1024 for improvements in awk command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment