Class | Slop |
In: |
lib/slop.rb
|
Parent: | Object |
VERSION | = | '2.4.4' | @return [String] The current version string |
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 |
* 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'
* 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
Identical to {Slop.parse}, but removes parsed options from the original Array
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
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
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
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
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
Returns the parsed list into a option/value hash
@example
opts.to_hash #=> { :name => 'Emily' } # strings! opts.to_hash(false) #=> { 'name' => 'Emily' }
@return [Hash]
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