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