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
----- 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
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
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: