Class: WhatOption::OptionInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/whatoption.rb

Instance Method Summary collapse

Constructor Details

#initialize(commands, option) ⇒ OptionInfo

method



48
49
50
51
52
53
54
55
56
57
# File 'lib/whatoption.rb', line 48

def initialize(commands, option)
  # Must clone, because we will delete in place later
  @commands = commands.clone
  @calling_command = commands[0]

  commands.delete_at(0)
  @sub_commands    = commands

  @option = option
end

Instance Method Details

#listObject

method



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/whatoption.rb', line 90

def list
  sheet = load_sheet
  if not sheet
    exit 1
  end

  option_max_len = sheet.keys.map(&:length).max
  left = option_max_len + 3

  lmda = -> hash do hash['desc'].length end
  desc_max_len = sheet.values.map(&lmda).max

  middle = desc_max_len + 3

  puts "Options for '#{@commands.join}':"
  sheet.each do |key, value|
    printf "  %s%s%s\n", key.ljust(left), value['desc'].ljust(middle), value['func']
  end
end

#load_sheetObject

routine



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
# File 'lib/whatoption.rb', line 61

def load_sheet
  require 'tomlrb'
  # It's very interesting here, you have to put parentheses then 'commands.join()' is
  # treated as a string.
  # Without parentheses, the + is affected by OPTIONS_DATA_DIR,
  # So first '+'s both side are Pathname
  # And '+' is to add a '/' to component
  @file = OPTIONS_DATA_DIR + (@commands.join('/') + ".toml")

  # if the file not exists, it is maybe in the dir (with its own name)
  # For example:
  #   "bash -c" will first search 'ROOT/bash.toml'
  #   if not exist, then we will search 'ROOT/bash/bash.toml'
  #
  if ! File.exist? @file
    @file = Pathname.new(@file.to_s.delete_suffix(".toml")) +
            (@commands.last + ".toml")
  end

  if File.exist? @file
    return Tomlrb.load_file @file
  else
    puts "File #@file not exist!"
    nil
  end
end

#resolveObject

routine



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/whatoption.rb', line 112

def resolve
  sheet = load_sheet
  if not sheet
    exit 1
  end

  record = sheet[@option] ||
    puts("Error: Option [#@option] is not recorded in #@file".red) ||
    exit(1)
  @rec_usage = record['usage'] ||
    puts("Error: No field 'usage' in #@file [#@calling_command]".red) ||
    exit(1)
  @rec_desc   = record['desc']   ||
    puts("Error: No field 'desc' in #@file [#@calling_command]".red) ||
    exit(1)
  @rec_func   = record['func']   ||
    puts("Error: No field 'func' in #@file [#@calling_command]".red) ||
    exit(1)
end

#showObject

method



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/whatoption.rb', line 134

def show
  resolve

  result_cmd = @calling_command.yellow
  if not @sub_commands.empty?
    result_cmd += ' ' + @sub_commands.join(' ').blue
  end
  result_cmd += ' ' + @rec_usage.magenta
  puts result_cmd

  # newline description
  print "\n  #{@option}: "
  puts @rec_desc.green
  print "\n  "
  puts @rec_func
  puts
end