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

Methods

[]   add_callback   banner   banner=   command   description   description=   each   fetch_command   fetch_option   get   help   missing   new   on   opt   option   optspec   parse   parse   parse!   parse!   present?   respond_to_missing?   run   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.6.0'
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

commands  [R]  The Hash of sub-commands for this Slop instance.
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.

Add a new command.

command - The Symbol or String used to identify this command. options - A Hash of configuration options (see Slop::new)

Returns a new instance of Slop mapped to this command.

Get or set the description (used for commands).

desc - The String to set the description.

Returns the description String.

Set the description (used for commands).

desc - The String to set the description.

Enumerable interface. Yields each Slop::Option.

Fetch a Slop object associated with this command.

command - The String or Symbol name of the command.

Examples:

  opts.command :foo do
    on :v, :verbose, 'Enable verbose mode'
  end

  # ruby run.rb foo -v
  opts.fetch_command(:foo).verbose? #=> true

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

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.

Specify code to be executed when these options are parsed.

callable - An object responding to a call method.

yields - The instance of Slop parsing these options

         An Array of unparsed arguments

Example:

  Slop.parse do
    on :v, :verbose

    run do |opts, args|
      puts "Arguments: #{args.inspect}" if opts.verbose?
    end
  end

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(include_commands = false)

Alias for to_hash

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

include_commands - If true, merge options from all sub-commands.

Print a handy Slop help string.

Returns the banner followed by available option help strings.

[Validate]