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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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:

1
hello -n 6 --name -- /tmp
set_options
  • References/Ruby on Rails/Ruby/Classes/GetoptLong

set_options(*arguments) Instance Public methods

2025-01-10 15:47:30
set_error
  • References/Ruby on Rails/Ruby/Classes/GetoptLong

set_error(type, message) Instance Protected methods

2025-01-10 15:47:30
each_option
  • References/Ruby on Rails/Ruby/Classes/GetoptLong

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

2025-01-10 15:47:30
terminated?
  • References/Ruby on Rails/Ruby/Classes/GetoptLong

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

2025-01-10 15:47:30
get
  • References/Ruby on Rails/Ruby/Classes/GetoptLong

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

2025-01-10 15:47:30
get_option
  • References/Ruby on Rails/Ruby/Classes/GetoptLong

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

2025-01-10 15:47:30
each
  • References/Ruby on Rails/Ruby/Classes/GetoptLong

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

2025-01-10 15:47:30
ordering=
  • References/Ruby on Rails/Ruby/Classes/GetoptLong

ordering=(ordering) Instance Public methods

2025-01-10 15:47:30
terminate
  • References/Ruby on Rails/Ruby/Classes/GetoptLong

terminate() Instance Public methods Explicitly terminate option processing.

2025-01-10 15:47:30
error_message
  • References/Ruby on Rails/Ruby/Classes/GetoptLong

error_message() Instance Public methods Return the appropriate error message

2025-01-10 15:47:30