OptionParser question

Hi,

  how can I parse options with OptionParser that are not prefixed
by a "-x" or "--xxx"?

My script should be called like:
webserver.rb <options> <command>
e.g.
webserver.rb --maintenance start
or
webserver.rb start

There is a fixed number of commands that can be given,
e.g. start/stop/restart/sync

I'im thinking in the lines of (which doesn't work ...):

     opts.on("COMMAND", ["start", "stop", "restart", "sync"] , " tell the webserver what to do, e.g. start") { |@command| }

Is there a way to instruct OptionParser to require this?

Thanx

Christian

Christian van der Leeden.vcf (532 Bytes)

···

-----------------------------------------------
Christian van der Leeden
Software Engineering

OptionParser only concerns itself with -x and --xxx. Anything else
(like 'start') will be left in ARGV after you have processed the
options.

So, given ARGV == ['--maintenance', 'start']

  parser = OptionParser.new do |p|
    p.on('--maintenance', ...) do { ... }
  end
  parser.parse!(ARGV)
  
  # Now ARGV == ['start']

Cheers,
Gavin

···

On Tuesday, July 13, 2004, 7:49:53 AM, Christian wrote:

Hi,

  how can I parse options with OptionParser that are not prefixed
by a "-x" or "--xxx"?

My script should be called like:
webserver.rb <options> <command>
e.g.
webserver.rb --maintenance start
or
webserver.rb start

As far as I know, you can't do this. For items that aren't preceded by
-/-- or follow ' -- ', it's your responsibility to handle.

-austin

···

On Tue, 13 Jul 2004 06:49:53 +0900, Christian van der Leeden <lists@logicunited.com> wrote:

        how can I parse options with OptionParser that are not prefixed
by a "-x" or "--xxx"?

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Suggestion: do not use OptionParser.

Download and install my port of Damian Conway's Getopt::Declare (at
rubyforge.net).

You can then just write code as simple as:

require "Getopt/Declare"

args = Getopt::Declare.new(<<'EOPARAM')
  [pvtype: cmd /start|stop/ ]
   -maintenance Go into maintenance mode
   <cmd:cmd> Begin/Stop script [required]
                        Use start or stop.
EOPARAM

print args.inspect

···

-------------------------------------------------

test.rb

Error: required parameter '<cmd>' not found.

(try 'C:/ruby/learning/test.rb -help' for more information)

test.rb -h

test.rb: version dated Mon Jul 12 21:55:16 Hora est. de SudamÚrica E. 2004

Usage: test.rb [options] <cmd>
       test.rb -help
       test.rb -version

Options:
   -maintenance Go into maintenance mode
   <cmd> Begin/Stop script
                        Use start or stop.

test.rb start

<cmd> => "start"
Unused:

test.rb -maintenance start

<cmd> => "start"
-maintenance => "-maintenance"
Unused: