When a ruby script gets arguments passed to it

Dear All,
I was wondering what kinds of command line parsing I
can do. I mean, if I have a script foo.rb and I
type:

./foo -c “comment string” -t type -o option

OR I type

./foo -c"comment string" -ttype -ooption

in a “vanilla” ruby script, will it be like “vanilla"
bourne shell options ie: the -c would be the $1
"comment string” would be $2… and so on.

What is the “vanilla” method, and what is the
"smarter" way to parse it. In the bourne shell, I can
use getopts but I can’t handle if the person omits the
space between arguments [ perhaps I need a patch ].
Anyway, I want to setup an environment and then run a
further command with arguments that I figured out.
ie
foo.rb -c “test comment” -d pgroup1

results in lp -t “test comment” -d printer2

or results in lp -t “test comment” -d fax1

based on other command line or environment.

Thanks very much in advance !

Greg

···

Do you Yahoo!?
Yahoo! Web Hosting - establish your business online
http://webhosting.yahoo.com

Global aray ARGV – contains command line parameters.
GetoptLong – parses options, a.l.a shell’s getopts, but better (search
http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib_standard.html
for GetoptLong description)

Gennady.

···

----- Original Message -----
From: “Greg Banschbach” glbny@yahoo.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, March 11, 2003 11:36 AM
Subject: when a ruby script gets arguments passed to it…

Dear All,
I was wondering what kinds of command line parsing I
can do. I mean, if I have a script foo.rb and I
type:

./foo -c “comment string” -t type -o option

OR I type

./foo -c"comment string" -ttype -ooption

in a “vanilla” ruby script, will it be like “vanilla”
bourne shell options ie: the -c would be the $1
“comment string” would be $2… and so on.

What is the “vanilla” method, and what is the
“smarter” way to parse it. In the bourne shell, I can
use getopts but I can’t handle if the person omits the
space between arguments [ perhaps I need a patch ].
Anyway, I want to setup an environment and then run a
further command with arguments that I figured out.
ie
foo.rb -c “test comment” -d pgroup1

results in lp -t “test comment” -d printer2

or results in lp -t “test comment” -d fax1

based on other command line or environment.

Thanks very much in advance !

Greg


Do you Yahoo!?
Yahoo! Web Hosting - establish your business online
http://webhosting.yahoo.com

There is a library distributed with ruby to parse arguments. As an
alternative, I have an argument parsing module which I will send to you if
you like. I will release it generally when I’ve completed some
documentation. For the one distributed with most linux distros there is a
program in the ruby examples directory which uses it.

In general, the best syntax (IMHO, of course) is the extract each argument
from the input list using ARGV.shift. Other methods for accessing members of
a string array can also be used.

···

On Tuesday 11 March 2003 12:36 pm, Greg Banschbach wrote:

Dear All,
I was wondering what kinds of command line parsing I
can do. I mean, if I have a script foo.rb and I
type:

./foo -c “comment string” -t type -o option

OR I type

./foo -c"comment string" -ttype -ooption

in a “vanilla” ruby script, will it be like “vanilla”
bourne shell options ie: the -c would be the $1
“comment string” would be $2… and so on.

What is the “vanilla” method, and what is the
“smarter” way to parse it. In the bourne shell, I can
use getopts but I can’t handle if the person omits the
space between arguments [ perhaps I need a patch ].
Anyway, I want to setup an environment and then run a
further command with arguments that I figured out.
ie
foo.rb -c “test comment” -d pgroup1

results in lp -t “test comment” -d printer2

or results in lp -t “test comment” -d fax1

based on other command line or environment.

Thanks very much in advance !

Greg


Do you Yahoo!?
Yahoo! Web Hosting - establish your business online
http://webhosting.yahoo.com


Seth Kurtzberg
M. I. S. Corp.
480-661-1849
seth@cql.com

Seth Kurtzberg wrote:

In general, the best syntax (IMHO, of course) is the extract each argument
from the input list using ARGV.shift. Other methods for accessing members of
a string array can also be used.

If the args are non-positional and do not contain whitespace, you can
use ARGV.delete:

foo_option = ARGV.delete(‘-foo’)
bar_option = ARGV.delete(‘-bar’)

ARGF.each {|line|
… # treat remaining args as files
}

Of course, this fails if your files are named -foo or -bar…