Command Line Argument Processing

What is everyone using (other than manually) to process command line
arguments? I have looked at optparse (OptionParser) but it is not well
documented. Is there something better? Or is there better
documentation for optparse out there?

On 1/9/07, [email protected] [email protected] wrote:

What is everyone using (other than manually) to process command line arguments? I have looked at optparse (OptionParser) but it is not well documented. Is there something better? Or is there better documentation for optparse out there?

Do you have Programming Ruby?

I found the documentation on p711-712 to be sufficient.

An example can be found at:
http://stdlib.rubyonrails.org/libdoc/optparse/rdoc/index.html

On 1/9/07, [email protected] [email protected] wrote:

What is everyone using (other than manually) to process command line arguments? I have looked at optparse (OptionParser) but it is not well documented. Is there something better? Or is there better documentation for optparse out there?

I use optparse and it fits my needs pretty well. I’ve started with the
example found here
http://www.ruby-doc.org/core/
and I could do every thing I want. One example is often better than a
thousand of documentation lines.

I hope this help you.

Cheers,

[email protected] wrote:

What is everyone using (other than manually) to process command line
arguments? I have looked at optparse (OptionParser) but it is not
well documented. Is there something better? Or is there better
documentation for optparse out there?

OptionParser is very good, and generates reasonably nice on-line help.

You might want to look at http://rubyforge.org/projects/optionparser/ as
well (I haven’t used it, but some people seem to like it).

If you want to use something very simple that you can paste into a
script and hack up as you see fit, there is argos:

http://redshift.sourceforge.net/argos.rb

Joel VanderWerf wrote:

If you want to use something very simple that you can paste into a
script and hack up as you see fit, there is argos:

http://redshift.sourceforge.net/argos.rb

Does anyone use getoptlong? Seems powerful and simple ATST.

http://www.ruby-doc.org/stdlib/libdoc/getoptlong/rdoc/classes/GetoptLong.html

cheers,

paulv

[email protected] wrote:

What is everyone using (other than manually) to process command line arguments?
I have looked at optparse (OptionParser) but it is not well documented. Is there
something better? Or is there better documentation for optparse out there?

Like you, I was confused by the documentation for optparse and
getoptlong. So, for a while, I was using the Usage gem:
http://rubyforge.org/projects/usage/

You have to install the gem and then look at its documentation to
figure out how to use it, but I found it reasonably easy. Eventually,
though, I got tired of some if its limitations and started writing my
own template code (sample follows). It’s certainly a manual process,
but I liked feeling the wind in my hair.

USAGE = <<ENDUSAGE
Usage:
findfile [-d max_depth] [-a] [-c] [-i] name_regexp [content_regexp]
-d,–depth the maximum depth to recurse to (defaults to no
limit)
-a,–showall with content_regexp, show every match per file
(defaults to only show the first-match per file)
-c,–usecase with content_regexp, use case-sensitive matching
(defaults to case-insensitive)
-i,–includedirs also find directories matching name_regexp
(defaults to files only; incompatible with
content_regexp)
-h,–help show some help examples
ENDUSAGE

EXAMPLES = <<ENDEXAMPLES

Examples:
findfile foo

Print the path to all files with ‘foo’ in the name

findfile -i foo

Print the path to all files and directories with ‘foo’ in the name

findfile js$

Print the path to all files whose name ends in “js”

findfile js$ vector

Print the path to all files ending in “js” with “Vector” or

“vector”

(or “vEcTOr”, “VECTOR”, etc.) in the contents, and print some of

the

first line that has that content.

findfile js$ -c Vector

Like above, but must match exactly “Vector” (not ‘vector’ or

‘VECTOR’).

findfile . vector -a

Print the path to every file with “Vector” (any case) in it

somewhere

printing every line in those files (with line numbers) with that

content.

findfile -d 0 .

Print the path to every file that is in the current directory.

findfile -d 1 .

Print the path to every file that is in the current directory or

any

of its child directories (but no subdirectories of the children).

ENDEXAMPLES

ARGS = {}
UNFLAGGED_ARGS = [ :name_regexp, :content_regexp ]
next_arg = UNFLAGGED_ARGS.first
ARGV.each{ |arg|
case arg
when ‘-d’,‘–depth’
next_arg = :max_depth
when ‘-a’,‘–showall’
ARGS[:showall] = true
when ‘-c’,‘–usecase’
ARGS[:usecase] = true
when ‘-i’,‘–includedirs’
ARGS[:includedirs] = true
when ‘-h’,‘–help’
ARGS[:help] = true
else
if next_arg
if next_arg==:max_depth
arg = arg.to_i + 1
end
ARGS[next_arg] = arg
UNFLAGGED_ARGS.delete( next_arg )
end
next_arg = UNFLAGGED_ARGS.first
end
}

if ARGS[:help] or !ARGS[:name_regexp]
puts USAGE
puts EXAMPLES if ARGS[:help]
exit
end

[email protected] wrote:

What is everyone using (other than manually) to process command line arguments? I have looked at optparse (OptionParser) but it is not well documented. Is there something better? Or is there better documentation for optparse out there?

Fed up with some quirks of getoptlong, and confused by optparse, I
wrote (and use) my own library called “getopt”. It includes two
classes - Getopt::Std and Getopt::Long. The former is nearly identical
to the Perl equivalent, while the latter is more or less a refactored
version of getoptlong.

http://raa.ruby-lang.org/project/getopt/

Regards,

Dan

[email protected] ha escrito:

What is everyone using (other than manually) to process command line arguments? I have looked at
optparse (OptionParser) but it is not well documented. Is there something better? Or is there better
documentation for optparse out there?

Getopt::Declare, for everything, since my good old Perl days, as
there’s still nothing better.

Example:

#!/usr/bin/env ruby

require ‘Getopt/Declare’

args = Getopt::Declare.new(<<‘EOF’)
-q[uiet] don’t print status messages to stdout
but multi-line. This allows something.
-f[ile] <file:of> write report to file
EOF

p args

The example above makes it support -q, -quiet and -f, -file as output

file.

-h[elp] and -v[ersion] come for free, too

How to get it:

http://rubyforge.org/projects/getoptdeclare/

or:

gem install getopt-declare

Docs are not online (my fault, as they were long and rubyforge was
still in its infancy at the time).
Usage is almost identical to its Perl’s cousin, so you can look at the
docs there.
The gem DOES ship with an rdoc file called Declare.rdoc, which
unfortunately rubygem’s system still cannot ‘ri’ automatically, so you
need to ‘ri’ it manually.
Getopt-Declare uses a very complex parser behind the scenes, where you
just type in the help line description (all that getopt-declare needs).
Don’t be deceived by its simplicity (it has more features than any
other command-line parser). It comes with a bunch of much more complex
demos in the samples directory of the gem distro.

The one gotcha, you just need a tab between your argument and your
description (at least until my next release, where I finally remove
that limitation and allow spaces, too)

PS. I cannot take credit for Getopt/Declare, as it was something
Damian Conway came up many moons ago. I just ported it to ruby and
fixed some bugs here and there.