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

Methods

[]   banner   command   description   each   execute   get   help   inspect   method_missing   missing   new   on   on_empty   on_empty=   on_noopts   on_optionless   opt   option   optspec   parse   parse   parse!   parse!   present?   respond_to?   summary   to_h   to_hash   to_s   to_struct  

Included Modules

Enumerable

Classes and Modules

Class Slop::Error
Class Slop::InvalidArgumentError
Class Slop::InvalidOptionError
Class Slop::MissingArgumentError
Class Slop::MissingOptionError
Class Slop::Option
Class Slop::Options

Constants

VERSION = '2.4.4'   @return [String] The current version string

Attributes

aliases  [RW]  @return [Array] A list of aliases this command uses
banner  [W]  @overload banner=(string)
  Set the banner
  @param [String] string The text to set the banner to
commands  [R]  @return [Hash]
description  [W]  @overload description=(string)
  Set the description
  @param [String] string The text to set the description to
longest_flag  [RW]  @return [Integer] The length of the longest flag slop knows of
options  [R]  @return [Options]
summary  [W]  @overload summary=(string)
  Set the summary
  @param [String] string The text to set the summary to

Public Class methods

@option opts [Boolean] :help

  * Automatically add the `help` option

@option opts [Boolean] :strict

  * Raises when a non listed option is found, false by default

@option opts [Boolean] :multiple_switches

  * Allows `-abc` to be processed as the options 'a', 'b', 'c' and will
    force their argument values to true. By default Slop with parse this
    as 'a' with the argument 'bc'

@option opts [String] :banner

  * The banner text used for the help

@option opts [Proc, call] :on_empty

  * Any object that respondes to `call` which is executed when Slop has
    no items to parse

@option opts [IO, puts] :io ($stderr)

  * An IO object for writing to when :help => true is used

@option opts [Boolean] :exit_on_help (true)

  * When false and coupled with the :help option, Slop will not exit
    inside of the `help` option

@option opts [Boolean] :ignore_case (false)

  * Ignore options case

@option opts [Proc, call] :on_noopts

  * Trigger an event when no options are found

@option opts [Boolean] :autocreate (false)

  * Autocreate options depending on the Array passed to {#parse}

@option opts [Boolean] :arguments (false)

  * Set to true to enable all specified options to accept arguments
    by default

@option opts [Array] :aliases ([])

  * Primary uses by commands to implement command aliases

@option opts [Boolean] :completion (true)

  * When true, commands will be auto completed. Ie `foobar` will be
    executed simply when `foo` `fo` or `foob` are used

@option options [Boolean] :all_accept_arguments (false)

  * When true, every option added will take an argument, this saves
    having to enable it for every option

Build options from an optspec string

@param [String] optspec The option spec string @param [Array] options A list of options to forward to Slop.new @return [Slop] A new instance of Slop

Parses the items from a CLI format into a friendly object

@param [Array] items Items to parse into options. @example Specifying three options to parse:

 opts = Slops.parse do
   on :v, :verbose, 'Enable verbose mode'
   on :n, :name,    'Your name'
   on :a, :age,     'Your age'
 end

@return [Slop] Returns an instance of Slop

Identical to {Slop.parse}, but removes parsed options from the original Array

@return [Slop] Returns an instance of Slop

Public Instance methods

@param [Symbol] key Option symbol @example

  opts[:name] #=> "Emily"
  opts.get(:name) #=> "Emily"

@return [Object] Returns the value associated with that option. If an

  option doesn't exist, a command will instead be searched for

Set or return banner text

@param [String] text Displayed banner text @example

  opts = Slop.parse do
    banner "Usage - ruby foo.rb [arguments]"
  end

@return [String] The current banner

Namespace options depending on what command is executed

@param [Symbol, String] label @param [Hash] options @example

  opts = Slop.new do
    command :create do
      on :v, :verbose
    end
  end

  # ARGV is `create -v`
  opts.commands[:create].verbose? #=> true

@since 1.5.0 @raise [ArgumentError] When this command already exists @return [Slop] a new instance of Slop namespaced to label

Set or return the description

@param [String] text Displayed description text @example

  opts = Slop.parse do
    description "This command does a lot of stuff with other stuff."
  end

@return [String] The current description

Enumerable interface

Add an execution block (for commands)

@example

  opts = Slop.new do
    command :foo do
      on :v, :verbose

      execute { |o| p o.verbose? }
    end
  end
  opts.parse %w[foo --verbose] #=> true

@param [Array] args The list of arguments to send to this command

  is invoked

@since 1.8.0 @yield [Slop] an instance of Slop for this command

get(key)

Alias for #[]

help()

Alias for to_s

@return [String] This Slop object will options and configuration

  settings revealed

Allows you to check whether an option was specified in the parsed list

Merely sugar for `present?`

@example

  #== ruby foo.rb -v
  opts.verbose? #=> true
  opts.name?    #=> false

@see Slop#present? @return [Boolean] true if this option is present, false otherwise

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

@example

  opts = Slop.new do
    on :n, :name, 'Your name', true
    on :p, :password, 'Your password', true
    on :A, 'Use auth?'
  end

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

@return [Array] A list of options missing from the parsed string @since 2.1.0

on(*args, &block)

Alias for option

Trigger an event when Slop has no values to parse

@param [Object, call] obj The object (which can be anything

  responding to `call`)

@example

  Slop.parse do
    on_empty { puts 'No argument given!' }
  end

@since 1.5.0

on_empty=(obj=nil, &block)

Alias for on_empty

Trigger an event when the arguments contain no options

@param [Object, call] obj The object to be triggered (anything

  responding to `call`)

@example

  Slop.parse do
    on_noopts { puts 'No options here!' }
  end

@since 1.6.0

on_optionless(obj=nil, &block)

Alias for on_noopts

opt(*args, &block)

Alias for option

Specify an option with a short or long version, description and type

@param [*] args Option configuration. @option args [Symbol, String] :short_flag Short option name. @option args [Symbol, String] :long_flag Full option name. @option args [String] :description Option description for use in Slop#help @option args [Boolean] :argument Specifies whether this option requires

  an argument

@option args [Hash] :options Optional option configurations. @example

  opts = Slop.parse do
    on :n, :name, 'Your username', true # Required argument
    on :a, :age,  'Your age (optional)', :optional => true
    on :g, :gender, 'Your gender', :optional => false
    on :V, :verbose, 'Run in verbose mode', :default => true
    on :P, :people, 'Your friends', true, :as => Array
    on :h, :help, 'Print this help screen' do
      puts help
    end
  end

@return [Slop::Option]

Parse a list of options, leaving the original Array unchanged

@param [Array] items A list of items to parse

Parse a list of options, removing parsed options from the original Array

@param [Array] items A list of items to parse

Check if an option is specified in the parsed list

Does the same as Slop#option? but a convenience method for unacceptable method names

@param [Object] The object name(s) to check @since 1.5.0 @return [Boolean] true if these options are present, false otherwise

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

Set or return the summary

@param [String] text Displayed summary text @example

  opts = Slop.parse do
    summary "do stuff with more stuff"
  end

@return [String] The current summary

to_h(symbols=true)

Alias for to_hash

Returns the parsed list into a option/value hash

@example

  opts.to_hash #=> { :name => 'Emily' }

  # strings!
  opts.to_hash(false) #=> { 'name' => 'Emily' }

@return [Hash]

Returns the banner followed by available options listed on the next line

@example

 opts = Slop.parse do
   banner "Usage - ruby foo.rb [arguments]"
   on :v, :verbose, "Enable verbose mode"
 end
 puts opts

@return [String] Help text.

Return parsed items as a new Class

@example

  opts = Slop.new do
    on :n, :name, 'Persons name', true
    on :a, :age, 'Persons age', true, :as => :int
    on :s, :sex, 'Persons sex m/f', true, :match => /^[mf]$/
    on :A, :admin, 'Enable admin mode'
  end

  opts.parse %w[ --name Lee --age 22 -s m --admin ]

  person = opts.to_struct("Person")
  person.class  #=> Struct::Person
  person.name   #=> 'Lee'
  person.age    #=> 22
  person.sex    #=> m
  person.admin  #=> true

@param [String] name The name of this class @return [Class] The new class, or nil if there are no options @since 2.0.0

[Validate]