Type:
Class
Constants:
ORDERINGS : [REQUIRE_ORDER = 0, PERMUTE = 1, RETURN_IN_ORDER = 2]

Orderings.

ARGUMENT_FLAGS : [NO_ARGUMENT = 0, REQUIRED_ARGUMENT = 1, OPTIONAL_ARGUMENT = 2]

Argument flags.

STATUS_TERMINATED : 0, 1, 2

The GetoptLong class allows you to parse command line options similarly to the GNU getopt_long() C library call. Note, however, that GetoptLong is a pure Ruby implementation.

GetoptLong allows for POSIX-style options like --file as well as single letter options like -f

The empty option -- (two minus symbols) is used to end option processing. This can be particularly important if options have optional arguments.

Here is a simple example of usage:

require 'getoptlong'

opts = GetoptLong.new(
  [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
  [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
  [ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
)

dir = nil
name = nil
repetitions = 1
opts.each do |opt, arg|
  case opt
    when '--help'
      puts <<-EOF
hello [OPTION] ... DIR

-h, --help:
   show help

--repeat x, -n x:
   repeat x times

--name [name]:
   greet user by name, if name not supplied default is John

DIR: The directory in which to issue the greeting.
      EOF
    when '--repeat'
      repetitions = arg.to_i
    when '--name'
      if arg == ''
        name = 'John'
      else
        name = arg
      end
  end
end

if ARGV.length != 1
  puts "Missing dir argument (try --help)"
  exit 0
end

dir = ARGV.shift

Dir.chdir(dir)
for i in (1..repetitions)
  print "Hello"
  if name
    print ", #{name}"
  end
  puts
end

Example command line:

hello -n 6 --name -- /tmp
each_option

each_option() Instance Public methods `each_option' is an alias of `each'.

2015-04-11 18:05:06
set_error

set_error(type, message) Instance Protected methods

2015-04-11 18:32:30
set_options

set_options(*arguments) Instance Public methods

2015-04-11 18:21:04
get

get() Instance Public methods Get next option name and its argument, as an Array

2015-04-11 18:10:58
terminated?

terminated?() Instance Public methods Returns true if option processing has

2015-04-11 18:28:59
get_option

get_option() Instance Public methods `get_option' is an alias of `get'.

2015-04-11 18:12:13
new

new(*arguments) Class Public methods

2015-04-11 17:54:22
each

each() Instance Public methods Iterator version of `get'. The

2015-04-11 18:01:30
terminate

terminate() Instance Public methods Explicitly terminate option processing.

2015-04-11 18:22:59
ordering=

ordering=(ordering) Instance Public methods

2015-04-11 18:16:18