OptionParser Question

B

Bryan Richardson

[Note: parts of this message were removed to make it a legal post.]

Hello all,

I'm using OptionParser to parse command line options. I've figured out how
to handle options for each flag (i.e. opts.on('--port PORT')), but I'm not
sure how to handle command line arguments that aren't associated with a
flag. For example, say I wanted to parse a file with my file_parser
executable using a certain type of parser. I'd like to say "file_parser
--type CDF <file_name>". I know how to get the type of parser to use (see
above), but I'm not so sure how to easily get the name of the file I want to
parse.

Any suggestions? Thanks! -- BTR
 
A

ara.t.howard

Hello all,

I'm using OptionParser to parse command line options. I've figured
out how
to handle options for each flag (i.e. opts.on('--port PORT')), but
I'm not
sure how to handle command line arguments that aren't associated
with a
flag. For example, say I wanted to parse a file with my file_parser
executable using a certain type of parser. I'd like to say
"file_parser
--type CDF <file_name>". I know how to get the type of parser to
use (see
above), but I'm not so sure how to easily get the name of the file
I want to
parse.

Any suggestions? Thanks! -- BTR

don't bother:

1) gem install main


2) cat a.rb
#! /usr/bin/env ruby

Main {

argument('file_name'){}

option('type'){
argument :required
}

def run
p params['file_name'].given?
p params['file_name']
p params['type'].given?
p params['type']
end
}

a @ http://codeforpeople.com/
 
S

Stefano Crocco

[Note: parts of this message were removed to make it a legal post.]

Alle Thursday 20 December 2007, Bryan Richardson ha scritto:
Hello all,

I'm using OptionParser to parse command line options. I've figured out how
to handle options for each flag (i.e. opts.on('--port PORT')), but I'm not
sure how to handle command line arguments that aren't associated with a
flag. For example, say I wanted to parse a file with my file_parser
executable using a certain type of parser. I'd like to say "file_parser
--type CDF <file_name>". I know how to get the type of parser to use (see
above), but I'm not so sure how to easily get the name of the file I want
to parse.

Any suggestions? Thanks! -- BTR

Command line arguments (that is, those not associated with an option) can be obtained in a different way, depending on wheter you use OptionParser#parse or OptionParser#parse!. The former returns an array with all the arguments; the second modifies its argument, removing all the options (and their arguments) and leaving only the command line arguments. For example:

require 'optparse'

options= {}
o = OptionParser.new do |o|
o.on('--type TYPE', 'Use TYPE parser'){|t| options[:type]=t}
end

args = o.parse ARGV
puts "The arguments are #{args.inspect}"

If you want to use parse!, replace the last two lines with:

o.parse! ARGV
puts "The arguments are #{ARGV.inspect}"

I hope this helps

Stefano
 
R

Robert Klemme

[Note: parts of this message were removed to make it a legal post.]

Alle Thursday 20 December 2007, Bryan Richardson ha scritto:
Hello all,

I'm using OptionParser to parse command line options. I've figured out how
to handle options for each flag (i.e. opts.on('--port PORT')), but I'm not
sure how to handle command line arguments that aren't associated with a
flag. For example, say I wanted to parse a file with my file_parser
executable using a certain type of parser. I'd like to say "file_parser
--type CDF <file_name>". I know how to get the type of parser to use (see
above), but I'm not so sure how to easily get the name of the file I want
to parse.

Any suggestions? Thanks! -- BTR

Command line arguments (that is, those not associated with an option) can be obtained in a different way, depending on wheter you use OptionParser#parse or OptionParser#parse!. The former returns an array with all the arguments; the second modifies its argument, removing all the options (and their arguments) and leaving only the command line arguments. For example:

require 'optparse'

options= {}
o = OptionParser.new do |o|
o.on('--type TYPE', 'Use TYPE parser'){|t| options[:type]=t}
end

args = o.parse ARGV
puts "The arguments are #{args.inspect}"

If you want to use parse!, replace the last two lines with:

o.parse! ARGV
puts "The arguments are #{ARGV.inspect}"

I hope this helps

The question is, does Bryan want to treat the file name as part of the
option (for example, because there might be multiple occurrences) or
whether it's simply one of the names to process. You provide a solution
for the latter. A solution for the other variant might be

o.on('--parse-cdf FILE', 'parse file with CDF') {|file| ...}

i.e. pull the type into the option.

Kind regards

robert
 
B

Bryan Richardson

[Note: parts of this message were removed to make it a legal post.]

Ah yes... this worked great!!!! Thanks! -- BTR

Alle Thursday 20 December 2007, Bryan Richardson ha scritto:
Hello all,

I'm using OptionParser to parse command line options. I've figured out how
to handle options for each flag (i.e. opts.on('--port PORT')), but I'm not
sure how to handle command line arguments that aren't associated with a
flag. For example, say I wanted to parse a file with my file_parser
executable using a certain type of parser. I'd like to say "file_parser
--type CDF <file_name>". I know how to get the type of parser to use (see
above), but I'm not so sure how to easily get the name of the file I want
to parse.

Any suggestions? Thanks! -- BTR

Command line arguments (that is, those not associated with an option) can
be obtained in a different way, depending on wheter you use
OptionParser#parse or OptionParser#parse!. The former returns an array with
all the arguments; the second modifies its argument, removing all the
options (and their arguments) and leaving only the command line arguments.
For example:

require 'optparse'

options= {}
o = OptionParser.new do |o|
o.on('--type TYPE', 'Use TYPE parser'){|t| options[:type]=t}
end

args = o.parse ARGV
puts "The arguments are #{args.inspect}"

If you want to use parse!, replace the last two lines with:

o.parse! ARGV
puts "The arguments are #{ARGV.inspect}"

I hope this helps

Stefano
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top