[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

getoption long question

Daniel Bretoi

10/29/2003 7:23:00 PM

opts = GetoptLong.new(_
···[ "--create-test", "-T", GetoptLong::NO_ARGUMENT ],
)

what if I don't actually want short options such as the -T? (running out
of suitable letters, or something)

How would I use it then?

db

--
A.D. 1844: Samuel Morse invents Morse code. Cryptography export
restrictions prevent the telegraph's use outside the U.S. and Canada.

4 Answers

Simon Strandgaard

10/29/2003 7:32:00 PM

0

On Thu, 30 Oct 2003 04:22:47 +0900, Daniel Bretoi wrote:

> opts = GetoptLong.new(_
> ···[ "--create-test", "-T", GetoptLong::NO_ARGUMENT ],
> )
>
> what if I don't actually want short options such as the -T? (running out
> of suitable letters, or something)
>
> How would I use it then?


How about ?

opts = GetoptLong.new(_
[ "--create-test", GetoptLong::NO_ARGUMENT ],
)


The following code illustrates how I do the same in AEditor:


--
Simon Strandgaard


require 'aeditor/backend/global'
require 'getoptlong'

class Cmdline
class Message < StandardError; end # display message and exit
class Error < StandardError; end # display error+usage and exit

def initialize(files_to_open)
@files_to_open = files_to_open
end
attr_reader :files_to_open
def Cmdline.parse(argv)
save_argv = ARGV.dup
begin
ARGV.replace(argv)
options = GetoptLong.new(
["--version", "-v", GetoptLong::NO_ARGUMENT],
["--help", GetoptLong::NO_ARGUMENT]
)
options.quiet = true
options.each do |opt, arg|
case opt
when "--help"
raise Message, "help message"
when "--version"
raise Message, "ver #{Global::VERSION}"
# else
# raise "Invalid option '#{opt}'"
end
end
# only return Cmdline instance if everything is OK
return Cmdline.new(ARGV.dup)
rescue GetoptLong::InvalidOption
raise Error
ensure
ARGV.replace(save_argv)
end
end
def Cmdline.usage
<<TXT
Usage:
#{File.basename $0} [arguments] [file..] Edit specified file(s)

Arguments:
--help This information you see here and exit.
--version Print version info and exit.
TXT
end
end

Daniel Carrera

10/29/2003 8:35:00 PM

0

> opts = GetoptLong.new(_
> ···[ "--create-test", "-T", GetoptLong::NO_ARGUMENT ],
> )
>
> what if I don't actually want short options such as the -T? (running out
> of suitable letters, or something)
>
> How would I use it then?


opts = GetoptLong.new(
[ "--create-test", GetoptLong::NO_ARGUMENT ]
)

The short options are optional. GetoptLong is very flexible. You can
have as many or as few short and long options as you like.

$ cat test.rb
require 'getoptlong'

opts = GetoptLong.new(
[ "--create-test", GetoptLong::NO_ARGUMENT ]
)
$ ruby test.rb
$

Cheers,
--
Daniel Carrera | OpenPGP KeyID: 9AF77A88
PhD grad student. |
Mathematics Dept. | "To understand recursion, you must first
UMD, College Park | understand recursion".

Daniel Bretoi

10/29/2003 10:33:00 PM

0

On Thu, Oct 30, 2003 at 05:35:04AM +0900, Daniel Carrera wrote:
> > opts = GetoptLong.new(_
> > ???[ "--create-test", "-T", GetoptLong::NO_ARGUMENT ],
> > )
> >
> > what if I don't actually want short options such as the -T? (running out
> > of suitable letters, or something)
> >
> > How would I use it then?
>
>
> opts = GetoptLong.new(
> [ "--create-test", GetoptLong::NO_ARGUMENT ]
> )
>
> The short options are optional. GetoptLong is very flexible. You can
> have as many or as few short and long options as you like.
>
> $ cat test.rb
> require 'getoptlong'
>
> opts = GetoptLong.new(
> [ "--create-test", GetoptLong::NO_ARGUMENT ]
> )
> $ ruby test.rb
> $

Thank you. I swear I tried this before asking, and I got errors. Once
you ask....

db

>
> Cheers,
> --
> Daniel Carrera | OpenPGP KeyID: 9AF77A88
> PhD grad student. |
> Mathematics Dept. | "To understand recursion, you must first
> UMD, College Park | understand recursion".

--
A.D. 1844: Samuel Morse invents Morse code. Cryptography export
restrictions prevent the telegraph's use outside the U.S. and Canada.

Mark Wilson

10/29/2003 10:34:00 PM

0


On Oct 29, 2003, at 2:22 PM, Daniel Bretoi wrote:

> opts = GetoptLong.new(_
> ···[ "--create-test", "-T", GetoptLong::NO_ARGUMENT ],
> )
>
> what if I don't actually want short options such as the -T? (running
> out
> of suitable letters, or something)
>
> How would I use it then?

I recommend using optparse rather than GetoptLong, but you may decide
differently. Information about optparse can be found here:

http://learningruby.com/usingoptp...

Regards,

Mark