Class: Milc::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logic) ⇒ Command

Returns a new instance of Command.



10
11
12
# File 'lib/milc/command.rb', line 10

def initialize(logic)
  @logic = logic
end

Instance Attribute Details

#logicObject (readonly)

Returns the value of attribute logic.



9
10
11
# File 'lib/milc/command.rb', line 9

def logic
  @logic
end

Instance Method Details

#command_optionsObject

overriden



35
36
37
# File 'lib/milc/command.rb', line 35

def command_options
  "nVc:" # n と V と c: は必須
end

#load_options(options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/milc/command.rb', line 45

def load_options(options)
  if options["c"]
    yaml_path = options["c"]
    logic.config = YAML.load_file_with_erb(yaml_path)
    logic.load_config
  else
    show_help_and_exit1
  end
  logic.load_options(options)
end

#run(args) ⇒ Object



14
15
16
17
18
# File 'lib/milc/command.rb', line 14

def run(args)
  setup(args)
  logic.process
  # exit 0
end

#setup(args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/milc/command.rb', line 20

def setup(args)
  # ARGV.getopts については以下を参照
  # http://d.hatena.ne.jp/zariganitosh/20140819/ruby_optparser_true_power
  # http://docs.ruby-lang.org/ja/2.1.0/method/OptionParser=3a=3aArguable/i/getopts.html
  args.extend(OptionParser::Arguable) unless args.is_a?(OptionParser::Arguable)
  options = args.getopts(command_options)
  show_help_and_exit1 unless args.empty?

  Milc.dry_run = !!options["n"]
  Milc.verbose = !!options["V"]

  load_options(options)
end

#show_help_and_exit1Object



39
40
41
42
43
# File 'lib/milc/command.rb', line 39

def show_help_and_exit1
  ## シェルスクリプトのUsage
  $stderr.puts logic.help_message
  exit 1
end