Class: RubyDrills::CLI

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

Overview

Manage the processing of command line options

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.input_argsArray<String>

Returns The input array of strings to process as CLI options.

Returns:

  • (Array<String>)

    The input array of strings to process as CLI options.



16
17
18
# File 'lib/ruby_drills/cli.rb', line 16

def input_args
  @input_args
end

.option_processorsArray

Returns The Procs that process the parsed options.

Returns:

  • (Array)

    The Procs that process the parsed options.



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

def option_processors
  @option_processors
end

.optionsProc

Returns The Proc defining the valid command line options.

Returns:

  • (Proc)

    The Proc defining the valid command line options.



9
10
11
# File 'lib/ruby_drills/cli.rb', line 9

def options
  @options
end

Class Method Details

.add_options(&block) ⇒ Object

Add another set of CLI options (a Slop block)



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby_drills/cli.rb', line 19

def add_options(&block)
  if options
    old_options = options
    self.options = proc do
      instance_exec(&old_options)
      instance_exec(&block)
    end
  else
    self.options = block
  end
  self
end

.parse_options(args = ARGV.dup) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_drills/cli.rb', line 45

def parse_options(args=ARGV.dup)
  unless options
    raise NoOptionsError, "No command line options defined! Use RubyDrills::CLI.add_options to add command line options."
  end

  self.input_args = args

  opts = Slop.parse!(args, :help => true, :multiple_switches => false, &options)

  # Option processors are optional.
  if option_processors
    option_processors.each { |processor| processor.call(opts) }
  end

  self
end

.process_options(&block) ⇒ Object

Add a block responsible for processing parsed options.



33
34
35
36
37
# File 'lib/ruby_drills/cli.rb', line 33

def process_options(&block)
  self.option_processors ||= []
  option_processors << block
  self
end

.resetObject

Clear ‘options` and `option_processors`



40
41
42
43
# File 'lib/ruby_drills/cli.rb', line 40

def reset
  self.options = nil
  self.option_processors = nil
end