getoption long question

D

Daniel Bretoi

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
 
S

Simon Strandgaard

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
 
D

Daniel Carrera

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,
 
D

Daniel Bretoi

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
 
M

Mark Wilson

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/usingoptparse.shtml

Regards,

Mark
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top