#!/bin/sh
# sausage CMD... - show inline usage and options
# prints comments at beginning, flags after `# OPTIONS`
#
# ---
# inspired by https://pi.duncano.de/twoman

usage() {
  awk '
  BEGIN { getline; while (getline) if ($0!="#") { if (sub(/^# /,"")) { print } else break } }
  /# OPTIONS$/{ print "options:"; p=1 }
  $1~/^-[^-]/&&p {
    p=2
    gsub(/\|/,", ",$1)
    sub(/\)$/,"",$1)
    $1="\t" $1
    if ($2~/shift[;]?/) {
      $2=""
      sub(/=.*/,"",$3)
      $1=$1" <" $3 ">"
    }
  for (i=2; i<NF&&$i!="#"; i++) $i=""
    $i="\t"
    if (length($1)<15) $i=$i "\t"
      gsub(/ +/," "); gsub(/ *\t */,"\t")
      print
      next
  }
  p==2{exit}
' "$@"
}

for cmd; do
  if [ ! -e "${cmd}" ]; then
    cmd="$(command -v "${cmd}" 2>/dev/null)"
    [ -z "${cmd}" ] && continue
  fi
  usage "${cmd}"
  echo
done
