Sha256: e40e06e4adec01069a65504613db786e2074551b22cbca95cee9121e11513a68

Contents?: true

Size: 1.15 KB

Versions: 6

Compression:

Stored size: 1.15 KB

Contents

module MyScripts
  # 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
  class CLI
    class ScriptNameError < NameError  # :nodoc:
      def initialize(message=nil)
        message ? super(message) : super
      end
    end

    attr_accessor :stdout, :stdin

    # Instantiates new CLI(command line interface) and runs script with given name (token)
    # and argv inside new CLI instance
    def self.run( script_name, argv )
      new.run script_name, argv
    end

    # Creates new command line interface
    def initialize( stdin=$stdin, stdout=$stdout )
      @stdin = stdin
      @stdout = stdout
    end

    # Runs a script with given name (token) and argv inside this CLI instance
    def run( script_name, argv )
      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, argv, self).run
    end

    private

    def script_class_name(script_name)
      "MyScripts::#{script_name.to_s.camel_case}"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
my_scripts-0.1.0 lib/my_scripts/cli.rb
my_scripts-0.0.24 lib/my_scripts/cli.rb
my_scripts-0.0.23 lib/my_scripts/cli.rb
my_scripts-0.0.22 lib/my_scripts/cli.rb
my_scripts-0.0.19 lib/my_scripts/cli.rb
my_scripts-0.0.17 lib/my_scripts/cli.rb