Class: MyScripts::CLI

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

Overview

This class encapsulates Command Line Interface for running scripts. It can be instantiated with stubbed stdin and stdout if you want to run tests on your scripts

Defined Under Namespace

Classes: ScriptNameError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdin = $stdin, stdout = $stdout, kernel = Kernel) ⇒ CLI

Creates new command line interface



20
21
22
23
24
# File 'lib/my_scripts/cli.rb', line 20

def initialize( stdin=$stdin, stdout=$stdout, kernel = Kernel )
  @stdin = stdin
  @stdout = stdout
  @kernel = kernel
end

Instance Attribute Details

#kernelObject

Returns the value of attribute kernel.



11
12
13
# File 'lib/my_scripts/cli.rb', line 11

def kernel
  @kernel
end

#stdinObject

Returns the value of attribute stdin.



11
12
13
# File 'lib/my_scripts/cli.rb', line 11

def stdin
  @stdin
end

#stdoutObject

Returns the value of attribute stdout.



11
12
13
# File 'lib/my_scripts/cli.rb', line 11

def stdout
  @stdout
end

Class Method Details

.run(script_name, argv, argf = ARGF) ⇒ Object

Instantiates new CLI(command line interface) and runs script with given name (token) and argv inside new CLI instance



15
16
17
# File 'lib/my_scripts/cli.rb', line 15

def self.run( script_name, argv, argf=ARGF )
  new.run script_name, argv, argf
end

Instance Method Details

#run(script_name, argv, argf = ARGF) ⇒ Object

Runs a script with given name (token) and argv inside this CLI instance

Raises:



27
28
29
30
31
32
# File 'lib/my_scripts/cli.rb', line 27

def run( script_name, argv, argf=ARGF )
  script = script_class_name(script_name).to_class
  raise ScriptNameError.new("Script #{script_class_name(script_name)} not found") unless script
  
  script.new(script_name, self, argv, argf).run
end