class Macros4Cuke::CLI::CmdLine

Manages the application command-line interface (CLI).
It is merely a thin wrapper around the OptionParser library.
Responsibilities:
Examples of command lines:
--setup PROJ_PATH
--suggest

Constants

ShortHelpMsg

Attributes

options[R]

A Hash with the result of the command-line parse.

parser[R]

OptionParser object

Public Class Methods

new() click to toggle source

Constructor.

# File lib/macros4cuke/cli/cmd-line.rb, line 36
  def initialize()
    @options = {}

    @parser = OptionParser.new do |opts|
      opts.banner = <<-EOS
Usage: macros4cuke [options]
The command-line options are:
EOS
      # Mandatory argument
      msg_p1 = 'Make the Cucumber project at given path '
      msg_p2 = 'ready for macro-steps.'
      opts.on('--setup PROJECT_PATH', msg_p1 + msg_p2) do |project_path|
        valid_path = validated_feature_path(project_path)
        options[:setup] ||= []
        options[:setup] << valid_path 
      end

      # No argument, shows at tail.  This will print an options summary.
      opts.on_tail('-h', '--help', 'Show this message') do
        # puts opts
        options[:help] = true
      end

      opts.on_tail('-v', '--version', 'Display version number.') do
        puts Macros4Cuke::Version
        options[:version] = true
      end
      
      version_verbose_msg = 'Display gem and platform version numbers.'
      opts.on_tail('-V', '--version-verbose', version_verbose_msg) do
        cuke = "Cucumber #{Cucumber::VERSION}" 
        ruby = "Ruby #{RUBY_VERSION} #{RUBY_PLATFORM}"
        msg = "#{Macros4Cuke::Version} (using #{cuke}, running on #{ruby})"
        puts msg
        options[:version] = true
      end 
    end
  end

Public Instance Methods

parse!(theCmdLineArgs) click to toggle source

Perform the command-line parsing

# File lib/macros4cuke/cli/cmd-line.rb, line 76
def parse!(theCmdLineArgs)
  begin
    parser.parse!(theCmdLineArgs.dup) 
  rescue Macros4Cuke::CmdLineError => e
    $stderr.puts e.message
    exit
  rescue OptionParser::InvalidOption => e
    $stderr.puts e.message
    exit
  rescue OptionParser::MissingArgument => e
    err_msg = +''
    e.args.each do |arg|
      err_msg << "No argument provided with command line option: #{arg}\n"
    end
    $stderr.puts err_msg
    exit
  end
  
  # When no option provided then display minimalistic help info
  short_help if options.empty?
  
  show_help if options[:help]

  # Some options stop the application
  exit if options[:version] || options[:help]
  
  return options
end

Private Instance Methods

short_help() click to toggle source
# File lib/macros4cuke/cli/cmd-line.rb, line 126
def short_help()
  puts ShortHelpMsg
  exit
end
show_help() click to toggle source
# File lib/macros4cuke/cli/cmd-line.rb, line 122
def show_help()
  puts parser.help
end
validated_feature_path(theProjectPath) click to toggle source

Given the project path, retrieve its /features dir.

# File lib/macros4cuke/cli/cmd-line.rb, line 108
def validated_feature_path(theProjectPath)
  dirs = [theProjectPath, 'features', 'support']
  feature_path = dirs.reduce(Pathname.getwd) do |path, dir_name|
    path += dir_name
    unless path.exist?
      raise DirectoryNotFound.new(path.relative_path_from(Pathname.getwd))
    end
    
    path
  end

  return feature_path
end