Sha256: 38eed384ef794a84f4eada0078a483012e8ece71d51a8a9980f43cd1195391e0

Contents?: true

Size: 1.29 KB

Versions: 9

Compression:

Stored size: 1.29 KB

Contents

module MyScripts
  # Base class for all scripts. Subclass it and override run method with actual
  # work your script will be doing
  class Script
    def initialize( name, cli, argv, argf )
      @name = name
      @cli = cli
      @argv = argv
      @argf = argf
    end

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

    # This is where all action happens. Should be defined by all Script implementations
    def run
    end

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

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

    def gets
      @cli.stdin.gets
    end

    # Outputs usage notes (and optional extended explanation), then exits with code 1
    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

    # Outputs error text, then exits with code 1
    def error( text )
      puts "Script #{@name} #{version} - Error: #{text}"
      exit 1
    end

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
my_scripts-0.1.15 lib/my_scripts/script.rb
my_scripts-0.1.14 lib/my_scripts/script.rb
my_scripts-0.1.13 lib/my_scripts/script.rb
my_scripts-0.1.12 lib/my_scripts/script.rb
my_scripts-0.1.11 lib/my_scripts/script.rb
my_scripts-0.1.10 lib/my_scripts/script.rb
my_scripts-0.1.9 lib/my_scripts/script.rb
my_scripts-0.1.8 lib/my_scripts/script.rb
my_scripts-0.1.7 lib/my_scripts/script.rb