Class: Pyer::Options
Overview
Options class
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.parse(items = ARGV, &block) ⇒ Object
items - The Array of items to extract options from (default: ARGV).
Instance Method Summary collapse
-
#[](key) ⇒ Object
Fetch an options argument value.
-
#banner(desc = nil) ⇒ Object
Banner.
-
#command(name = nil, desc = nil, &block) ⇒ Object
(also: #cmd)
Command.
-
#each(&block) ⇒ Object
Enumerable interface.
-
#flag(name, desc, &block) ⇒ Object
Add an flag to options.
-
#help ⇒ Object
Print a handy Options help string and exit.
-
#initialize(&block) ⇒ Options
constructor
Create a new instance of Options and optionally build options via a block.
-
#parse(items = ARGV) ⇒ Object
Parse a list of items, executing and gathering options along the way.
-
#to_hash ⇒ Object
(also: #to_h)
Returns a new Hash with option flags as keys and option values as values.
-
#value(name, desc, &block) ⇒ Object
Add a value to options.
Constructor Details
#initialize(&block) ⇒ Options
Create a new instance of Options and optionally build options via a block.
block - An optional block used to specify options.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pyer/options.rb', line 49 def initialize(&block) @banner = '' @commands = [] @triggered_command = nil @options = [] @triggered_options = [] @longest_cmd = 0 @longest_opt = 0 instance_eval(&block) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object (private)
Returns true if this option is present. If this method does not end with a ? character it will instead return the value of the option or nil
Examples:
opts.parse %(--verbose)
opts.verbose? #=> true
opts.other? #=> false
248 249 250 251 252 253 254 255 256 257 |
# File 'lib/pyer/options.rb', line 248 def method_missing(method) meth = method.to_s if meth.end_with?('?') !find_option(meth.chop!).nil? else o = find_option(meth) o.callback.call if !o.nil? && o.callback.respond_to?(:call) o.nil? ? nil : o.value end end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
43 44 45 |
# File 'lib/pyer/options.rb', line 43 def commands @commands end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
44 45 46 |
# File 'lib/pyer/options.rb', line 44 def @options end |
Class Method Details
.parse(items = ARGV, &block) ⇒ Object
items - The Array of items to extract options from (default: ARGV). block - An optional block used to add options.
Examples:
Options.parse(ARGV) do
value 'name', 'Your username'
flag 'verbose', 'Enable verbose mode'
end
short option is the first letter of long option Returns a new instance of Options.
39 40 41 |
# File 'lib/pyer/options.rb', line 39 def self.parse(items = ARGV, &block) new(&block).parse items end |
Instance Method Details
#[](key) ⇒ Object
Fetch an options argument value.
key - The Symbol or String option short or long flag.
Returns the Object value for this option, or nil.
212 213 214 215 216 |
# File 'lib/pyer/options.rb', line 212 def [](key) key = key.to_s option = .find { |opt| opt.name == key || opt.short == key } option.value if option end |
#banner(desc = nil) ⇒ Object
Banner
Example:
'This is the banner'
151 152 153 154 |
# File 'lib/pyer/options.rb', line 151 def (desc = nil) @banner += desc + "\n" unless desc.nil? @banner end |
#command(name = nil, desc = nil, &block) ⇒ Object Also known as: cmd
Command
Examples:
command 'run', 'Running'
command :test, 'Testing'
Returns the created instance of Command. or returns the command given in argument
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/pyer/options.rb', line 165 def command(name = nil, desc = nil, &block) if name.nil? @triggered_command.callback.call if !@triggered_command.nil? && @triggered_command.callback.respond_to?(:call) @triggered_command.nil? ? nil : @triggered_command.name else @longest_cmd = name.size if name.size > @longest_cmd cmd = Command.new(name, desc, &block) @commands << cmd end end |
#each(&block) ⇒ Object
Enumerable interface. Yields each Option.
219 220 221 |
# File 'lib/pyer/options.rb', line 219 def each(&block) .each(&block) end |
#flag(name, desc, &block) ⇒ Object
Add an flag to options
Examples:
flag :verbose, 'Enable verbose mode'
flag 'debug', 'Enable debug mode'
Returns the created instance of Flag.
200 201 202 203 204 205 |
# File 'lib/pyer/options.rb', line 200 def flag(name, desc, &block) @longest_opt = name.size if name.size > @longest_opt option = Flag.new(name, desc, &block) @options << option option end |
#help ⇒ Object
Print a handy Options help string and exit.
110 111 112 113 114 115 116 117 118 |
# File 'lib/pyer/options.rb', line 110 def help helpstr = "Usage: #{File.basename($PROGRAM_NAME)} " helpstr << 'command ' unless commands.empty? helpstr << "[options]\n" helpstr << ' ' + unless .empty? helpstr << help_commands unless commands.empty? helpstr << helpstr end |
#parse(items = ARGV) ⇒ Object
Parse a list of items, executing and gathering options along the way.
items - The Array of items to extract options from (default: ARGV). block - An optional block which when used will yield non options.
Returns an Array of original items with options removed.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/pyer/options.rb', line 66 def parse(items = ARGV) item = items.shift # need some help ? show_help if item == '?' || item == '-h' || item == '--help' || item == 'help' || item.nil? # parsing command unless commands.empty? parse_command(item) item = items.shift end # parsing options until item.nil? option = parse_option(item) if option.expects_argument option.value = items.shift fail MissingArgumentError, "missing #{item} argument" if option.value.nil? || option.value.start_with?('-') else option.value = true end item = items.shift end # return the Options instance self end |
#to_hash ⇒ Object Also known as: to_h
Returns a new Hash with option flags as keys and option values as values.
include_commands - If true, merge options from all sub-commands.
226 227 228 |
# File 'lib/pyer/options.rb', line 226 def to_hash Hash[.map { |opt| [opt.name.to_sym, opt.value] }] end |
#value(name, desc, &block) ⇒ Object
Add a value to options
Examples:
value 'user', 'Your username'
value :pass, 'Your password'
Returns the created instance of Value.
185 186 187 188 189 190 |
# File 'lib/pyer/options.rb', line 185 def value(name, desc, &block) @longest_opt = name.size if name.size > @longest_opt option = Value.new(name, desc, &block) @options << option option end |