Class: Command::CommandBase

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(postfix = "") ⇒ CommandBase

Returns a new instance of CommandBase.



10
11
12
13
14
# File 'lib/commandbase.rb', line 10

def initialize(postfix = "")
  @opt = OptionParser.new(nil, 20)
  @opt.banner = "Usage: #{@opt.program_name} #{self.class.to_s.scan(/::(.+)$/)[0][0].downcase} #{postfix}"
  @options = {}
end

Class Method Details

.execute_and_rescue_exit(argv) ⇒ Object

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



30
31
32
33
34
35
36
# File 'lib/commandbase.rb', line 30

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

Instance Method Details

#execute(argv) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/commandbase.rb', line 16

def execute(argv)
  @opt.parse!(argv)
rescue OptionParser::InvalidOption => e
  warn "不正なオプションです(#{e})"
  exit 1
rescue OptionParser::MissingArgument => e
  warn "オプションの引数が不正です(#{e})"
  exit 1
end

#oneline_help(msg) ⇒ Object



38
39
40
# File 'lib/commandbase.rb', line 38

def oneline_help(msg)
  ""
end