Class: MyScripts::Script

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

Overview

Base class for all scripts. Subclass it and override run method with actual work your script will be doing

Direct Known Subclasses

Ansi, Bon, Citi, Gitto, Jew, Msdn, Rabbit, Wake

Instance Method Summary collapse

Constructor Details

#initialize(name, cli, argv, argf) ⇒ Script

Returns a new instance of Script.



5
6
7
8
9
10
# File 'lib/my_scripts/script.rb', line 5

def initialize( name, cli, argv, argf )
  @name = name
  @cli = cli
  @argv = argv
  @argf = argf
end

Instance Method Details

#error(text) ⇒ Object

Outputs error text, then exits with code 1



46
47
48
49
# File 'lib/my_scripts/script.rb', line 46

def error( text )
  puts "Script #{@name} #{version} - Error: #{text}"
  exit 1
end

#getsObject



33
34
35
# File 'lib/my_scripts/script.rb', line 33

def gets
  @cli.stdin.gets
end

#puts(*args) ⇒ Object



28
29
30
31
# File 'lib/my_scripts/script.rb', line 28

def puts( *args )
  @cli.stdout.puts *args
  nil
end

#runObject

This is where all action happens. Should be defined by all Script implementations



21
22
# File 'lib/my_scripts/script.rb', line 21

def run
end

#system(*args) ⇒ Object



24
25
26
# File 'lib/my_scripts/script.rb', line 24

def system( *args)
  @cli.kernel.system *args
end

#to_sObject



51
52
53
# File 'lib/my_scripts/script.rb', line 51

def to_s
  "#{@name} #{@argv.join(' ')} -> #{self.class}"
end

#usage(examples, explanation = nil) ⇒ Object

Outputs usage notes (and optional extended explanation), then exits with code 1



38
39
40
41
42
43
# File 'lib/my_scripts/script.rb', line 38

def usage( examples, explanation = nil )
  puts "Script #{@name} #{version} - Usage:"
  (examples.respond_to?(:split) ? examples.split("\n") : examples).map {|line| puts "    #{@name} #{line}"}
  puts explanation if explanation
  exit 1
end

#versionObject



12
13
14
15
16
17
18
# File 'lib/my_scripts/script.rb', line 12

def version
  if self.class.const_defined? :VERSION
    self.class::VERSION  # Script's own version
  else
    VERSION # Gem version
  end
end