Class: DRbQS::Command::OptionSetting

Inherits:
Object
  • Object
show all
Defined in:
lib/drbqs/command_line/option_setting.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(help_message, setting) ⇒ OptionSetting

Returns a new instance of OptionSetting.



6
7
8
9
# File 'lib/drbqs/command_line/option_setting.rb', line 6

def initialize(help_message, setting)
  @opt_parser = OptionParser.new(help_message)
  @setting = setting
end

Instance Attribute Details

#settingObject (readonly)

Returns the value of attribute setting.



4
5
6
# File 'lib/drbqs/command_line/option_setting.rb', line 4

def setting
  @setting
end

Instance Method Details

#define(options = {}, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/drbqs/command_line/option_setting.rb', line 23

def define(options = {}, &block)
  instance_eval(&block) if block_given?
  if options[:log_level]
    set(:log_level, '--log-level LEVEL', String,
        "Set the log level. The value accepts 'fatal', 'error', 'warn', 'info', and 'debug'. The default is 'error'.")
  end
  if options[:daemon]
    set(:daemon, '--daemon OUT', String, 'Execute as daemon and set output file for stdout and stderr.')
  end
  if options[:debug]
    set(:debug, '--debug', 'Set $DEBUG true.')
  end
end

#parse!(argv) ⇒ Object



37
38
39
# File 'lib/drbqs/command_line/option_setting.rb', line 37

def parse!(argv)
  @opt_parser.parse!(argv)
end

#set(*args, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/drbqs/command_line/option_setting.rb', line 11

def set(*args, &block)
  unless @setting
    raise "Not in method 'define'."
  end
  @opt_parser.on(*args[1..-1]) do |v|
    @setting.set(args[0], v)
    if block_given?
      yield(@opt_parser)
    end
  end
end