Sha256: 8ce25646f8f1fb1de0a8f738cef97499569b0fd62a230a821892106c1bad0424

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 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

4 entries across 4 versions & 1 rubygems

Version Path
my_scripts-0.0.13 lib/my_scripts/cli.rb
my_scripts-0.0.12 lib/my_scripts/cli.rb
my_scripts-0.0.11 lib/my_scripts/cli.rb
my_scripts-0.0.9 lib/my_scripts/cli.rb