Class: Vedeu::Config::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/configuration/cli.rb

Overview

The Configuration::CLI class parses command-line arguments using OptionParser into options used by Vedeu to affect certain behaviours.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ Vedeu::Configuration::CLI

Returns a new instance of Vedeu::Config::CLI.

Configure Vedeu via command-line arguments. Options set here via arguments override the client application configuration set via API::External#configure.

Parameters:

  • args (Array) (defaults to: [])


24
25
26
27
# File 'lib/vedeu/configuration/cli.rb', line 24

def initialize(args = [])
  @args    = args
  @options = {}
end

Instance Attribute Details

#argsArray<String> (readonly, protected)

Returns:

  • (Array<String>)


45
46
47
# File 'lib/vedeu/configuration/cli.rb', line 45

def args
  @args
end

#optionsHash (readonly, protected)

Returns:

  • (Hash)


49
50
51
# File 'lib/vedeu/configuration/cli.rb', line 49

def options
  @options
end

Class Method Details

.configure(args = []) ⇒ Object

Parameters:

  • args (Array) (defaults to: [])


12
13
14
# File 'lib/vedeu/configuration/cli.rb', line 12

def self.configure(args = [])
  new(args).configuration
end

Instance Method Details

#allowed_optionsArray<Symbol> (private)

Returns:

  • (Array<Symbol>)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vedeu/configuration/cli.rb', line 67

def allowed_options
  [
    :colour_mode,
    :cooked,
    :debug,
    :drb,
    :drb_height,
    :drb_host,
    :drb_port,
    :drb_width,
    :fake,
    :interactive,
    :log,
    :raw,
    :run_many,
    :run_once,
    :root,
    :standalone,
  ]
end

Returns:

  • (String)


89
90
91
# File 'lib/vedeu/configuration/cli.rb', line 89

def banner
  parser.banner = "Usage: #{$PROGRAM_NAME} [options]"
end

#colour_modeOptionParser (private)

CLI arguments:

-C, --colour-mode

Returns:

  • (OptionParser)


98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vedeu/configuration/cli.rb', line 98

def colour_mode
  parser.on('-C', '--colour-mode [COLOURS]', Integer,
            'Run application in either `8`, `16`, `256` or `16777216` ' \
            'colour mode.') do |colours|
    if [8, 16, 256, 16_777_216].include?(colours)
      options[:colour_mode] = colours

    else
      options[:colour_mode] = 8

    end
  end
end

#configurationHash

Returns the configuration options set up by parsing the command-line arguments passed to the client application.

Returns:

  • (Hash)


33
34
35
36
37
38
39
# File 'lib/vedeu/configuration/cli.rb', line 33

def configuration
  setup!

  parser.parse!(args)

  Vedeu::Config.log(Esc.blue { '[cli]' }, options)
end

#cookedOptionParser (private)

CLI arguments:

-c, --cooked

Returns:

  • (OptionParser)


117
118
119
120
121
# File 'lib/vedeu/configuration/cli.rb', line 117

def cooked
  parser.on('-c', '--cooked', 'Run application in cooked mode.') do
    options[:terminal_mode] = :cooked
  end
end

#debugOptionParser (private)

CLI arguments:

-d, --debug

Returns:

  • (OptionParser)


128
129
130
131
132
# File 'lib/vedeu/configuration/cli.rb', line 128

def debug
  parser.on('-d', '--debug', 'Run application with debugging on.') do
    options[:debug] = true
  end
end

#drbOptionParser (private)

CLI arguments:

--drb

Returns:

  • (OptionParser)


139
140
141
142
143
# File 'lib/vedeu/configuration/cli.rb', line 139

def drb
  parser.on('--drb', 'Run application with DRb on.') do
    options[:drb] = true
  end
end

#drb_heightOptionParser (private)

CLI arguments:

--drb-height

Returns:

  • (OptionParser)


150
151
152
153
154
155
156
# File 'lib/vedeu/configuration/cli.rb', line 150

def drb_height
  parser.on('--drb-height',
            'Set the height for fake terminal.') do |height|
    options[:drb]        = true
    options[:drb_height] = height
  end
end

#drb_hostOptionParser (private)

CLI arguments:

--drb-host

Returns:

  • (OptionParser)


163
164
165
166
167
168
169
# File 'lib/vedeu/configuration/cli.rb', line 163

def drb_host
  parser.on('--drb-host',
            'Set the hostname/IP for the DRb server.') do |hostname|
    options[:drb]      = true
    options[:drb_host] = hostname
  end
end

#drb_portOptionParser (private)

CLI arguments:

--drb-port

Returns:

  • (OptionParser)


176
177
178
179
180
181
# File 'lib/vedeu/configuration/cli.rb', line 176

def drb_port
  parser.on('--drb-port', 'Set the port for the DRb server.') do |port|
    options[:drb]      = true
    options[:drb_port] = port
  end
end

#drb_widthOptionParser (private)

CLI arguments:

--drb-width

Returns:

  • (OptionParser)


188
189
190
191
192
193
194
# File 'lib/vedeu/configuration/cli.rb', line 188

def drb_width
  parser.on('--drb-width',
            'Set the width for fake terminal.') do |width|
    options[:drb]       = true
    options[:drb_width] = width
  end
end

#fakeOptionParser (private)

CLI arguments:

-i, --interactive

Returns:

  • (OptionParser)


201
202
203
204
205
# File 'lib/vedeu/configuration/cli.rb', line 201

def fake
  parser.on('-f', '--fake', 'Run application in fake mode.') do
    options[:terminal_mode] = :fake
  end
end

#interactiveOptionParser (private)

Returns:

  • (OptionParser)


208
209
210
211
212
213
# File 'lib/vedeu/configuration/cli.rb', line 208

def interactive
  parser.on('-i', '--interactive',
            'Run the application in interactive mode (default).') do
    options[:interactive] = true
  end
end

#logOptionParser (private)

CLI arguments:

-l, --log

Returns:

  • (OptionParser)


220
221
222
223
224
225
# File 'lib/vedeu/configuration/cli.rb', line 220

def log
  parser.on('-l', '--log [FILENAME]', String,
            'Specify the path for the log file.') do |filename|
    options[:log] = filename
  end
end

#parserOptionParser (private)

Returns:

  • (OptionParser)


62
63
64
# File 'lib/vedeu/configuration/cli.rb', line 62

def parser
  @parser ||= OptionParser.new
end

#rawOptionParser (private)

CLI arguments:

-r, --raw

Returns:

  • (OptionParser)


232
233
234
235
236
# File 'lib/vedeu/configuration/cli.rb', line 232

def raw
  parser.on('-r', '--raw', 'Run application in raw mode (default).') do
    options[:terminal_mode] = :raw
  end
end

#rootOptionParser (private)

CLI arguments:

-s, --root

Returns:

  • (OptionParser)


243
244
245
246
247
248
# File 'lib/vedeu/configuration/cli.rb', line 243

def root
  parser.on('-s', '--root []', String,
            'Start the application from the specified controller.') do |c|
    options[:root] = c
  end
end

#run_manyOptionParser (private)

CLI arguments:

-n, --run-many

Returns:

  • (OptionParser)


255
256
257
258
259
260
# File 'lib/vedeu/configuration/cli.rb', line 255

def run_many
  parser.on('-n', '--run-many',
            'Run the application loop continuously (default).') do
    options[:once] = false
  end
end

#run_onceOptionParser (private)

CLI arguments:

-1, --run-once

Returns:

  • (OptionParser)


267
268
269
270
271
# File 'lib/vedeu/configuration/cli.rb', line 267

def run_once
  parser.on('-1', '--run-once', 'Run the application loop once.') do
    options[:once] = true
  end
end

#setup!void (private)

This method returns an undefined value.

Setup Vedeu using CLI configuration options for the client application.



57
58
59
# File 'lib/vedeu/configuration/cli.rb', line 57

def setup!
  ([:banner] + allowed_options).each { |opt| send(opt) }
end

#standaloneOptionParser (private)

CLI arguments:

-I, --noninteractive, --standalone

Returns:

  • (OptionParser)


278
279
280
281
282
283
284
# File 'lib/vedeu/configuration/cli.rb', line 278

def standalone
  parser.on('-I', '--noninteractive', '--standalone',
            'Run the application non-interactively; ' \
            'i.e. not requiring intervention from the user.') do
    options[:interactive] = false
  end
end