Class: Slop
- Inherits:
-
Object
- Object
- Slop
- Includes:
- Enumerable
- Defined in:
- lib/slop.rb,
lib/slop/option.rb,
lib/slop/version.rb
Defined Under Namespace
Classes: InvalidArgumentError, InvalidOptionError, MissingArgumentError, Option, Options
Constant Summary collapse
- VERSION =
'1.2.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 = {}) { ... } ⇒ Slop
Parses the items from a CLI format into a friendly object.
-
.parse!(items = ARGV, options = {}) { ... } ⇒ 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(options = {}, &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) ⇒ Object
(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 = nil) ⇒ 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(options = {}, &block) ⇒ Slop
Returns a new instance of Slop.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/slop.rb', line 48 def initialize(={}, &block) @options = Options.new @banner = nil @longest_flag = 0 @strict = [:strict] @invalid_options = [] 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.
171 172 173 |
# File 'lib/slop.rb', line 171 def method_missing(meth, *args, &block) meth.to_s[/\?$/] ? !!self[meth.to_s.chomp('?')] : super end |
Instance Attribute Details
#banner(text = nil) ⇒ String
Set or return banner text.
76 77 78 79 |
# File 'lib/slop.rb', line 76 def (text=nil) @banner = text if text @banner end |
#longest_flag ⇒ Object
Returns the value of attribute longest_flag.
42 43 44 |
# File 'lib/slop.rb', line 42 def longest_flag @longest_flag end |
Class Method Details
.parse(items = ARGV, options = {}) { ... } ⇒ Slop
Parses the items from a CLI format into a friendly object.
26 27 28 |
# File 'lib/slop.rb', line 26 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.
109 110 111 112 |
# File 'lib/slop.rb', line 109 def [](key) option = @options[key] option ? option.argument_value : nil end |
#each ⇒ Object
Enumerable interface
96 97 98 99 |
# File 'lib/slop.rb', line 96 def each return enum_for(:each) unless block_given? @options.each { |option| yield option } end |
#option(*args, &block) ⇒ Object Also known as: opt, on
Specify an option with a short or long version, description and type.
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/slop.rb', line 133 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.
84 85 86 |
# File 'lib/slop.rb', line 84 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.
91 92 93 |
# File 'lib/slop.rb', line 91 def parse!(items=ARGV, &block) parse_items items, true, &block end |
#to_hash(symbols = nil) ⇒ Hash Also known as: to_h
Returns the parsed list into a option/value hash.
156 157 158 |
# File 'lib/slop.rb', line 156 def to_hash(symbols=nil) @options.to_hash(symbols) end |
#to_s ⇒ String Also known as: help
Returns the banner followed by available options listed on the next line.
186 187 188 189 |
# File 'lib/slop.rb', line 186 def to_s = "#{@banner}\n" if @banner ( || '') + .to_help end |