Class: Speedflow::Command

Inherits:
Object
  • Object
show all
Extended by:
Message
Defined in:
lib/speedflow/command.rb

Overview

Used to manage the commands

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Message

error, info, message, notice, success

Class Attribute Details

.argvObject

Public: Define argv (used to test)

Returns Array of argv.



95
96
97
# File 'lib/speedflow/command.rb', line 95

def argv
  @argv ||= ARGV
end

Class Method Details

.config_from_fresh_options(program, command, options = {}) ⇒ Object

Public: Get config from fresh options

program - Program. command - Commad. options - Options.

Returns configuration Hash.



50
51
52
53
54
55
56
# File 'lib/speedflow/command.rb', line 50

def config_from_fresh_options(program, command, options = {})
  execute_from_option(program, command, 'config') do |value|
    options = { config: value }
  end

  config_from_options(options)
end

.config_from_options(options) ⇒ Object

Speedflow configuration with the options passed in as overrides

options - the configuration overrides

Returns a full Speedflow configuration.



33
34
35
36
37
38
39
40
41
# File 'lib/speedflow/command.rb', line 33

def config_from_options(options)
  Speedflow::Configuration.get(options)
rescue ConfigurationFileNotFound => exception
  error exception.message
  abort
rescue ConfigurationFileFormat => exception
  error exception.message
  abort
end

.execute_from_option(prog, command, opt_key) ⇒ Object

Public: Exectute option from key

prog - Program. command - Commad. opt_key - Option key.

Returns nothing.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/speedflow/command.rb', line 65

def execute_from_option(prog, command, opt_key)
  opt_index = prog.options.find_index { |o| o.config_key == opt_key }
  opt = prog.options[opt_index]
  opt_parser = OptionParser.new do |opts|
    command.add_default_options(opts)
    opts.on(*opt.for_option_parser) { |value| yield(value) }
  end

  # rubocop:disable HandleExceptions
  begin
    opt_parser.parse(argv)
  rescue OptionParser::InvalidOption
    # Nothing.
  end
  # rubocop:enable HandleExceptions
end

.inherited(base) ⇒ Object

Keep a list of subclasses of Speedflow::Command every time it’s inherited. Called automatically.

base - the subclass

Returns nothing.



23
24
25
26
# File 'lib/speedflow/command.rb', line 23

def inherited(base)
  subclasses << base
  super(base)
end

.invalid_command(cmd) ⇒ Object

Display invalid command.

cmd - the command

Returns nothing



87
88
89
90
# File 'lib/speedflow/command.rb', line 87

def invalid_command(cmd)
  error "Command not found '#{cmd}'."
  notice 'Please, use --help to display valid commands.'
end

.subclassesObject

A list of subclasses of Speedflow::Command

Returns Array of commands.



13
14
15
# File 'lib/speedflow/command.rb', line 13

def subclasses
  @subclasses ||= []
end