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.8.0'
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
(also: #get)
Returns the value associated with that option.
-
#command(label, options = {}, &block) ⇒ Slop
Namespace options depending on what command is executed.
-
#each(&block) ⇒ Object
Enumerable interface.
-
#execute(obj = nil, &block) ⇒ Object
Add an execution block (for commands).
-
#initialize(*opts, &block) ⇒ Slop
constructor
A new instance of Slop.
- #inspect ⇒ Object
-
#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=)
Trigger an event when Slop has no values to parse.
-
#on_noopts(obj = nil, &block) ⇒ Object
(also: #on_optionless)
Trigger an event when the arguments contain no options.
-
#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.
-
#present?(option_name) ⇒ Boolean
Check if an option is specified in the parsed list.
-
#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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/slop.rb', line 94 def initialize(*opts, &block) = opts.last.is_a?(Hash) ? opts.pop : {} [:banner] = opts.shift if opts[0].respond_to? :to_str opts.each { |o| [o] = true } @options = Options.new @commands = {} @execution_block = nil @longest_flag = 0 @invalid_options = [] @banner = [:banner] @strict = [:strict] @ignore_case = [:ignore_case] @multiple_switches = [:multiple_switches] @autocreate = [:autocreate] @arguments = [:arguments] @on_empty = [:on_empty] @on_noopts = [:on_noopts] || [:on_optionless] @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 ([:io] || $stderr).puts help exit unless [:exit_on_help] == false 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
Merely sugar for present?
311 312 313 314 |
# File 'lib/slop.rb', line 311 def method_missing(meth, *args, &block) super unless meth.to_s[-1, 1] == '?' present? meth.to_s.chomp '?' end |
Instance Attribute Details
#banner(text = nil) ⇒ String
Set or return banner text
136 137 138 139 |
# File 'lib/slop.rb', line 136 def (text=nil) @banner = text if text @banner end |
#commands ⇒ Hash (readonly)
47 48 49 |
# File 'lib/slop.rb', line 47 def commands @commands end |
#longest_flag ⇒ Integer
Returns The length of the longest flag slop knows of.
55 56 57 |
# File 'lib/slop.rb', line 55 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
31 32 33 |
# File 'lib/slop.rb', line 31 def self.parse(items=ARGV, ={}, &block) initialize_and_parse items, false, , &block end |
Instance Method Details
#[](key) ⇒ Object Also known as: get
Returns the value associated with that option. If an option doesn't exist, a command will instead be searched for
166 167 168 169 |
# File 'lib/slop.rb', line 166 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
221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/slop.rb', line 221 def command(label, ={}, &block) if @commands[label] raise ArgumentError, "command `#{label}` already exists" end slop = Slop.new @sloptions.merge @commands[label] = slop if block_given? block.arity == 1 ? yield(slop) : slop.instance_eval(&block) end slop end |
#each(&block) ⇒ Object
Enumerable interface
156 157 158 |
# File 'lib/slop.rb', line 156 def each(&block) @options.each(&block) end |
#execute(obj = nil, &block) ⇒ Object
Add an execution block (for commands)
280 281 282 283 284 285 286 |
# File 'lib/slop.rb', line 280 def execute(obj=nil, &block) if obj || block_given? @execution_block = obj || block elsif @execution_block.respond_to?(:call) @execution_block.call(self) end end |
#inspect ⇒ Object
343 344 345 346 |
# File 'lib/slop.rb', line 343 def inspect "#<Slop config_options=#{@sloptions.inspect}\n " + .map(&:inspect).join("\n ") + "\n>" end |
#on_empty(obj = nil, &block) ⇒ Object Also known as: on_empty=
Trigger an event when Slop has no values to parse
245 246 247 |
# File 'lib/slop.rb', line 245 def on_empty(obj=nil, &block) @on_empty ||= (obj || block) end |
#on_noopts(obj = nil, &block) ⇒ Object Also known as: on_optionless
Trigger an event when the arguments contain no options
259 260 261 |
# File 'lib/slop.rb', line 259 def on_noopts(obj=nil, &block) @on_noopts ||= (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
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/slop.rb', line 193 def option(*args, &block) = args.last.is_a?(Hash) ? args.pop : {} short, long, desc, arg, extras = args .merge!(extras) 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
144 145 146 |
# File 'lib/slop.rb', line 144 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
151 152 153 |
# File 'lib/slop.rb', line 151 def parse!(items=ARGV, &block) parse_items items, true, &block end |
#present?(option_name) ⇒ Boolean
Check if an option is specified in the parsed list
Does the same as Slop#option? but a convenience method for unacceptable method names
324 325 326 |
# File 'lib/slop.rb', line 324 def present?(option_name) !!get(option_name) end |
#to_hash(symbols = false) ⇒ Hash Also known as: to_h
Returns the parsed list into a option/value hash
296 297 298 |
# File 'lib/slop.rb', line 296 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
337 338 339 340 |
# File 'lib/slop.rb', line 337 def to_s = "#{@banner}\n" if @banner ( || '') + .to_help end |