On Wed, Jul 31, 2002 at 02:17:13PM +0900, Patrick Bennett wrote:
> 
> 
> Jim Freeze wrote:
> 
> >I use getoptlong alot, but I'm not quite sure I understand your problem.
> >Can you provide a short example?
> >
> I thought my message was fairly self-explanatory, but I guess not.  Here 
> 'ya go.
> ----
> I want the user to be able to specify:
> somecommand.rb -l -c (other args)
> 
> and I have two methods (each only caring about certain arguments)
> 
> def method1
>         opts = GetoptLong.new( [ "-l", GetoptLong::NO_ARGUMENT] )
> 
>         opts.each{ |opt,arg| ..... }
> end
> 
> (called later)
> def method2
>         opts = GetoptLong.new( [ "-c", GetoptLong::NO_ARGUMENT] )
> 
>         opts.each{ |opt,arg| ..... }
> end
> 
Ok, now I see. My question is why do you call it twice?
In all of my usage, I collect all the arguments up front.
So, for the above I would do:

  def get_options
    opts = GetoptLong.new(
      [ "--log",  "-l",     GetoptLong::NO_ARGUMENT ],
      [ "--cxyz",  "-c",     GetoptLong::NO_ARGUMENT ]
    )

    opts.each do |opt, arg|
      case opt
        when "--log"
          ...do log setup
          @do_log = true
        when "--cxyz"
          ...do cxyz stuff
        else
          raise "Invalid option '#{opt}'"
      end#case
    end#each

    raise "some error" unless ARGV.size == whatItShouldBe

    file = ARGV.shift
    ...
  end

Then, get_options is called at app launch.

class App
  def initialize
    get_optioins
  rescue => err
      STDERR.puts err
      STDERR.puts usage
      exit 1
  end
  ...
end

App.new.run


> When method1 is called it will raise an exception when it runs into the 
> first argument it doesn't understand (the -c argument), and remove it 
> from ARGV at the same time.
> Later, when method2 is called, the argument(s) it cared about are no 
> longer there to parse.
> 
> Additionally, while I'm on the subject of GetoptLong - in Perl, if I 
> define an option as 'logfile' then getopts will accept --l, --lo, --log, 
> --logf, --logfi, etc. as well as -l, -log, etc.
> Ruby's GetoptLong only allows long names if the user explicitly 
> specifies --xxxx.  A minor (but somewhat annoying) nit.
> 
I agree that it has its limitations, but they have not been sufficient
for me to take the time to do anything about them.
One nice feature I would like to see is support of multiple
options in short form such as in tar:

  tar xzvf blah blah

  instead of

  tar -x -z -v -f blah blah


Jim
-- 
Jim Freeze
If only I had something clever to say for my comment...
~