module Snooper::Options

Constants

ParsedOptions

Public: Command Line Options

Public Class Methods

parse(arguments) click to toggle source

Public: Parse the command line

arguments - The list of string arguments to be parsed

Returns an Options struct containing :base_path and :command

# File lib/snooper/options.rb, line 21
    def self.parse(arguments)
      
      helptext = <<END

Snooper is  a lightweight test  automation tool,  it monitors files  and folders
while  you work  and  re-runs  your tests  when  you  change something.  Snooper
doesn't care what language you're using  or what framework you are testing with,
it's all configureable.

For more information see snooper(1).
END

      options = ParsedOptions.new
      options.config_path = nil

      parser = OptionParser.new do |parser|
        parser.banner =
          "Useage: #{File.basename __FILE__} [--config <CONFIG> | --help] " + 
          "[<COMMAND>]*"

        parser.separator helptext

        parser.on '-c', '--config CONFIGFILE', 'YAML configuration file' do |path|
          options.config_path = path
        end

        parser.on("--version", "show version information") do
          puts "Snooper v#{VERSION}"
          exit
        end
        
        parser.on("-p", "--poll [FREQUENCY]", "Force filesystem polling.") do |freq|
          options.poll = freq || true
        end

        parser.on("-h", "--help", "Show this message") do
          puts parser
          exit
        end
      end

      # Parse the arguments
      begin
        parser.parse! arguments
      rescue OptionParser::InvalidOption, \
        OptionParser::MissingArgument, \
        OptionParser::InvalidArgument => e
        puts e
        puts parser
        exit 1
      end

      options.config_path = find_config unless options.config_path

      options.command = arguments.join " " if not arguments.empty?
      options
    end

Private Class Methods

find_config() click to toggle source

Internal : Find a config file to use

Returns the path to the config, or nil if none can be found

# File lib/snooper/options.rb, line 85
def self.find_config
  file_name = '.snooper.yaml'

  Pathname.pwd.ascend do |subpath|
    location = subpath + file_name
    return location if location.exist?
  end

  nil
end