Class Slop
In: lib/slop.rb
lib/slop/commands.rb
lib/slop/option.rb
Parent: Object

Methods

[]   add_callback   banner   banner=   each   fetch_option   get   help   inspect   method_missing   missing   new   on   opt   option   optspec   parse   parse   parse!   parse!   present?   respond_to?   separator   strict?   to_h   to_hash   to_s  

Included Modules

Enumerable

Classes and Modules

Class Slop::Commands
Class Slop::Error
Class Slop::InvalidArgumentError
Class Slop::InvalidCommandError
Class Slop::InvalidOptionError
Class Slop::MissingArgumentError
Class Slop::MissingOptionError
Class Slop::Option

Constants

VERSION = '3.3.2'
DEFAULT_OPTIONS = { :strict => false, :help => false, :banner => nil, :ignore_case => false, :autocreate => false, :arguments => false, :optional_arguments => false, :multiple_switches => true, :longest_flag => 0   Returns a default Hash of configuration options this Slop instance uses.

Attributes

config  [R]  The Hash of configuration options for this Slop instance.
options  [R]  The Array of Slop::Option objects tied to this Slop instance.

Public Class methods

Create a new instance of Slop and optionally build options via a block.

config - A Hash of configuration options. block - An optional block used to specify options.

Build a Slop object from a option specification.

This allows you to design your options via a simple String rather than programatically. Do note though that with this method, you‘re unable to pass any advanced options to the on() method when creating options.

string - The optspec String config - A Hash of configuration options to pass to Slop.new

Examples:

  opts = Slop.optspec(<<-SPEC)
  ruby foo.rb [options]
  ---
  n,name=     Your name
  a,age=      Your age
  A,auth      Sign in with auth
  p,passcode= Your secret pass code
  SPEC

  opts.fetch_option(:name).description #=> "Your name"

Returns a new instance of Slop.

items - The Array of items to extract options from (default: ARGV). config - The Hash of configuration options to send to Slop.new(). block - An optional block used to add options.

Examples:

  Slop.parse(ARGV, :help => true) do
    on '-n', '--name', 'Your username', :argument => true
  end

Returns a new instance of Slop.

items - The Array of items to extract options from (default: ARGV). config - The Hash of configuration options to send to Slop.new(). block - An optional block used to add options.

Returns a new instance of Slop.

Public Instance methods

Fetch an options argument value.

key - The Symbol or String option short or long flag.

Returns the Object value for this option, or nil.

Add a callback.

label - The Symbol identifier to attach this callback.

Returns nothing.

Get or set the banner.

banner - The String to set the banner.

Returns the banner String.

Set the banner.

banner - The String to set the banner.

Returns nothing.

Enumerable interface. Yields each Slop::Option.

Fetch a Slop::Option object.

key - The Symbol or String option key.

Examples:

  opts.on(:foo, 'Something fooey', :argument => :optional)
  opt = opts.fetch_option(:foo)
  opt.class #=> Slop::Option
  opt.accepts_optional_argument? #=> true

Returns an Option or nil if none were found.

get(key)

Alias for #[]

help()

Alias for to_s

Returns the String inspection text.

Convenience method for present?(:option).

Examples:

  opts.parse %( --verbose )
  opts.verbose? #=> true
  opts.other?   #=> false

Returns true if this option is present. If this method does not end with a ? character it will instead call super().

Fetch a list of options which were missing from the parsed list.

Examples:

  opts = Slop.new do
    on :n, :name=
    on :p, :password=
  end

  opts.parse %w[ --name Lee ]
  opts.missing #=> ['password']

Returns an Array of Strings representing missing options.

Add an Option.

objects - An Array with an optional Hash as the last element.

Examples:

  on '-u', '--username=', 'Your username'
  on :v, :verbose, 'Enable verbose mode'

Returns the created instance of Slop::Option.

opt(*objects, &block)

Alias for on

option(*objects, &block)

Alias for on

Parse a list of items, executing and gathering options along the way.

items - The Array of items to extract options from (default: ARGV). block - An optional block which when used will yield non options.

Returns an Array of original items.

Parse a list of items, executing and gathering options along the way. unlike parse() this method will remove any options and option arguments from the original Array.

items - The Array of items to extract options from (default: ARGV). block - An optional block which when used will yield non options.

Returns an Array of original items with options removed.

Check for an options presence.

Examples:

  opts.parse %w( --foo )
  opts.present?(:foo) #=> true
  opts.present?(:bar) #=> false

Returns true if all of the keys are present in the parsed arguments.

Override this method so we can check if an option? method exists.

Returns true if this option key exists in our list of options.

Add string separators between options.

text - The String text to print.

Is strict mode enabled?

Returns true if strict mode is enabled, false otherwise.

to_h()

Alias for to_hash

Returns a new Hash with option flags as keys and option values as values.

Print a handy Slop help string.

Returns the banner followed by available option help strings.

[Validate]