Class: RubyDrills::CLI
- Inherits:
-
Object
- Object
- RubyDrills::CLI
- Defined in:
- lib/ruby_drills/cli.rb
Overview
Manage the processing of command line options
Class Attribute Summary collapse
-
.input_args ⇒ Array<String>
The input array of strings to process as CLI options.
-
.option_processors ⇒ Array
The Procs that process the parsed options.
-
.options ⇒ Proc
The Proc defining the valid command line options.
Class Method Summary collapse
-
.add_options(&block) ⇒ Object
Add another set of CLI options (a Slop block).
- .parse_options(args = ARGV.dup) ⇒ Object
-
.process_options(&block) ⇒ Object
Add a block responsible for processing parsed options.
-
.reset ⇒ Object
Clear ‘options` and `option_processors`.
Class Attribute Details
.input_args ⇒ Array<String>
Returns 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_processors ⇒ Array
Returns The Procs that process the parsed options.
12 13 14 |
# File 'lib/ruby_drills/cli.rb', line 12 def option_processors @option_processors end |
.options ⇒ Proc
Returns The Proc defining the valid command line options.
9 10 11 |
# File 'lib/ruby_drills/cli.rb', line 9 def @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 (&block) if = self. = proc do instance_exec(&) instance_exec(&block) end else self. = 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 (args=ARGV.dup) unless 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, &) # 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 (&block) self.option_processors ||= [] option_processors << block self end |
.reset ⇒ Object
Clear ‘options` and `option_processors`
40 41 42 43 |
# File 'lib/ruby_drills/cli.rb', line 40 def reset self. = nil self.option_processors = nil end |