Module: Rbdock::Command::Options

Defined in:
lib/rbdock/command/options.rb

Class Method Summary collapse

Class Method Details

.check_version_avaiable(argv) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/rbdock/command/options.rb', line 73

def self.check_version_avaiable argv
  argv.each do |v|
    if not Rbdock::Generate.ruby_versions.include? v
      raise ArgumentError, "Definition not found: #{v} \n\nYou can list all available ruby versions with `rbdock --list'."            
    end
  end
end

.create_command_parser(options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rbdock/command/options.rb', line 27

def self.create_command_parser options
  OptionParser.new do |opt|
    show_version opt
    show_help opt

    opt.on('-i name','--image=name', 'Set image name(ubuntu|centos)') { |v|
      options[:image] = v
    }
    opt.on('--rbenv', desc='Use rbenv') {
      options[:use_rbenv] = true
    }
    opt.on('--rvm',   desc='Use rvm') {
      options[:use_rvm] = true
    }
    opt.on('-a path','--app=path', 'Add Rails/Sinatra app') { |path|
      options[:app] = path
    }         
    opt.on('-l','--list', 'List all available ruby versions') {
      list_ruby_versions
    }          
  end
end

.list_ruby_versionsObject



81
82
83
84
85
86
# File 'lib/rbdock/command/options.rb', line 81

def self.list_ruby_versions
  STDERR.print "Available versions:\n   "
  STDERR.print Rbdock::Generate.ruby_versions.join("\n   ")
  STDERR.puts
  exit
end

.parse!(argv) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rbdock/command/options.rb', line 7

def self.parse!(argv)
  options = {}

  set_default_value options
  command_parser = create_command_parser options
  
  begin
    command_parser.parse!(argv)          
    ruby_version_perser options, argv
  rescue OptionParser::MissingArgument, OptionParser::InvalidOption, ArgumentError => e
    abort e.message
  end
  
  options
end

.ruby_version_perser(options, argv) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
71
# File 'lib/rbdock/command/options.rb', line 66

def self.ruby_version_perser options, argv
  raise ArgumentError, "#{options[:command]} ruby versions not found." if argv.empty?
  check_version_avaiable argv
  options[:ruby_versions] = argv
  options
end

.set_default_value(options) ⇒ Object



23
24
25
# File 'lib/rbdock/command/options.rb', line 23

def self.set_default_value options
  options[:image] = "ubuntu"
end

.show_help(opt) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/rbdock/command/options.rb', line 58

def self.show_help opt
  opt.banner   = "Usage: #{opt.program_name} <ruby-versions> [<args>]"
  opt.on_tail('-h','--help','Show this message') {
    STDERR.puts opt.help
    exit              
  }
end

.show_version(opt) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/rbdock/command/options.rb', line 50

def self.show_version opt        
  opt.on_tail('-v','--version','Show version') {
    opt.version = Rbdock::VERSION
    STDERR.puts opt.ver
    exit              
  }        
end