Class: Templater::CLI::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/templater/cli/parser.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.error_message(e) ⇒ Object



78
79
80
# File 'lib/templater/cli/parser.rb', line 78

def self.error_message(e)
  "One or more unknown options given: #{e.args.join(', ')}"
end

.parse(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
74
75
76
# File 'lib/templater/cli/parser.rb', line 10

def self.parse(args)
  # The options specified on the command line will be collected in *options*.
  # We set default values here.
  options = {}
  options[:pretend] = false
  options[:force] = false
  options[:skip] = false
  options[:quiet] = false
  options[:verbose] = false
  options[:help] = false
  options[:version] = false

  opts = OptionParser.new do |opts|

    opts.banner = ""

    if block_given?
      yield opts, options
      opts.separator ""
    end

    opts.separator "General options:"

    opts.on("-p", "--pretend", "Run, but do not make any changes.") do |s|
      options[:pretend] = s
    end

    opts.on("-f", "--force", "Overwrite files that already exist.") do |s|
      options[:force] = s
    end

    opts.on("-s", "--skip", "Skip files that already exist.") do |s|
      options[:skip] = s
    end
  
    opts.on("-d", "--delete", "Delete files that have previously been generated with this generator.") do |s|
      options[:delete] = s
    end
  
    opts.on("--no-color", "Don't colorize the output") do
      options[:no_color] = true
    end

    opts.on("-h", "--help", "Show this message") do
      options[:help] = true
    end
    
    opts.on("--debug", "Do not catch errors") do
      options[:debug] = true
    end

  end

  def opts.inspect; "<#OptionParser #{object_id}>"; end

  options[:opts] = opts

  begin
    opts.parse!(args)
  rescue OptionParser::InvalidOption => e
    puts
    puts error_message(e)
    puts
  end

  options
end