Module: Dindi::CommandParser

Defined in:
lib/dindi/command_parser.rb

Class Method Summary collapse

Class Method Details

.parse(args, options) ⇒ 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
# File 'lib/dindi/command_parser.rb', line 10

def self.parse(args, options)
  # The options specified on the command line will be collected in 
  # *options*.
  #
  # We set the default value here
  options.project_name = nil
  options.with_bundle_install = false

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: dindi [options]"

    opts.separator " "
    opts.separator "Specific options:"

    opts.on("-n", "--name PROJECT_NAME",
            "Specify your project name PROJECT_NAME that will be created") do |project_name|
      options.project_name = project_name.downcase
    end

    opts.on("-b", "--[no-]bundle-install", "Run bundler install after project creation") do |b|
      options.with_bundle_install = b
    end

    # No argument, shows at tail. This will print an options summary.
    # Try it and see!
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      puts ""
      raise
    end

  end

  opts.parse!(args)

  options.ruby_version = "latest"

  # set project absolute dir
  options.project_absolute_dir = nil
  if options.project_name
    options.project_absolute_dir = File.join(FileUtils.pwd, options.project_name)
  end

rescue Exception => e
  if e.message.match(/invalid option/i) or e.message.match(/missing argument/i)
    puts "ERROR: #{e.message}".red
    puts ""
    puts opts
    puts ""
  end
  raise
end