class Pyer::Options
Options
class
Attributes
Private Class Methods
Create a new instance of Options
and optionally build options via a block.
block - An optional block used to specify options.
# 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
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
.
# File lib/pyer/options.rb, line 39 def self.parse(items = ARGV, &block) new(&block).parse items end
Private Instance Methods
Fetch an options argument value.
key - The Symbol or String option short or long flag.
Returns the Object
value for this option, or nil.
# File lib/pyer/options.rb, line 212 def [](key) key = key.to_s option = options.find { |opt| opt.name == key || opt.short == key } option.value if option end
Examples:
command 'run', 'Running' command :test, 'Testing'
Returns the created instance of Command
. or returns the command given in argument
# 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
Enumerable interface. Yields each Option
.
# File lib/pyer/options.rb, line 219 def each(&block) options.each(&block) end
Returns an Array of Strings representing missing options.
# File lib/pyer/options.rb, line 235 def find_option(name) @triggered_options.find { |opt| opt.name == name } end
Add an flag to options
Examples:
flag :verbose, 'Enable verbose mode' flag 'debug', 'Enable debug mode'
Returns the created instance of Flag
.
# 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
Print a handy Options
help string and exit.
# File lib/pyer/options.rb, line 110 def help helpstr = "Usage: #{File.basename($PROGRAM_NAME)} " helpstr << 'command ' unless commands.empty? helpstr << "[options]\n" helpstr << ' ' + banner unless banner.empty? helpstr << help_commands unless commands.empty? helpstr << help_options helpstr end
# File lib/pyer/options.rb, line 120 def help_commands helpstr = "Commands:\n" @commands.each do |cmd| tab = ' ' * (@longest_cmd + 1 - cmd.name.size) helpstr << ' ' + cmd.name + tab + ': ' + cmd.description + "\n" end helpstr end
# File lib/pyer/options.rb, line 129 def help_options helpstr = "Options:\n" @options.each do |opt| tab = ' ' * (@longest_opt + 1 - opt.name.size) arg = opt.expects_argument ? ' <arg>' : ' ' helpstr << ' -' + opt.short + '|--' + opt.name + arg + tab + ': ' + opt.description + "\n" end helpstr end
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
# 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
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.
# 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
# File lib/pyer/options.rb, line 90 def parse_command(command) cmd = commands.find { |c| c.name == command } fail UnknownCommandError if cmd.nil? @triggered_command = cmd end
# File lib/pyer/options.rb, line 96 def parse_option(option) if option.match(/^--[^-]+$/).nil? && option.match(/^-[^-]$/).nil? fail InvalidOptionError, "invalid #{option} option" end key = option.sub(/\A--?/, '') triggered_option = options.find { |opt| opt.name == key || opt.short == key } fail UnknownOptionError, "unknown #{option} option" if triggered_option.nil? @triggered_options << triggered_option triggered_option end
# File lib/pyer/options.rb, line 139 def show_help puts help exit end
Returns a new Hash with option flags as keys and option values as values.
include_commands - If true, merge options from all sub-commands.
# File lib/pyer/options.rb, line 226 def to_hash Hash[options.map { |opt| [opt.name.to_sym, opt.value] }] end
Add a value to options
Examples:
value 'user', 'Your username' value :pass, 'Your password'
Returns the created instance of Value
.
# 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