Sweatless –help text

I just stumbled upon a nice way to generate the --help text from the comments at the top of the file.

Here’s how it’s done in hub‘s build script:

#!/usr/bin/env bash
# Usage: script/build [-o output] [test]
#
# Sets up GOPATH and compiles hub. With `test`, runs tests instead.

#[blah]

case "$1" in
 #[blah]
 -h | --help )
  sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0"
  exit
  ;;
 #[blah]
esac

This lets you maintain your help text at the top of the file only, which is a convenient and straigthforward place to look for it when editing the file, and have the --help case short and simpleclever.

I like it. :)

Leave a Reply

Your email address will not be published. Required fields are marked *