GetoptLong example

S

Steve Litt

Hi all,

Anyone have a short and easy GetoptLong example in Ruby? It's not
object oriented in Perl or C, so I'm in unfamiliar territory.

Thanks

SteveT

Steve Litt
http://www.troubleshooters.com
(e-mail address removed)


Hi all,

I cruised the net, found an example, and turned it into a small
program that works. So if anyone wants to use GetoptLong, here it
is:

#!/usr/bin/ruby
require "getoptlong"

getoptlong = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--short', '-s', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--offset', '-o', GetoptLong::REQUIRED_ARGUMENT ]
)


def usage()
puts "Usage:"
puts "myprog [--help] [--short optional_arg] [--offset
required_arg]"
end

short_arg = nil # declare local var to store arg
offset_arg = nil # declare local var to store arg

begin
getoptlong.each do |opt, arg|
case opt
when "--help"
puts "dia help"
when "--short"
print "Short option. Arg is=>"
print arg
short_arg = arg
print "<\n"
when "--offset"
print "Offset option. Arg is=>"
print arg
offset_arg = arg
print "<\n"
end
end
rescue StandardError=>my_error_message
puts
puts my_error_message
usage()
puts
exit
end

unless short_arg == ""
puts
print "Short arg is=>"
print short_arg
puts "<"
end

print "Offset arg is=>"
print offset_arg
puts "<"




Steve Litt
http://www.troubleshooters.com
(e-mail address removed)
 
S

Steve Litt

Have you tried OptionParser? It is fully OO and integrated with
Application.

Yes, that was the sound of my own horn. :)

I know CommandLine cannot solve everyones needs, but I would be
interested to hear about it if it doesn't meet your needs. Maybe
there is something I can change or add to the library.

My nefarious global take over plans are to see CommandLine -
which has a Ruby flavor - be put in the stdlib and surplant the
existing optparse and getoptlong parsers which use aging
methodologies.

Hi Jim,

I came across OptionParser. I tried reading the documentation, but I
saw no simple proof of concept to get me started. I therefore opted
for GetoptLong. At least it's not a total stranger, I've used it in
C and Perl.

Perhaps I just came across the wrong documentation.

I'd like to have the OptionParser documentation contain a proof of
concept, and a couple more examples that follow logically, without
skipping steps.



Thanks

SteveT

Steve Litt
http://www.troubleshooters.com
(e-mail address removed)
 
W

Wybo Dekker

I came across OptionParser. I tried reading the documentation, but I
saw no simple proof of concept to get me started. I therefore opted
for GetoptLong. At least it's not a total stranger, I've used it in
C and Perl.

Perhaps I just came across the wrong documentation.

I'd like to have the OptionParser documentation contain a proof of
concept, and a couple more examples that follow logically, without
skipping steps.

http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html

is good, although I don't like the use of ostruct. For a cleaner, yet
extensive, construction see http://www.servalys.nl/scripts/mk with its
rdoc at http://www.servalys.nl/scripts/doc/files/mk.html
 
J

Jim Freeze

Hi Steve

I came across OptionParser. I tried reading the documentation, but I
saw no simple proof of concept to get me started. I therefore opted
for GetoptLong. At least it's not a total stranger, I've used it in
C and Perl.

Perhaps I just came across the wrong documentation.

I'd like to have the OptionParser documentation contain a proof of
concept, and a couple more examples that follow logically, without
skipping steps.

Is this the documentation that you are referring to that needs improvement:

http://rubyforge.org/docman/view.php/632/233/posted-docs.index.html

Please help me out and post some comments on what you like and don't
like. I am still making improvements in the docs.
 
S

Steve Litt

Hi Steve



Is this the documentation that you are referring to that needs
improvement:


http://rubyforge.org/docman/view.php/632/233/posted-docs.index.ht
ml

Please help me out and post some comments on what you like and
don't like. I am still making improvements in the docs.

Hi Jim,

I'm pretty sure those are the docs I couldn't fathom. IMHO what you
need is a quickstart -- all code, no theory. Start with the
simplest possible program that the user can paste into his editor
and then give him the command line to run, and show the expected
output.

I think the idea is to get the user to say "hey, I can do this".
Once he's hooked, he has incentive to dig into the incredibly rich
array of tools you offer.

Personally, when I write documentation on Troubleshooters.Com, I
always start with a "Hello World" and then, one feature at a time,
I move from the known to the unknown (my wife's a teacher :).

Another thing. I usually show full programs (in blue background) so
the user can paste them into his editor and run them immediately,
and then the expected results in yellow background so the user can
compare his results to the expected results. Once the user sees
both the code and the results, he can then figure out why it works
like it does, and he can also experiment with the code to learn
even more.

For an example of how I write my documentation, see this:

http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm

Please keep in mind that I'm displaying this as an example of my
documentation methods, not as an example of particularly good Ruby
documentation. I wrote most of this material with less than a
week's Ruby experience, and before I signed up with this mailing
list. In fact, a companion document called "Ruby the Right Way" was
my reason for my "They say I write Ruby like Perl" post.

If any of you have the book "Samba Unleashed", the chapters with my
name on them are another good example of my documentation methods.

In summary -- IMHO a quickstart would be the catalyst to greater
acceptance of OptionParser. Use whole but tiny programs, without
error checking and the like, to make your points. That way the user
can cut and paste right onto his box and follow your examples on
his own. Within 10 minutes the user understands the power of
OptionParser and is ready to learn more.

Thanks

SteveT

Steve Litt
http://www.troubleshooters.com
(e-mail address removed)
 
J

Jim Freeze

I cruised the net, found an example, and turned it into a small
program that works. So if anyone wants to use GetoptLong, here it
is:

#!/usr/bin/ruby
require "getoptlong"

getoptlong =3D GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--short', '-s', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--offset', '-o', GetoptLong::REQUIRED_ARGUMENT ]
)


def usage()
puts "Usage:"
puts "myprog [--help] [--short optional_arg] [--offset
required_arg]"
end

short_arg =3D nil # declare local var to store arg
offset_arg =3D nil # declare local var to store arg

begin
getoptlong.each do |opt, arg|
case opt
when "--help"
puts "dia help"
when "--short"
print "Short option. Arg is=3D>"
print arg
short_arg =3D arg
print "<\n"
when "--offset"
print "Offset option. Arg is=3D>"
print arg
offset_arg =3D arg
print "<\n"
end
end
rescue StandardError=3D>my_error_message
puts
puts my_error_message
usage()
puts
exit
end

unless short_arg =3D=3D ""
puts
print "Short arg is=3D>"
print short_arg
puts "<"
end

print "Offset arg is=3D>"
print offset_arg
puts "<"

Here is the above code re-writen using CommandLine as
requested by Steve. It is a fully working example.

require 'rubygems'
require 'commandline'

class App < CommandLine::Application
def initialize
synopsis "[-d] [-s [opt_arg]] [-o req_arg]"
short_description "A sample program"
option :names =3D> %w(-short -s),
:arity =3D> [0,1],
:arg_description =3D> "opt_arg"
:eek:pt_found =3D> get_arg,
:eek:pt_not_found =3D> nil

option :names =3D> %w(--offset -o),
:arg_description =3D> "req_arg",
:eek:pt_found =3D> get_args

option :help
end

def main
puts "Short arg is=3D>#{opt["--short"]}<" unless opt["--short"].nil?
puts "Offset arg is=3D>#{opt["--offset"]}<"
end

end#class App
 
S

Steve Litt

Here is the above code re-writen using CommandLine as
requested by Steve. It is a fully working example.

require 'rubygems'
require 'commandline'

class App < CommandLine::Application
def initialize
synopsis "[-d] [-s [opt_arg]] [-o req_arg]"
short_description "A sample program"
option :names => %w(-short -s),

:arity => [0,1],
:arg_description => "opt_arg"
:eek:pt_found => get_arg,
:eek:pt_not_found => nil

option :names => %w(--offset -o),

:arg_description => "req_arg",
:eek:pt_found => get_args

option :help
end

def main
puts "Short arg is=>#{opt["--short"]}<" unless
opt["--short"].nil? puts "Offset arg is=>#{opt["--offset"]}<"
end

end#class App

The preceding has a couple typos that keep it from running. The
following cures the typos, and also adds an argument called
"--myflag" that is a flag that can go on or off, but errors out if
you try to give it an argument:

#!/usr/bin/env ruby
require 'rubygems'
require 'commandline'

class App < CommandLine::Application
def initialize
synopsis "[-d] [-s [opt_arg]] [-o req_arg]"
short_description "A sample program"
option :names => %w(--short -s),
:arity => [0,1],
:arg_description => "opt_arg",
:eek:pt_found => get_arg,
:eek:pt_not_found => nil

option :names => %w(--offset -o),
:arg_description => "req_arg",
:eek:pt_found => get_args

option :help
option :names => %w(--myflag -m),
:arity => [0,0],
:arg_description => "flag",
:eek:pt_found => get_arg
end

def main
puts "Short arg is=>#{opt["--short"]}<" unless opt["--short"].nil?
puts "Offset arg is=>#{opt["--offset"]}<"
puts "Myarg arg is=>#{opt["--myflag"]}<"
end

end#class App
 
S

Steve Litt

I cruised the net, found an example, and turned it into a small
[clip]
Here is the above code re-writen using CommandLine as
requested by Steve. It is a fully working example.

NOW I think I understand CommandLine::Application. It's like a
GetoptLong that also gives you a usage() subroutine for free, and
if you change your arguments, usage() automatically changes in
sync. Very nice!

SteveT

Steve Litt
http://www.troubleshooters.com
(e-mail address removed)
 
J

Jim Freeze

On Thursday 15 December 2005 05:50 pm, Jim Freeze wrote:
The preceding has a couple typos that keep it from running. The
following cures the typos, and also adds an argument called
"--myflag" that is a flag that can go on or off, but errors out if
you try to give it an argument:

Thanks for fixing the errors. I should have mentioned that it
was untested. And thanks for adding the shebang. Forgot
that too.
#!/usr/bin/env ruby
require 'rubygems'
require 'commandline'

class App < CommandLine::Application
def initialize
synopsis "[-d] [-s [opt_arg]] [-o req_arg]"
short_description "A sample program"
option :names =3D> %w(--short -s),
:arity =3D> [0,1],
:arg_description =3D> "opt_arg",
:eek:pt_found =3D> get_arg,
:eek:pt_not_found =3D> nil

option :names =3D> %w(--offset -o),
:arg_description =3D> "req_arg",
:eek:pt_found =3D> get_args

option :help
option :names =3D> %w(--myflag -m),
:arity =3D> [0,0],
:arg_description =3D> "flag",
:eek:pt_found =3D> get_arg

A flag option can be written as:
option :flag, :names =3D> %w(--myflag -m),

The get_arg won't do anything since there is no argment for this option.
end

def main
puts "Short arg is=3D>#{opt["--short"]}<" unless opt["--short"].nil?
puts "Offset arg is=3D>#{opt["--offset"]}<"
puts "Myarg arg is=3D>#{opt["--myflag"]}<"
end

end#class App
 
J

Jim Freeze

NOW I think I understand CommandLine::Application. It's like a
GetoptLong that also gives you a usage() subroutine for free, and
if you change your arguments, usage() automatically changes in
sync. Very nice!

Uh huh. An notice that you never actually have to type App.new
or App.run
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top