Class: BitClust::Subcommands::PropertyCommand

Inherits:
BitClust::Subcommand show all
Defined in:
lib/bitclust/subcommands/property_command.rb

Instance Method Summary collapse

Methods inherited from BitClust::Subcommand

#error, #help, #option_error, #srcdir_root

Constructor Details

#initializePropertyCommand

Returns a new instance of PropertyCommand.


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bitclust/subcommands/property_command.rb', line 14

def initialize
  super
  @mode = nil
  @parser.banner = "Usage: #{File.basename($0, '.*')} property [options]"
  @parser.on('--list', 'List all properties.') {
    @mode = :list
  }
  @parser.on('--get', 'Get property value.') {
    @mode = :get
  }
  @parser.on('--set', 'Set property value.') {
    @mode = :set
  }
end

Instance Method Details

#exec(argv, options) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bitclust/subcommands/property_command.rb', line 50

def exec(argv, options)
  prefix = options[:prefix]
  db = MethodDatabase.new(prefix)
  case @mode
  when :list
    db.properties.each do |key, val|
      puts "#{key}=#{val}"
    end
  when :get
    argv.each do |key|
      puts db.propget(key)
    end
  when :set
    key, val = *argv
    db.transaction {
      db.propset key, val
    }
  else
    raise "must not happen: #{@mode}"
  end
end

#parse(argv) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bitclust/subcommands/property_command.rb', line 29

def parse(argv)
  super
  unless @mode
    error "one of (--list|--get|--set) is required"
  end
  case @mode
  when :list
    unless argv.empty?
      error "--list requires no argument"
    end
  when :get
    ;
  when :set
    unless argv.size == 2
      error "--set requires just 2 arguments"
    end
  else
    raise "must not happen: #{@mode}"
  end
end