Class: Command::CommandBase

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(postfixies = "") ⇒ CommandBase

postfixies は改行で区切ることで2パターン以上記述できる



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

def initialize(postfixies = "")
  @opt = OptionParser.new(nil, 20)
  command_name = self.class.to_s.scan(/::(.+)$/)[0][0].downcase
  banner = postfixies.split("\n").map.with_index { |postfix, i|
    (i == 0 ? "Usage: " : "   or: ") + "narou #{command_name} #{postfix}"
  }.join("\n")
  @opt.banner = "<bold><green>#{TermColor.escape(banner)}</green></bold>".termcolor
  @options = {}
  # ヘルプを見やすく色付け
  def @opt.help
    msg = super
    # 見出し部分
    msg.gsub!(/((?:Examples|Options|Configuration|[^\s]+? Variable List):)/) do
      "<underline><bold>#{$1}</bold></underline>".termcolor
    end
    # Examples のコメント部分
    msg.gsub!(/(#.+)$/) do
      "<cyan>#{TermColor.escape($1)}</cyan>".termcolor
    end
    # 文字列部分
    msg.gsub!(/(".+?")/) do
      "<yellow>#{TermColor.escape($1)}</yellow>".termcolor
    end
    msg
  end
end

Class Method Details

.execute!(argv) ⇒ Object

普通にコマンドを実行するけど、exit(2) を補足してexitstatus を返す 正常終了なら0



82
83
84
85
86
87
88
# File 'lib/commandbase.rb', line 82

def self.execute!(argv)
  self.new.execute(argv)
rescue SystemExit => e
  e.status
else
  0
end

.helpObject

ヘルプを見やすく色付け



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/commandbase.rb', line 20

def @opt.help
  msg = super
  # 見出し部分
  msg.gsub!(/((?:Examples|Options|Configuration|[^\s]+? Variable List):)/) do
    "<underline><bold>#{$1}</bold></underline>".termcolor
  end
  # Examples のコメント部分
  msg.gsub!(/(#.+)$/) do
    "<cyan>#{TermColor.escape($1)}</cyan>".termcolor
  end
  # 文字列部分
  msg.gsub!(/(".+?")/) do
    "<yellow>#{TermColor.escape($1)}</yellow>".termcolor
  end
  msg
end

.oneline_help(msg) ⇒ Object



90
91
92
# File 'lib/commandbase.rb', line 90

def self.oneline_help(msg)
  ""
end

Instance Method Details

#execute(argv) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/commandbase.rb', line 38

def execute(argv)
  @options.clear
  load_local_settings
  @opt.parse!(argv)
rescue OptionParser::InvalidOption => e
  error "不明なオプションです(#{e})"
  exit 1
rescue OptionParser::MissingArgument => e
  error "オプションの引数が指定されていないか正しくありません(#{e})"
  exit 1
end

#force_change_settings_function(pairs) ⇒ Object

設定の強制設定



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/commandbase.rb', line 112

def force_change_settings_function(pairs)
  settings = Inventory.load("local_setting", :local)
  modified = false
  pairs.each do |name, value|
    if settings[name].nil? || settings[name] != value
      settings[name] = value
      puts "<bold><cyan>#{name}#{value} に強制変更しました</cyan></bold>".termcolor
      modified = true
    end
  end
  settings.save if modified
end

#hook_call(target_method, *argv) ⇒ Object

指定したメソッドを呼び出す際に、フック関数があればそれ経由で呼ぶ

指定したメソッドは存在しなくてもいい。存在しなければ空のProcが作られる



99
100
101
102
103
104
105
106
107
# File 'lib/commandbase.rb', line 99

def hook_call(target_method, *argv)
  hook = "hook_#{target_method}"
  target_method_proc = self.method(target_method) rescue ->{}
  if respond_to?(hook)
    self.__send__(hook, *argv, &target_method_proc)
  else
    target_method_proc.call(*argv)
  end
end

#load_local_settingsObject



50
51
52
53
54
55
56
57
58
# File 'lib/commandbase.rb', line 50

def load_local_settings
  command_prefix = self.class.to_s.scan(/[^:]+$/)[0].downcase
  local_settings = Inventory.load("local_setting", :local)
  local_settings.each do |name, value|
    if name =~ /^#{command_prefix}\.(.+)$/
      @options[$1] = value
    end
  end
end

#tagname_to_ids(array) ⇒ Object

タグ情報をID情報に展開する



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/commandbase.rb', line 63

def tagname_to_ids(array)
  database = Database.instance
  tag_index = Hash.new { [] }
  database.each do |id, data|
    tags = data["tags"] || []
    tags.each do |tag|
      tag_index[tag] |= [id]
    end
  end
  array.map! { |arg|
    ids = tag_index[arg]
    ids.empty? ? arg : ids
  }.flatten!
end