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.5.2'
Instance Attribute Summary collapse
-
#banner(text = nil) ⇒ String
Set or return banner text.
- #commands ⇒ Hash readonly
-
#longest_flag ⇒ Integer
The length of the longest flag slop knows of.
- #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.
-
#command(label, options = {}, &block) ⇒ Slop
Namespace options depending on what command is executed.
-
#each(&block) ⇒ 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.
-
#on_empty(obj = nil, &block) ⇒ Object
(also: #on_empty=)
Add an object to be called when Slop has no values to parse.
-
#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.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/slop.rb', line 68 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 @commands = {} @longest_flag = 0 @invalid_options = [] @banner ||= [:banner] @strict = [:strict] @multiple_switches = [:multiple_switches] @on_empty = [:on_empty] @sloptions = 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.
239 240 241 242 |
# File 'lib/slop.rb', line 239 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.
106 107 108 109 |
# File 'lib/slop.rb', line 106 def (text=nil) @banner = text if text @banner end |
#commands ⇒ Hash (readonly)
48 49 50 |
# File 'lib/slop.rb', line 48 def commands @commands end |
#longest_flag ⇒ Integer
Returns The length of the longest flag slop knows of.
56 57 58 |
# File 'lib/slop.rb', line 56 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.
136 137 138 139 |
# File 'lib/slop.rb', line 136 def [](key) option = @options[key] option ? option.argument_value : @commands[key] end |
#command(label, options = {}, &block) ⇒ Slop
Namespace options depending on what command is executed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/slop.rb', line 189 def command(label, ={}, &block) if @commands[label] raise ArgumentError, "command `#{label}` already exists" end = @sloptions.merge() slop = Slop.new() @commands[label] = slop if block_given? block.arity == 1 ? yield(slop) : slop.instance_eval(&block) end slop end |
#each(&block) ⇒ Object
Enumerable interface
126 127 128 |
# File 'lib/slop.rb', line 126 def each(&block) @options.each(&block) end |
#on_empty(obj = nil, &block) ⇒ Object Also known as: on_empty=
Add an object to be called when Slop has no values to parse
214 215 216 |
# File 'lib/slop.rb', line 214 def on_empty(obj=nil, &block) @on_empty ||= (obj || block) end |
#option(*args, &block) ⇒ Slop::Option Also known as: opt, on
Specify an option with a short or long version, description and type.
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/slop.rb', line 161 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.
114 115 116 |
# File 'lib/slop.rb', line 114 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.
121 122 123 |
# File 'lib/slop.rb', line 121 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.
227 228 229 |
# File 'lib/slop.rb', line 227 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.
253 254 255 256 |
# File 'lib/slop.rb', line 253 def to_s = "#{@banner}\n" if @banner ( || '') + .to_help end |