Sha256: 1c5bacce254705d9300669941f4814fe754daf4d8107489e5e080c96e4a1dfdb

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 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, argv, cli )
      @name = name
      @argv = argv
      @cli = cli
    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

2 entries across 2 versions & 1 rubygems

Version Path
my_scripts-0.1.5 lib/my_scripts/script.rb
my_scripts-0.1.3 lib/my_scripts/script.rb