Class: BaseChip::Cli::TaskOrData
- Inherits:
-
Object
- Object
- BaseChip::Cli::TaskOrData
- Includes:
- Reporting
- Defined in:
- lib/base_chip/cli.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#long_description ⇒ Object
Returns the value of attribute long_description.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize ⇒ TaskOrData
constructor
A new instance of TaskOrData.
- #mine_options(ostruct, arguments, unknown_check = false) ⇒ Object
- #option(name, hash) ⇒ Object
- #option_usage(o) ⇒ Object
- #options_table ⇒ Object
- #s2o(arg) ⇒ Object
- #table(array, delimiter = ' : ', indent = '') ⇒ Object
Methods included from Reporting
Constructor Details
#initialize ⇒ TaskOrData
Returns a new instance of TaskOrData.
112 113 114 |
# File 'lib/base_chip/cli.rb', line 112 def initialize @options = {} end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
110 111 112 |
# File 'lib/base_chip/cli.rb', line 110 def description @description end |
#long_description ⇒ Object
Returns the value of attribute long_description.
111 112 113 |
# File 'lib/base_chip/cli.rb', line 111 def long_description @long_description end |
#name ⇒ Object
Returns the value of attribute name.
108 109 110 |
# File 'lib/base_chip/cli.rb', line 108 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
109 110 111 |
# File 'lib/base_chip/cli.rb', line 109 def @options end |
Instance Method Details
#mine_options(ostruct, arguments, unknown_check = false) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/base_chip/cli.rb', line 148 def (ostruct, arguments, unknown_check = false) found = true while found found = false arguments.size.times do |i| a = arguments[i] case a when *(self..keys.map{|k|k.to_s}) h = self.[a] case h[:type] when :boolean; ostruct.send("#{h[:name]}=",true ) when :string ; ostruct.send("#{h[:name]}=",arguments[i+1].to_s); arguments.delete_at(i+1) # TODO error if i+1 doesn't exist when :integer; ostruct.send("#{h[:name]}=",arguments[i+1].to_i); arguments.delete_at(i+1) # TODO error if i+1 doesn't exist end arguments.delete_at(i) found = true break end end end if unknown_check arguments.each do |a| fault "#{self.name || 'base_chip'} didn't understand the option #{a}" if a =~ /^\-/ end end end |
#option(name, hash) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/base_chip/cli.rb', line 121 def option(name,hash) hash.merge! :name=>name @options[s2o name] = hash if hash[:alias] hash[:alias].each do |a| @options[s2o a] = hash end end end |
#option_usage(o) ⇒ Object
130 131 132 133 134 135 136 137 |
# File 'lib/base_chip/cli.rb', line 130 def option_usage(o) name = s2o o[:name] alt = o[:alias] ? " (#{ o[:alias].map{|a|s2o a}.join('|') })" : '' case o[:type] when :boolean; return name + alt else return "#{name}#{alt} <#{o[:type]}>" end end |
#options_table ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/base_chip/cli.rb', line 138 def return if self..size == 0 array = [] self..values.uniq.each do |t| array << [self.option_usage(t),t[:description]] end puts '' puts " #{ self.name ? self.name.to_s.titleize : 'Global' } Options:" table(array,nil," ") end |
#s2o(arg) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/base_chip/cli.rb', line 115 def s2o(arg) arg = arg.to_s arg = (arg.length > 1 ? '--' : '-') + arg arg.gsub!(/_/,'-') arg end |
#table(array, delimiter = ' : ', indent = '') ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/base_chip/cli.rb', line 174 def table(array,delimiter=' : ',indent='') delimiter ||= ' : ' widths = [] array.each do |array2| array2.size.times do |i| widths[i] ||= 0 array2[i] ||= '' len = array2[i].length + 2 widths[i] = len if widths[i] < len end end format = indent + widths.map{ |w| "%-#{w}s" }.join(delimiter) array.each do |array2| puts format % array2 end end |