On Thursday 15 December 2005 05:50 pm, Jim Freeze wrote:

> Here is the above code re-writen using CommandLine as
> requested by Steve. It is a fully working example.
>
> require 'rubygems'
> require 'commandline'
>
> class App < CommandLine::Application
>   def initialize
>     synopsis "[-d] [-s [opt_arg]] [-o req_arg]"
>     short_description "A sample program"
>     option :names => %w(-short -s),
>
>                :arity     => [0,1],
>                :arg_description => "opt_arg"
>                :opt_found => get_arg,
>                :opt_not_found => nil
>
>     option :names => %w(--offset -o),
>
>                :arg_description => "req_arg",
>                :opt_found => get_args
>
>     option :help
>   end
>
>   def main
>     puts "Short arg is=>#{opt["--short"]}<" unless
> opt["--short"].nil? puts "Offset arg is=>#{opt["--offset"]}<"
>   end
>
> end#class App

The preceding has a couple typos that keep it from running. The 
following cures the typos, and also adds an argument called 
"--myflag" that is a flag that can go on or off, but errors out if 
you try to give it an argument:

#!/usr/bin/env ruby
require 'rubygems'
require 'commandline'

class App < CommandLine::Application
  def initialize
    synopsis "[-d] [-s [opt_arg]] [-o req_arg]"
    short_description "A sample program"
    option :names => %w(--short -s),
               :arity     => [0,1],
               :arg_description => "opt_arg",
               :opt_found => get_arg,
               :opt_not_found => nil

    option :names => %w(--offset -o),
               :arg_description => "req_arg",
               :opt_found => get_args

    option :help
    option :names => %w(--myflag -m),
               :arity     => [0,0],
    		:arg_description => "flag",
		:opt_found => get_arg
  end

  def main
  puts "Short arg is=>#{opt["--short"]}<" unless opt["--short"].nil?
  puts "Offset arg is=>#{opt["--offset"]}<"
  puts "Myarg arg is=>#{opt["--myflag"]}<"
  end

end#class App