Method: Puppet::Util::CommandLine::Trollop.options

Defined in:
lib/puppet/util/command_line/trollop.rb

.options(args = ARGV, *a, &b) ⇒ Object

The easy, syntactic-sugary entry method into Trollop. Creates a Parser, 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 zero or more calls to opt (Parser#opt), zero or more calls to text (Parser#text), and probably a call to version (Parser#version).

The returned block contains a value for every option specified with opt. The value will be the value given on the commandline, or the default value if the option was not specified on the commandline. For every option specified on the commandline, a key “<option name>_given” will also be set in the hash.

Example:

require 'trollop'
opts = Trollop::options do
  opt :monkey, "Use monkey mode"                     # a flag --monkey, defaulting to false
  opt :goat, "Use goat mode", :default => true       # a flag --goat, defaulting to true
  opt :num_limbs, "Number of limbs", :default => 4   # an integer --num-limbs <i>, defaulting to 4
  opt :num_thumbs, "Number of thumbs", :type => :int # an integer --num-thumbs <i>, defaulting to nil
end

## if called with no arguments
p opts # => { :monkey => false, :goat => true, :num_limbs => 4, :num_thumbs => nil }

## if called with --monkey
p opts # => {:monkey_given=>true, :monkey=>true, :goat=>true, :num_limbs=>4, :help=>false, :num_thumbs=>nil}

See more examples at trollop.rubyforge.org.



772
773
774
775
# File 'lib/puppet/util/command_line/trollop.rb', line 772

def options args = ARGV, *a, &b
  @last_parser = Parser.new(*a, &b)
  with_standard_exception_handling(@last_parser) { @last_parser.parse args }
end