Class: WhatOption::OptionInfo
- Inherits:
-
Object
- Object
- WhatOption::OptionInfo
- Defined in:
- lib/whatoption.rb
Instance Method Summary collapse
-
#initialize(commands, option) ⇒ OptionInfo
constructor
method.
-
#list ⇒ Object
method.
-
#load_sheet ⇒ Object
routine.
-
#resolve ⇒ Object
routine.
-
#show ⇒ Object
method.
Constructor Details
#initialize(commands, option) ⇒ OptionInfo
method
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/whatoption.rb', line 50 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
#list ⇒ Object
method
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/whatoption.rb', line 92 def list sheet = load_sheet abort if not sheet 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 print "Options for ", @commands.join.yellow, ":\n" sheet.each do |key, value| printf " %s%s%s\n", key.ljust(left).magenta, value['desc'].ljust(middle).green, value['func'] end end |
#load_sheet ⇒ Object
routine
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 |
# File 'lib/whatoption.rb', line 63 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 WhatOption_DATA_DIR, # So first '+'s both side are Pathname # And '+' is to add a '/' to component @file = WhatOption_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 |
#resolve ⇒ Object
routine
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/whatoption.rb', line 114 def resolve sheet = load_sheet abort if not sheet record = sheet[@option] || puts("Error: Option [#@option] is not recorded in #@file".red) || abort @rec_usage = record['usage'] || puts("Error: No field 'usage' in #@file [#@calling_command]".red) || abort @rec_desc = record['desc'] || puts("Error: No field 'desc' in #@file [#@calling_command]".red) || abort @rec_func = record['func'] || puts("Error: No field 'func' in #@file [#@calling_command]".red) || abort end |
#show ⇒ Object
method
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/whatoption.rb', line 130 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 |