harzi@toybox:~/g/o/goldenderp-bin $ mkgetopts "$(mkvgenerate-imdb -D declared_option_string)" opt "$(./mkvgenerate-imdb_goldenderp -D declared_option_string)"      
 if [ "$opt" = "q" ] ; then
   opt_q="1"
 elif [ "$opt" = "h" ] ; then
   opt_h="1"
 elif [ "$opt" = "V" ] ; then
   opt_V="1"
 elif [ "$opt" = "D" ] ; then
   opt_D="1"
   D_arg="$OPTARG"
 elif [ "x$opt" = "x-" ] ; then
   break
 elif [ "$opt" = "p" ] ; then
   opt_p="1"
 elif [ "$opt" = "d" ] ; then
   opt_d="1"
 elif [ "$opt" = "g" ] ; then
   opt_g="1"
 elif [ "$opt" = "x" ] ; then
   opt_x="1"
 elif [ "$opt" = "w" ] ; then
   opt_w="1"
 elif [ "$opt" = "o" ] ; then
   opt_o="1"
   o_arg="$OPTARG"
 elif [ "$opt" = "O" ] ; then
   opt_O="1"
 elif [ "$opt" = "n" ] ; then
   opt_n="1"
 elif [ "$opt" = "t" ] ; then
   opt_t="1"
 elif [ "$opt" = "N" ] ; then
   opt_N="1"
 elif [ "$opt" = "S" ] ; then
   opt_S="1"
 fi


#!/usr/bin/env ruby
# generate new manpage with:
# rd2 -r rd/rd2man-lib.rb mkgetopts > ~/.local/share/man/man1/mkgetopts.1
=begin
= NAME
mkgetopts - make getopts parser for shell scripts
= SYNOPSIS
  mkgetopts [-s] option_string variable_name [merge_option_string]...

= DESCRIPTION

= OPTIONS

= FILES

= SEE ALSO
gengetopt(1)
=end
require 'strscan'
require 'optparse'
show_optstring=false
ARGV.options do |q|
  q.banner = "Usage: #{$0} [options] rd-file > output\n"
  q.on_head("global options:")

  q.on("-s", "--show-optstring",
       "also show optstring") do |i|
    show_optstring=true
  end
end  
ARGV.parse!
u=ARGV[0]
o=ARGV[1]
merge=0
if (ARGV.length > 2) then
  merge=1
  m=""
  ARGV[2..-1].each do |a|
    m="#{m}#{a}"
  end
  u="#{u}#{m}"
end
class OptStrScanner
  def initialize(sc)
    @sc=sc
  end
  def each_elt
    while (not @sc.eos?)
      found=@sc.scan(/.:/)
      if (found != nil) then
        yield found
        next
      end
      found=@sc.scan(/./)
      if (found != nil) then
        yield found
      end
    end
  end
end
state=0
sc=StringScanner.new(u)
oss=OptStrScanner.new(sc)
if (merge==1) then
  elts=Hash.new
  oss.each_elt do |c|
    elts[c]=1
  end
  mu=""
  elts.each do |e|
    mu="#{mu}#{e[0]}"
  end
  sc=StringScanner.new(mu)
  oss=OptStrScanner.new(sc)
end
if (show_optstring) then
  if (merge==1) then
    puts "# #{mu}"
  else
    puts "# #{u}"
  end
end

oss.each_elt do |c|
  ifelif = (state == 1) ? 'elif' : 'if'
  if (c.length == 2) then
    puts "  #{ifelif} [ \"$#{o}\" = \"#{c[0]}\" ] ; then"
    puts "    opt_#{c[0]}=\"1\""
    puts "    #{c[0]}_arg=\"$OPTARG\""
  elsif (c == "-")
    puts "  #{ifelif} [ \"x$#{o}\" = \"x#{c}\" ] ; then"
    puts "    break"
  else
    puts "  #{ifelif} [ \"$#{o}\" = \"#{c}\" ] ; then"
    puts "    opt_#{c}=\"1\""
  end
  state = 1
end
puts "  fi"