%# -*-mode: ruby-*- %define c " " #!/usr/bin/env ruby =begin -*-mode: ruby-*- <:NAME> %license <:DATE> <:AUTHOR> <<:EMAIL>> =end %if defined("MAIN") %if defined("OPTIONS") require "optparse" require "shellwords" %endif # defined("OPTIONS") %if defined("PKGDIR") BEGIN { $LOAD_PATH.unshift("@prefix@/share/@PACKAGE@") } %endif # defined("PKGDIR") ###################################################################### $g_opts = nil $g_args = nil ###################################################################### # main(): application itself starts here. # def main(opts, args) return 0 end # main(opts, args) ###################################################################### # Static constants. # PACKAGE = '@PACKAGE@' PKGVERSION = '@VERSION@' APPNAME = File.basename($0) APPVERSION = '$Revision: 1.1.1.1 $'.split[1] COPYRIGHT = <<"EOF" Copyright (c) <:YEAR> <:OWNER> <<:EMAIL>>. All rights reserved. EOF BANNER = "This is #{APPNAME} v. #{PKGVERSION}.\n\n#{COPYRIGHT}" USAGE = <<"EOF" #{BANNER} usage: #{APPNAME} [options] [file ...] EOF %if defined("OPTIONS") ###################################################################### # Wrapper for command line parser # class <:OPTIONS> Width = 32 NL = "\n%*s" % [5 + Width, ''] # Initialize with help, version text. # [usage] top part of message shown for help option # [banner] message shown for version and copyright info def initialize(usage, banner) @vals = Hash.new @banner = banner @parser = OptionParser.new(usage, Width) # set default values @vals["v"] = false @vals["myname"] = APPNAME # define program options @parser.def_option("-h", "--help", "Show this help.") { show_help } @parser.def_option("-V", "--version", "Show version and copyright.") { show_banner } @parser.def_option("-v", "--verbose", "Turn on verbose messages.") { |v| @vals["v"] = v } end # initialize(usage, banner) attr_reader :vals # [s] name of env var to parse for command line options def env(s) envopts = ENV[s] || ENV[s.upcase] @parser.order(*Shellwords::shellwords(envopts)) if envopts end # env(s) # [av] array of command line options, usually ARGV passed in, # in one form or another def parse(av) @parser.order(*av) end # parse(av) # Show help and exit. def show_help(rc = 0) print @parser; exit(rc) end # show_help(rc = 0) # Show help and exit. def show_banner(rc = 0) print @banner; exit(rc) end # show_banner(rc = 0) end # class <:OPTIONS> %endif defined("OPTIONS") ###################################################################### # Entry point if called as executable. # def start(av) %if defined("OPTIONS") opts = <:OPTIONS>.new(USAGE, BANNER); $g_opts = opts.vals $g_args = opts.parse(av) %else $g_opts = { "myname" => APPNAME, "v" => false } $g_args = av %endif defined("OPTIONS") main($g_opts, $g_args) end # start(av) if $0 == __FILE__ exit(start(ARGV)) end # $0 == __FILE__ %endif # defined("MAIN") #EOF %end Uses: MAIN, PKGDIR, OPTIONS MAIN: If MAIN is defined, include code to set up a standalone program. If not defined, the other options have no effect. PKGDIR: Includes code to tweak $LOAD_PATH so the program can be installed in PKGDATADIR ($PREFIX/share/program). OPTIONS: Includes a custom option parsing class based on OptionParser. This also provides usage messages and version/copyright info to the user.