In Files

Methods

Trollop

lib/trollop.rb -- trollop command-line processing library

Author

William Morgan (mailto: wmorgan-trollop@masanjin.net)

Copyright

Copyright 2007 William Morgan

License

GNU GPL version 2

Constants

FLOAT_RE

Regex for floating point numbers

PARAM_RE

Regex for parameters

VERSION

Public Class Methods

die(arg, msg=nil) click to toggle source

Informs the user that their usage of ‘arg’ was wrong, as detailed by ‘msg’, and dies. Example:

options do
  opt :volume, :default => 0.0
end

die :volume, "too loud" if opts[:volume] > 10.0
die :volume, "too soft" if opts[:volume] < 0.1

In the one-argument case, simply print that message, a notice about -h, and die. Example:

options do
  opt :whatever # ...
end

Trollop::die "need at least one filename" if ARGV.empty?
# File lib/trollop.rb, line 602
def die arg, msg=nil
  if msg
    $stderr.puts "Error: argument --#{@p.specs[arg][:long]} #{msg}."
  else
    $stderr.puts "Error: #{arg}."
  end
  $stderr.puts "Try --help for help."
  exit(-1)
end
options(args = ARGV, *a, &b) click to toggle source

The top-level entry method into Trollop. Creates a Parser object, passes the block to it, then parses args with it, handling any errors or requests for help or version information appropriately (and then exiting). Modifies args in place. Returns a hash of option values.

The block passed in should contain one or more calls to opt (Parser#opt), one or more calls to text (Parser#text), and probably a call to version (Parser#version).

See the synopsis in README.txt for examples.

# File lib/trollop.rb, line 564
def options args = ARGV, *a, &b
  @p = Parser.new(*a, &b)
  begin
    vals = @p.parse args
    args.clear
    @p.leftovers.each { |l| args << l }
    vals
  rescue CommandlineError => e
    $stderr.puts "Error: #{e.message}."
    $stderr.puts "Try --help for help."
    exit(-1)
  rescue HelpNeeded
    @p.educate
    exit
  rescue VersionNeeded
    puts @p.version
    exit
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.