Class: Aargvark
- Inherits:
-
Object
- Object
- Aargvark
- Defined in:
- lib/aargvark.rb
Defined Under Namespace
Classes: Argument
Instance Attribute Summary collapse
-
#aliens ⇒ Object
Returns the value of attribute aliens.
-
#argument_pool ⇒ Object
Returns the value of attribute argument_pool.
-
#letter_array ⇒ Object
Returns the value of attribute letter_array.
-
#matches ⇒ Object
Returns the value of attribute matches.
-
#noarg_count ⇒ Object
Returns the value of attribute noarg_count.
Instance Method Summary collapse
- #get_matches ⇒ Object
-
#initialize(argv, all_args) ⇒ Aargvark
constructor
A new instance of Aargvark.
- #spit_argument(in_arg) ⇒ Object
Constructor Details
#initialize(argv, all_args) ⇒ Aargvark
Returns a new instance of Aargvark.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/aargvark.rb', line 7 def initialize(argv, all_args) @argv = argv @argument_pool = [] @letter_array = [] @matches = [] all_args.each {|i| @argument_pool << Argument.new(i)} @noarg_count = [0,0,0] @argument_pool.each do |i| if i.noarg @noarg_count[0] += 1 if i.required @noarg_count[1] += 1 end end get_matches end |
Instance Attribute Details
#aliens ⇒ Object
Returns the value of attribute aliens.
5 6 7 |
# File 'lib/aargvark.rb', line 5 def aliens @aliens end |
#argument_pool ⇒ Object
Returns the value of attribute argument_pool.
5 6 7 |
# File 'lib/aargvark.rb', line 5 def argument_pool @argument_pool end |
#letter_array ⇒ Object
Returns the value of attribute letter_array.
5 6 7 |
# File 'lib/aargvark.rb', line 5 def letter_array @letter_array end |
#matches ⇒ Object
Returns the value of attribute matches.
5 6 7 |
# File 'lib/aargvark.rb', line 5 def matches @matches end |
#noarg_count ⇒ Object
Returns the value of attribute noarg_count.
5 6 7 |
# File 'lib/aargvark.rb', line 5 def noarg_count @noarg_count end |
Instance Method Details
#get_matches ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 135 136 137 |
# File 'lib/aargvark.rb', line 24 def get_matches # Add all present argument letters and aliases to the letter_array array @argument_pool.each do |arg| @letter_array << arg.letter if arg.alias @letter_array << arg.alias end end @argv.each do |s| if s.is_arg? || s.is_alias? unless @letter_array.index(s) type = s.is_arg? ? "Argument" : "Alias" raise ArgumentError, "#{type} #{s} was entered, but is not valid." end end end @argument_pool.each do |arg| if arg.letter letter_pos = @argv.index(arg.letter) if letter_pos = letter_pos + 1 end end if arg.alias alias_pos = @argv.index(arg.alias) if alias_pos = alias_pos + 1 end end if ! letter_pos && ! alias_pos && ! arg.noarg && arg.required error = "The use of argument \"#{arg.letter}\" " error += arg.alias ? "or its alias \"#{arg.alias}\" are" : "is" error += " required when running this script.\nPlease make sure that " error += arg.alias ? "either of these are" : "this is" error += " called when running the script from the commandline." raise ArgumentError, error end #need script to input the values for the :noarg arguments based on the end of #the sub-arguments for the last argument in the argv array if arg.noarg last_arg = @argv.collect {|e| e.scan(/^-[a-z]$|^--[a-z\-]+$/)}.flatten.pop arg_obj = spit_argument(last_arg) last_arg_pos = @argv.rindex(last_arg) diff = @argv.size - last_arg_pos end if letter_pos || alias_pos if @argv.rindex(arg.letter) != letter_pos || @argv.rindex(arg.alias) != alias_pos raise ArgumentError, "Argument \"#{arg.letter}\" was called twice from the commandline. "+ "Each argument should only be called once." end # Raise an error if both an argument and its alias are called from the # command line if letter_pos && alias_pos raise ArgumentError, "Argument \"#{arg.letter}\" and alias \"#{arg.alias}\" were both" + " called from the command line. Please use only the letter" + " argument or the alias, not both." end alien_pos = alien_count = 0 if @argv[alien_pos] != nil while ! @argv[alien_pos].is_arg? && ! @argv[alien_pos].is_alias? argv_diff = @argv.size - alien_pos break if @noarg_count[0] <= argv_diff && argv_diff <= @noarg_count[1] alien_pos += 1 alien_count += 1 break if ! @argv[alien_pos] end end min = arg.[0] max = arg.[1] if alien_count < min || max < alien_count error = "#{alien_count} sub-arguments have been given for argument #{arg.letter}.\n" + "The current parameters for #{arg.letter} specify that you use " error += min == max ? "exactly #{min}" : "between #{min} and #{max}" error += " sub-arguments. \nPlease double-check your " + "input array and ensure the correct number of sub-arguments." raise ArgumentError, error end @matches << arg.letter if arg.sub_args arg.sub_args.each do || required = [0] type = [1] if required == false if @argv[].class.to_s == type.to_s.capitalize && @letter_array.index(@argv[]) == nil @matches << @argv[] end elsif required == true if @argv[].class.to_s == type.to_s.capitalize && @letter_array.index(@argv[]) == nil @matches << @argv[] else raise ArgumentError, "\"#{@argv[]}\" is not a valid sub-argument for argument #{arg.letter}.\n" + "A valid sub-argument of type #{type.to_s.capitalize} must be entered in this position." end end += 1 end end end end end |
#spit_argument(in_arg) ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/aargvark.rb', line 139 def spit_argument(in_arg) if ! in_arg.is_arg? && ! in_arg.is_alias? raise ArgumentError, "#{in_arg} is not a valid argument request for spit_argument. The \"letter\" " + "variable should be either an argument or an alias." end @argument_pool.each do |a| return a if a.letter == in_arg || a.alias == in_arg end end |