Class: UnitSplit::CLI
- Inherits:
-
Object
- Object
- UnitSplit::CLI
- Defined in:
- lib/unit_split/cli.rb
Class Method Summary collapse
Class Method Details
.bootstrap(argv) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/unit_split/cli.rb', line 9 def bootstrap(argv) unit_type = nil parser = OptionParser.new parser.on('-s', 'seconds') { unit_type = :second } parser.on('-b', 'bytes') { unit_type = :byte } parser.on('-j', 'japanese number') { unit_type = :japanese } target = nil begin target = parser.parse!(argv) if target.kind_of? Array target = target.first end rescue OptionParser::MissingArgument => e abort(e.to_s) end if target.nil? || target.empty? if $stdin.ready? target = $stdin.readline.strip else abort "specify target number" end end unless unit_type abort "unit not specified" end unit = dispatch_unit(unit_type) unless unit abort "unknown unit type" end result = UnitSplit.split(target, unit) format_print result end |
.dispatch_unit(unit_type) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/unit_split/cli.rb', line 47 def dispatch_unit(unit_type) case unit_type when :second return UnitSplit::Unit::Second when :byte return UnitSplit::Unit::Byte when :japanese return UnitSplit::Unit::JapaneseNumber else return nil end end |
.format_print(result) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/unit_split/cli.rb', line 60 def format_print(result) max_value_length = 1 max_label_length = 1 # check label/value length result.each do |entry| label = entry[0] value = entry[1] if label && label.length > max_label_length max_label_length = label.length end if value && value.to_s.length > max_value_length max_value_length = value.to_s.length end end result.reverse.each do |entry| puts "%#{max_value_length}d %-#{max_label_length}s" % [entry[1], entry[0]] end end |