OptionParser can't tell me about non-options on command line

Discussion in 'Ruby' started by Alan Partis, Dec 23, 2008.

  1. Alan Partis

    Alan Partis Guest

    I'd like to have a command line invocation of my ruby script that looks
    like the following:

    Usage: vmci.rb [options] command args ...

    options:
    -h, -?, --help generate this usage message
    -l, --log-file FILE Identifies a log file to use.
    -e, --log-level LOGLEVEL desired level of logging.
    -x Bypass display of configuration

    command
    one of the command recognized by vmci.rb

    args
    any necessary arguments associated with the given command

    I can't figure out how to get at the "command" and "args" portions of
    the command line using OptionParser. Since a variable number of options
    may exist on the command line before the command, it's not intuitive to
    me how to know which element of the ARGV array to use, or how to extract
    these values via OptionParser.

    Can someone give me a little direction?

    Thanks much.

    ________________________________________________________
    alan partis
    thundernet development group
    --
    Posted via http://www.ruby-forum.com/.
    Alan Partis, Dec 23, 2008
    #1
    1. Advertising

  2. Alan Partis

    Alan Partis Guest

    Alan Partis wrote:
    > Can someone give me a little direction?


    Nevermind ... RTFM ... I just went back to pickaxe 3rd edition and found
    that the call to OptionParser.parse returns an array containing the
    remaining elements from ARGV that weren't processed as options.

    ________________________________________________________
    alan partis
    thundernet development group

    --
    Posted via http://www.ruby-forum.com/.
    Alan Partis, Dec 23, 2008
    #2
    1. Advertising

  3. On 23.12.2008 21:40, Alan Partis wrote:
    > Alan Partis wrote:
    >> Can someone give me a little direction?

    >
    > Nevermind ... RTFM ... I just went back to pickaxe 3rd edition and found
    > that the call to OptionParser.parse returns an array containing the
    > remaining elements from ARGV that weren't processed as options.


    And if you use parse! (which is what I generally do) the original array
    is adjusted appropriately. This is important if you want to use ARGF
    for example. My typical pattern is

    OptionParser.new do |opts|
    opts.on...
    end.parse! ARGV
    # now ARGV has only non args left

    Kind regards

    robert
    Robert Klemme, Dec 24, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yogesh
    Replies:
    1
    Views:
    366
    Victor Bazarov
    Mar 14, 2007
  2. Replies:
    0
    Views:
    406
  3. News123
    Replies:
    6
    Views:
    1,229
    Michele Simionato
    Aug 3, 2010
  4. Bryan Richardson
    Replies:
    6
    Views:
    282
    Bryan Richardson
    Feb 25, 2008
  5. Adam Penny
    Replies:
    3
    Views:
    233
    Adam Penny
    Oct 18, 2008
Loading...

Share This Page