Class: Slop
- Inherits:
-
Object
- Object
- Slop
- Includes:
- Enumerable
- Defined in:
- lib/slop.rb,
lib/slop/option.rb,
lib/slop/options.rb
Defined Under Namespace
Classes: InvalidArgumentError, InvalidOptionError, MissingArgumentError, Option, Options
Constant Summary collapse
- VERSION =
Returns The current version string.
'1.4.1'
Instance Attribute Summary collapse
-
#banner(text = nil) ⇒ String
Set or return banner text.
-
#longest_flag ⇒ Object
Returns the value of attribute longest_flag.
- #options ⇒ Options readonly
Class Method Summary collapse
-
.parse(items = ARGV, options = {}, &block) ⇒ Slop
Parses the items from a CLI format into a friendly object.
-
.parse!(items = ARGV, options = {}, &block) ⇒ Slop
Identical to Slop.parse, but removes parsed options from the original Array.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return the value of an option via the subscript operator.
-
#each ⇒ Object
Enumerable interface.
-
#initialize(*opts, &block) ⇒ Slop
constructor
A new instance of Slop.
-
#method_missing(meth, *args, &block) ⇒ Boolean
Allows you to check whether an option was specified in the parsed list.
-
#option(*args, &block) ⇒ Slop::Option
(also: #opt, #on)
Specify an option with a short or long version, description and type.
-
#parse(items = ARGV, &block) ⇒ Object
Parse a list of options, leaving the original Array unchanged.
-
#parse!(items = ARGV, &block) ⇒ Object
Parse a list of options, removing parsed options from the original Array.
-
#to_hash(symbols = false) ⇒ Hash
(also: #to_h)
Returns the parsed list into a option/value hash.
-
#to_s ⇒ String
(also: #help)
Returns the banner followed by available options listed on the next line.
Constructor Details
#initialize(*opts, &block) ⇒ Slop
Returns a new instance of Slop.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/slop.rb', line 57 def initialize(*opts, &block) = {} .merge! opts.pop if opts.last.is_a? Hash @banner = opts.shift if opts[0].respond_to?(:to_str) opts.each { |o| [o] = true } @options = Options.new @longest_flag = 0 @strict = [:strict] @invalid_options = [] @multiple_switches = [:multiple_switches] if block_given? block.arity == 1 ? yield(self) : instance_eval(&block) end if [:help] on :h, :help, 'Print this help message', :tail => true do puts help exit end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Boolean
Allows you to check whether an option was specified in the parsed list.
178 179 180 181 |
# File 'lib/slop.rb', line 178 def method_missing(meth, *args, &block) super unless meth.to_s =~ /\?\z/ !!self[meth.to_s.chomp '?'] end |
Instance Attribute Details
#banner(text = nil) ⇒ String
Set or return banner text.
89 90 91 92 |
# File 'lib/slop.rb', line 89 def (text=nil) @banner = text if text @banner end |
#longest_flag ⇒ Object
Returns the value of attribute longest_flag.
48 49 50 |
# File 'lib/slop.rb', line 48 def longest_flag @longest_flag end |
Class Method Details
.parse(items = ARGV, options = {}, &block) ⇒ Slop
Parses the items from a CLI format into a friendly object.
33 34 35 |
# File 'lib/slop.rb', line 33 def self.parse(items=ARGV, ={}, &block) initialize_and_parse(items, false, , &block) end |
Instance Method Details
#[](key) ⇒ Object
Return the value of an option via the subscript operator.
120 121 122 123 |
# File 'lib/slop.rb', line 120 def [](key) option = @options[key] option.argument_value if option end |
#each ⇒ Object
Enumerable interface
109 110 111 112 |
# File 'lib/slop.rb', line 109 def each return enum_for(:each) unless block_given? @options.each { |option| yield option } end |
#option(*args, &block) ⇒ Slop::Option Also known as: opt, on
Specify an option with a short or long version, description and type.
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/slop.rb', line 145 def option(*args, &block) = args.pop if args.last.is_a?(Hash) ||= {} short, long, desc, arg = (args) option = Option.new(self, short, long, desc, arg, , &block) @options << option option end |
#parse(items = ARGV, &block) ⇒ Object
Parse a list of options, leaving the original Array unchanged.
97 98 99 |
# File 'lib/slop.rb', line 97 def parse(items=ARGV, &block) parse_items items, &block end |
#parse!(items = ARGV, &block) ⇒ Object
Parse a list of options, removing parsed options from the original Array.
104 105 106 |
# File 'lib/slop.rb', line 104 def parse!(items=ARGV, &block) parse_items items, true, &block end |
#to_hash(symbols = false) ⇒ Hash Also known as: to_h
Returns the parsed list into a option/value hash.
166 167 168 |
# File 'lib/slop.rb', line 166 def to_hash(symbols=false) @options.to_hash(symbols) end |
#to_s ⇒ String Also known as: help
Returns the banner followed by available options listed on the next line.
192 193 194 195 |
# File 'lib/slop.rb', line 192 def to_s = "#{@banner}\n" if @banner ( || '') + .to_help end |