Class: CyberarmEngine::Console::Command::SubCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/cyberarm_engine/console/subcommand.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, command, type) ⇒ SubCommand

Returns a new instance of SubCommand.



7
8
9
10
11
# File 'lib/cyberarm_engine/console/subcommand.rb', line 7

def initialize(parent, command, type)
  @parent = parent
  @command = command
  @type = type
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



13
14
15
# File 'lib/cyberarm_engine/console/subcommand.rb', line 13

def command
  @command
end

Instance Method Details

#handle(arguments, console) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cyberarm_engine/console/subcommand.rb', line 15

def handle(arguments, console)
  if arguments.size > 1
    console.stdin("to many arguments for #{Style.highlight(command.to_s)}, got #{Style.error(arguments.size)} expected #{Style.notice(1)}.")
    return
  end

  case @type
  when :boolean
    case arguments.last
    when "", nil
      var = @parent.get(command.to_sym) || false
      console.stdin("#{command}: #{Style.highlight(var)}")
    when "on"
      var = @parent.set(command.to_sym, true)
      console.stdin("#{command} => #{Style.highlight(var)}")
    when "off"
      var = @parent.set(command.to_sym, false)
      console.stdin("#{command} => #{Style.highlight(var)}")
    else
      console.stdin("Invalid argument for #{Style.highlight(command.to_s)}, got #{Style.error(arguments.last)} expected #{Style.notice('on')}, or #{Style.notice('off')}.")
    end
  when :string
    case arguments.last
    when "", nil
      var = @parent.get(command.to_sym) || "\"\""
      console.stdin("#{command}: #{Style.highlight(var)}")
    else
      var = @parent.set(command.to_sym, arguments.last)
      console.stdin("#{command} => #{Style.highlight(var)}")
    end
  when :integer
    case arguments.last
    when "", nil
      var = @parent.get(command.to_sym) || "nil"
      console.stdin("#{command}: #{Style.highlight(var)}")
    else
      begin
        var = @parent.set(command.to_sym, Integer(arguments.last))
        console.stdin("#{command} => #{Style.highlight(var)}")
      rescue ArgumentError
        console.stdin("Error: #{Style.error("Expected an integer, got '#{arguments.last}'")}")
      end
    end
  when :decimal
    case arguments.last
    when "", nil
      var = @parent.get(command.to_sym) || "nil"
      console.stdin("#{command}: #{Style.highlight(var)}")
    else
      begin
        var = @parent.set(command.to_sym, Float(arguments.last))
        console.stdin("#{command} => #{Style.highlight(var)}")
      rescue ArgumentError
        console.stdin("Error: #{Style.error("Expected a decimal or integer, got '#{arguments.last}'")}")
      end
    end
  else
    raise RuntimeError
  end
end

#usageObject



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cyberarm_engine/console/subcommand.rb', line 85

def usage
  case @type
  when :boolean
    "#{Style.highlight(command)} #{Style.notice('[on|off]')}"
  when :string
    "#{Style.highlight(command)} #{Style.notice('[string]')}"
  when :integer
    "#{Style.highlight(command)} #{Style.notice('[0]')}"
  when :decimal
    "#{Style.highlight(command)} #{Style.notice('[0.0]')}"
  end
end

#valuesObject



76
77
78
79
80
81
82
83
# File 'lib/cyberarm_engine/console/subcommand.rb', line 76

def values
  case @type
  when :boolean
    %w[on off]
  else
    []
  end
end