Class: Macros4Cuke::CLI::CmdLine

Inherits:
Object
  • Object
show all
Defined in:
lib/macros4cuke/cli/cmd-line.rb

Overview

--suggest

Constant Summary collapse

ShortHelpMsg =
<<-END_MSG
For help about the command-line syntax, do:
macros4cuke --help
END_MSG

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCmdLine

Constructor.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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

Instance Attribute Details

#optionsObject (readonly)

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



30
31
32
# File 'lib/macros4cuke/cli/cmd-line.rb', line 30

def options
  @options
end

#parserObject (readonly)

OptionParser object



33
34
35
# File 'lib/macros4cuke/cli/cmd-line.rb', line 33

def parser
  @parser
end

Instance Method Details

#parse!(theCmdLineArgs) ⇒ Object

Perform the command-line parsing



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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