Shell – Extracting help message from script itself

documentationsedshell-script

I want that my help message be extracted from the script itself.

#!/bin/bash
#
# foo - do things
# Author: John Doe <jhon@doe>
# ----------------------------------------------
# SYNOPSIS
#   foo [OPTIONS] FILE
# 
# DESCRIPTION
#   At vero eos et accusamus et iusto odio
#   dignissimos ducimus qui blanditiis praesenti
#   voluptatum deleniti atque corrupti quos.
# ----------------------------------------------

sed -n '/# -\+$/,/# -\+$/ p' $0

It works! It prints only what is between the two # -\+$ delimiters, inclusive. The problem is that I don't want print the delimiters.

And do you have suggestions for a man page generator with human friendly syntax?

Update: Maybe stated my problem poorly. I want to print only what is between the two lines starting with # ----- and ending with ----.

I know this solution:

sed -n '/# -\+$/,/# -\+$/ p' $0 | head -n -1 | tail -n +2

But I want a clean and elegant solution that doesn't look hackish.

Best Answer

Try this please:

#!/bin/bash
#
# foo - do things
# Author: John Doe <jhon@doe>
# ----------------------------------------------
# SYNOPSIS
#   foo [OPTIONS] FILE
# 
# DESCRIPTION
#   At vero eos et accusamus et iusto odio
#   dignissimos ducimus qui blanditiis praesenti
#   voluptatum deleniti atque corrupti quos.
# ----------------------------------------------


cat `which $0` | sed -n '0,/# -\+$/d;/# -\+$/,$d;p'
Related Question