GetoptLong order of options

Discussion in 'Ruby' started by Milo Thurston, May 11, 2004.

  1. I've just started trying out ruby, having been using perl, and
    encountered a small problem with the getopt module, as follows.
    I re-wrote a script that is meant to glob huge numbers of files,
    to get around the shell's globbing limitations. It has options
    for the suffix to glob "-g" and other things to do e.g. "-d" to
    delete them. However, the options work in one order but not another,
    e.g.

    $ ./globber.rb -g gbk -d
    globs *.gbk and deletes the files.

    $ ./globber.rb -d -g gbk
    Fails to glob any files and exits.

    Here are the relevant parts of the code.

    parser = GetoptLong.new
    parser.ordering = GetoptLong::pERMUTE
    parser.set_options(
    ["-h", "--help", GetoptLong::NO_ARGUMENT],
    ["-d", "--delete", GetoptLong::NO_ARGUMENT],
    ["-g", "--glob", GetoptLong::REQUIRED_ARGUMENT],
    ["-o", "--output", GetoptLong::OPTIONAL_ARGUMENT]
    )

    # process options
    files = nil
    outfile = nil
    loop do
    begin
    opt, arg = parser.get
    break if not opt
    case opt
    when "-g"
    files = Dir["*.#{arg}"]
    when "-d"
    files.each {|f|
    File.unlink(f)
    puts "Deleting #{f}..."
    }
    exit
    end
    end

    if files == nil
    puts "No files available!"
    else
    puts files
    end


    Presumably my problem is the loop, but I can't see any other way to do
    this in the documents. Is there an equivalent of perl's use of a hash
    to process options?
    Thanks for any suggestion.

    --
    Remove surname to reply.
    http://www.bioinf.ceh.ac.uk/lab/
     
    Milo Thurston, May 11, 2004
    #1
    1. Advertising

  2. "Milo Thurston" <> schrieb im Newsbeitrag
    news:c7qnj9$7k3$...
    > I've just started trying out ruby, having been using perl, and
    > encountered a small problem with the getopt module, as follows.
    > I re-wrote a script that is meant to glob huge numbers of files,
    > to get around the shell's globbing limitations. It has options
    > for the suffix to glob "-g" and other things to do e.g. "-d" to
    > delete them. However, the options work in one order but not another,
    > e.g.
    >
    > $ ./globber.rb -g gbk -d
    > globs *.gbk and deletes the files.
    >
    > $ ./globber.rb -d -g gbk
    > Fails to glob any files and exits.
    >
    > Here are the relevant parts of the code.
    >
    > parser = GetoptLong.new
    > parser.ordering = GetoptLong::pERMUTE
    > parser.set_options(
    > ["-h", "--help", GetoptLong::NO_ARGUMENT],
    > ["-d", "--delete", GetoptLong::NO_ARGUMENT],
    > ["-g", "--glob", GetoptLong::REQUIRED_ARGUMENT],
    > ["-o", "--output", GetoptLong::OPTIONAL_ARGUMENT]
    > )
    >
    > # process options
    > files = nil
    > outfile = nil
    > loop do
    > begin
    > opt, arg = parser.get
    > break if not opt
    > case opt
    > when "-g"
    > files = Dir["*.#{arg}"]
    > when "-d"
    > files.each {|f|
    > File.unlink(f)
    > puts "Deleting #{f}..."
    > }
    > exit
    > end
    > end
    >
    > if files == nil
    > puts "No files available!"
    > else
    > puts files
    > end
    >
    >
    > Presumably my problem is the loop, but I can't see any other way to do
    > this in the documents. Is there an equivalent of perl's use of a hash
    > to process options?


    Your problem is that you do the work while you are processing options. If
    you place -d before -g then you won't have globbed anything when
    encountering -d - so there's nothing to delete. Normally you first
    process options and then act on them. This is what I did when using
    GetoptLong (untested):

    require 'getoptlong'

    files = []
    outfile = nil
    delete = nil

    opts = GetoptLong.new(
    ["--help", "-h", GetoptLong::NO_ARGUMENT],
    ["--delete", "-d", GetoptLong::NO_ARGUMENT],
    ["--glob", "-g", GetoptLong::REQUIRED_ARGUMENT],
    ["--output", "-o", GetoptLong::OPTIONAL_ARGUMENT]
    )

    opts.each do |opt, arg|
    case opt
    when "--help"
    puts "Help!"
    when "--delete"
    delete = true
    when "--glob"
    files.concat Dir["*.#{arg}"]
    when "--output"
    outfile = arg
    else
    raise "Error"
    end
    end

    if files.empty?
    $stderr.puts "no files"
    else
    files.each do |f|
    puts "Deleting #{f}"
    File.unlink(f)
    end if delete

    puts files
    end

    Note: now that optparse is part of the std distribution you might want to
    look into that, too.

    Regards

    robert
     
    Robert Klemme, May 11, 2004
    #2
    1. Advertising

  3. Robert Klemme <> wrote:
    > This is what I did when using
    > GetoptLong (untested):


    Thanks, I'll try that out.

    > Note: now that optparse is part of the std distribution you might want to
    > look into that, too.


    Is there any on-line documentation for it? I had some trouble trying to
    find the getoptlong info.
    Thanks.

    --
    Remove surname to reply.
    http://www.bioinf.ceh.ac.uk/lab/
     
    Milo Thurston, May 12, 2004
    #3
  4. "Milo Thurston" <> schrieb im Newsbeitrag
    news:c7sr48$36t$...
    > Robert Klemme <> wrote:
    > > This is what I did when using
    > > GetoptLong (untested):

    >
    > Thanks, I'll try that out.
    >
    > > Note: now that optparse is part of the std distribution you might want

    to
    > > look into that, too.

    >
    > Is there any on-line documentation for it? I had some trouble trying to
    > find the getoptlong info.
    > Thanks.


    http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html
    http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html

    Regards

    robert
     
    Robert Klemme, May 12, 2004
    #4
    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. Baron Samedi

    Seeking getoptlong()

    Baron Samedi, Mar 22, 2007, in forum: C Programming
    Replies:
    2
    Views:
    251
    Kenny McCormack
    Mar 22, 2007
  2. Massimiliano Mirra - bard

    From getoptlong to optparse

    Massimiliano Mirra - bard, Nov 2, 2004, in forum: Ruby
    Replies:
    14
    Views:
    398
    Robert Klemme
    Nov 14, 2004
  3. Daniel Berger

    GetoptLong bug?

    Daniel Berger, Mar 8, 2005, in forum: Ruby
    Replies:
    3
    Views:
    144
    Daniel Berger
    Mar 8, 2005
  4. Replies:
    2
    Views:
    214
  5. Steve Litt

    GetoptLong example

    Steve Litt, Dec 15, 2005, in forum: Ruby
    Replies:
    12
    Views:
    1,088
    Jim Freeze
    Dec 15, 2005
Loading...

Share This Page