Class: RCT

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

Overview


Assorted utility methods of general interest.

Class Method Summary collapse

Class Method Details

.argv_get(pos) ⇒ Object


Return the value of the flag at position pos in ARGV. Removes both the value and the flag from ARGV.



95
96
97
98
99
100
# File 'lib/rct/baseutils.rb', line 95

def self.argv_get(pos)
  value = ARGV[pos + 1]
  ARGV.delete_at(pos + 1)
  ARGV.delete_at(pos)
  return value
end

.bad_invocation(msg) ⇒ Object


Log an error to stdout (prefixed with ‘error:’) and exit.



76
77
78
79
# File 'lib/rct/baseutils.rb', line 76

def self.bad_invocation(msg)
  error(msg)
  exit(1)
end

.die(msg) ⇒ Object


Log an error to stdout (prefixed with ‘error:’) and exit.



85
86
87
88
# File 'lib/rct/baseutils.rb', line 85

def self.die(msg)
  error(msg)
  exit(1)
end

.error(msg) ⇒ Object


Log an error to stdout (prefixed with ‘error:’)



68
69
70
# File 'lib/rct/baseutils.rb', line 68

def self.error(msg)
  puts "error: #{msg}"
end

.get_opt(info) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rct/baseutils.rb', line 162

def self.get_opt(info)
  pos = 0
  while (pos < ARGV.length)
    arg = ARGV[pos]
    if (arg == info[0] || arg == info[1])
      return argv_get(pos)
    else
      pos += 1
    end
  end
end

.helpObject




106
107
108
109
110
111
112
113
114
115
# File 'lib/rct/baseutils.rb', line 106

def self.help

  # default is CLI invocation of method name
  # rct Time.get_time
  #
  # provide require file if it is not in CLI list
  # rct Time.get_time --req time_client
  # rct --test test-suite

end

.increase_log_levelObject


Increase log verbosity level by one.



50
51
52
# File 'lib/rct/baseutils.rb', line 50

def self.increase_log_level
  @log_level += 1
end

.log(level, line) ⇒ Object


Log a line of output, if appropriate for current log level.



58
59
60
61
62
# File 'lib/rct/baseutils.rb', line 58

def self.log(level, line)
  if (level <= @log_level)
    puts line
  end
end

.log_levelObject


Return current log level.



32
33
34
# File 'lib/rct/baseutils.rb', line 32

def self.log_level
  @log_level
end

.parse_global_optionsObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rct/baseutils.rb', line 124

def self.parse_global_options
  pos = 0

  if (ARGV == nil)
    die("No arguments!")
  end

  while (pos < ARGV.length)
    arg = ARGV[pos]

    if (arg == '-t' || arg == '--test')
      sset(RCT_MODE, RCT_MODE_TEST)
      sset(TEST_SUITE_FILE, argv_get(pos))

    elsif (arg == '--req')
      require argv_get(pos)

    elsif (arg == '-h' || arg == '--host')
      sset(SERVER_HOSTNAME, argv_get(pos))

    elsif (arg == '-p' || arg == '--port')
      sset(SERVER_PORT, argv_get(pos))

    elsif (arg == '--cafile')
      sset(SSL_CA_FILE, argv_get(pos))

    elsif (arg == '-v')
      increase_log_level()
      ARGV.delete_at(pos)

    else
      pos += 1
    end

  end
end

.parse_options(opts, required) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/rct/baseutils.rb', line 175

def self.parse_options(opts, required)
  return if opts == nil
  opts.each { |key, info|
    if ($GLOBAL_OPTS[info[0]] == 1 || $GLOBAL_OPTS[info[1]] == 1)
      bad_invocation("Error in CLI definition: #{info[0]}|#{info[1]} " +
                     "conflicts with global options!")
    end
    value = get_opt(info)
    if (required && value == nil)
      bad_invocation("Required argument #{info[0]}|#{info[1]} has no value!")
    end
    $STATE.set(key, value)
  }
end

.sdelete(key) ⇒ Object


Remove the value of a key from the global state.



218
219
220
# File 'lib/rct/baseutils.rb', line 218

def self.sdelete(key)
  $STATE.delete(key)
end

.set_log_level(level) ⇒ Object


Set log level (RESULT, INFO, DEBUG; from constants.rb).



40
41
42
43
44
# File 'lib/rct/baseutils.rb', line 40

def self.set_log_level(level)
  @log_level = RESULT if level == RESULT
  @log_level = INFO if level == INFO
  @log_level = DEBUG if level == DEBUG
end

.sget(key) ⇒ Object


Get the value of a key from the global state.



210
211
212
# File 'lib/rct/baseutils.rb', line 210

def self.sget(key)
  $STATE.get(key)
end

.sset(key, value, temp = false) ⇒ Object


Set a key,value pair in the global state.



194
195
196
# File 'lib/rct/baseutils.rb', line 194

def self.sset(key, value, temp=false)
  $STATE.set(key, value, temp)
end

.ssettmp(key, value) ⇒ Object


Set a temporary key,value pair in the global state.



202
203
204
# File 'lib/rct/baseutils.rb', line 202

def self.ssettmp(key, value)
  $STATE.set(key, value, true)
end