Name

GetoptLong — Command line option parser

Synopsis

The GetoptLong class parses command-line option arguments in a way similar to GNU getoptlong library.

Required Library

require ‘gettextfile’

Example

require 'getoptlong'

 opt = GetoptLong.new(
     ['--max-size', '-m', GetoptLong::REQUIRED_ARGUMENT],
     ['--quiet',    '-q', GetoptLong::NO_ARGUMENT],
     ['--help',           GetoptLong::NO_ARGUMENT],
     ['--version',        GetoptLong::NO_ARGUMENT])
 opt.each_option do |name,arg|
    case name
    when '--max-size'
  printf "max-size is %d\n", arg
    when '--quiet'
  print "be quiet!\n"
    when '--help'
  print "help message here\n"
  exit
    when '--version'
  print "version 0.1\n"
  exit
    end
  end

Inherited Class

Object

Class Method

GetoptLong::new(option...)

Creates and returns a GetoptLong object. If options are given, they are passed to the set_options method.

Instance Methods

opt.each {|optname, optarg|...}
opt.each_option {|optname, optarg|...}

Iterates over each command-line option. Option name and value are passed to the block.

opt.get
opt.get_option

Retrieves an option from command-line arguments, and returns the name-value pair of option.

opt.error
opt.error?

Returns type of the current error or nil if no error occurs.

opt.error_message

Returns an error message of the current error or nil if no error occurs.

opt.ordering=ordering

Sets option ordering. ordering is any of PERMUTE, REQUIRE_ORDER, or RETURN_IN_ORDER.

opt.ordering

Returns current ordering.

opt.quiet=bool

Sets status of quiet mode. In quiet mode, option parser doesn’t output error messages to ...

Get Ruby in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.