Module: Rubikon::HasArguments
Overview
This module is included in all classes used for parsing command-line arguments
Constant Summary collapse
- ARGUMENT_MATCHERS =
Provides a number of predefined regular expressions to check arguments against
{ # Allow only alphanumeric characters :alnum => /[[:alnum:]]+/, # Allow only floating point numbers as arguments :float => /-?[0-9]+(?:\.[0-9]+)?/, # Allow only alphabetic characters :letters => /[a-zA-Z]+/, # Allow only numeric arguments :numeric => /-?[0-9]+/ }
Instance Attribute Summary
Attributes included from Parameter
Instance Method Summary collapse
-
#[](arg) ⇒ Object
Access the arguments of this parameter using a numeric or symbolic index.
-
#args ⇒ Array<String>, Hash<Symbol, String>
(also: #arguments)
Returns the arguments given to this parameter.
-
#initialize(app, name, *options, &block) ⇒ Object
Creates a new parameter with arguments with the given name and an optional code block.
Methods included from Parameter
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (protected)
If a named argument with the specified method name exists, a call to that method will return the value of the argument.
247 248 249 250 251 252 253 |
# File 'lib/rubikon/has_arguments.rb', line 247 def method_missing(name, *args, &block) if args.empty? && !block_given? && @arg_names.include?(name) @args[name] else super end end |
Instance Method Details
#[](arg) ⇒ Object
Access the arguments of this parameter using a numeric or symbolic index
144 145 146 |
# File 'lib/rubikon/has_arguments.rb', line 144 def [](arg) @args[arg] end |
#args ⇒ Array<String>, Hash<Symbol, String> Also known as: arguments
Returns the arguments given to this parameter. They are given as a Hash when there are named arguments or as an Array when there are no named arguments
155 156 157 |
# File 'lib/rubikon/has_arguments.rb', line 155 def args @arg_names.empty? ? @args.values : @args end |
#initialize(app, name, *options, &block) ⇒ Object
Creates a new parameter with arguments with the given name and an optional code block
68 69 70 71 72 73 74 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rubikon/has_arguments.rb', line 68 def initialize(app, name, *, &block) super(app, name, &block) @arg_names = [] @arg_values = {} @args = {} @description = .shift if .first.is_a? String if .size == 1 && (.first.nil? || .first.is_a?(Fixnum) || .first.is_a?(Range)) = .first end if .is_a? Fixnum if > 0 @min_arg_count = @max_arg_count = elsif <= 0 @min_arg_count = - @max_arg_count = -1 end elsif .is_a? Range @min_arg_count = .first @max_arg_count = .last elsif .is_a? Array @arg_names = [] @max_arg_count = 0 @min_arg_count = 0 .each do |arg| if arg.is_a? Hash arg = arg.map do |arg_name, opt| [arg_name, opt.is_a?(Array) ? opt : [opt]] end arg = arg.sort_by do |arg_name, opt| opt.include?(:optional) ? 1 : 0 end arg.each do |arg_name, opt| matchers = opt.reject { |o| [:optional, :remainder].include? o } opt -= matchers @arg_names << arg_name.to_sym if !matchers.empty? matchers.map! do |m| ARGUMENT_MATCHERS[m] || (m.is_a?(Regexp) ? m : m.to_s) end @arg_values[arg_name] = /^#{Regexp.union *matchers}$/ end unless opt.include? :optional @min_arg_count += 1 end if opt.include? :remainder @max_arg_count = -1 break end @max_arg_count += 1 end else @arg_names << arg.to_sym @min_arg_count += 1 @max_arg_count += 1 end end else @min_arg_count = 0 @max_arg_count = 0 end end |