Skip to content

Instantly share code, notes, and snippets.

@mrinalwadhwa
Created August 3, 2016 17:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrinalwadhwa/3d6adcbcd89398ec103e09eab2a78577 to your computer and use it in GitHub Desktop.
Save mrinalwadhwa/3d6adcbcd89398ec103e09eab2a78577 to your computer and use it in GitHub Desktop.
Extract and display block of usage info
#!/usr/bin/env bash
##
## Usage: usage [command|path]
## Assuming that a command script file starts with a block of usage info
## where each line in the block starts with two '##' characters. This script
## extracts and displays that block of usage info.
##
## Example:
## Given a file a_command with the following contents:
##
## #!/usr/bin/env bash
## ##
## ## Usage: a_command <args>
## ## This is a_command
## ##
##
## > usage ~/bin/a_command
##
## Usage: a_command <args>
## This is a_command
## ...
##
## If the command a_command is in PATH
## > usage a_command
##
## Usage: a_command <args>
## This is a_command
## ...
##
if [ $# -ne 0 ]; then
grep "^##" "$(which "$1")" | sed "s/^##//"
else
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment