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.6.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.
-
#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.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/slop.rb', line 75 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] @ignore_case = [:ignore_case] @multiple_switches = [:multiple_switches] @on_empty = [:on_empty] @on_noopts = [:on_noopts] || [:on_optionless] @sloptions = io = [:io] || $stderr eoh = true if [:exit_on_help].nil? 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.puts help exit if eoh 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.
267 268 269 270 |
# File 'lib/slop.rb', line 267 def method_missing(meth, *args, &block) super unless meth.to_s =~ /\?\z/ present? meth.to_s.chomp '?' end |
Instance Attribute Details
#banner(text = nil) ⇒ String
Set or return banner text.
118 119 120 121 |
# File 'lib/slop.rb', line 118 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 Also known as: get
Returns the value associated with that option. If an option doesn't exist, a command will instead be searched for
148 149 150 151 |
# File 'lib/slop.rb', line 148 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
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/slop.rb', line 203 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
138 139 140 |
# File 'lib/slop.rb', line 138 def each(&block) @options.each(&block) end |
#inspect ⇒ Object
297 298 299 300 |
# File 'lib/slop.rb', line 297 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
228 229 230 |
# File 'lib/slop.rb', line 228 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
242 243 244 |
# File 'lib/slop.rb', line 242 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.
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/slop.rb', line 175 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.
126 127 128 |
# File 'lib/slop.rb', line 126 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.
133 134 135 |
# File 'lib/slop.rb', line 133 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.
278 279 280 |
# File 'lib/slop.rb', line 278 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.
255 256 257 |
# File 'lib/slop.rb', line 255 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.
291 292 293 294 |
# File 'lib/slop.rb', line 291 def to_s = "#{@banner}\n" if @banner ( || '') + .to_help end |