Class: Elscripto::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/elscripto/options.rb

Constant Summary collapse

VALID_COMMANDS =
%W[init start]

Class Method Summary collapse

Class Method Details

.parse(command, args) ⇒ Object

A customized OptionsParser



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
# File 'lib/elscripto/options.rb', line 13

def self.parse(command,args)
  # The options specified on the command line will be collected in *options*.
  # We set default values here.
  options = OpenStruct.new
  options.verbose = false
  options.path = FileUtils.pwd
  options.command = command
  options.config_file = './.elscripto'
  options.commands = options.definitions = []
  options.enviroment = :production
  opts = OptionParser.new do |opts|
    opts.program_name = 'elscripto'
    opts.banner = "Usage: #{opts.program_name} init|start [options]".green
    opts.separator ""
    opts.separator "Options:".green
    
    opts.on('-p','--path PATH',"Set elscripto's working directory") do |path|
      options.path = path
    end
    
    opts.on('-c','--commands CMD1;CMD2;CMD3...','Pass a list of comma-separated commands') do |cmds|
      options.commands = cmds.split(';').map(&:strip)
    end
    
    opts.on('-d','--definitions DEF1;DEF2;DEF3...','Pass a list of comma-separated command definitions') do |defs|
      options.definitions = defs.split(';').map(&:strip)
    end
    
    opts.on('-f','--file CONFIG FILE PATH','Pass a path to a config file') do |conf_file|
      options.config_file = conf_file
    end
    
    opts.on('-e','--enviroment [production|development]','Define runtime enviroment') do |env|
      options.enviroment = env.to_sym
    end
  end
  
  opts.separator ""
  opts.separator "Common options:".green
  
  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
  
  opts.on_tail("-v", "--version", "Show version") do
    puts "#{opts.program_name} #{Elscripto::VERSION}\n(c) 2013, Achillefs Charmpilas".green
    exit
  end
  
  opts.parse!(args)
  raise Elscripto::InvalidCommandError, "Please specify a valid command [#{VALID_COMMANDS.join('|')}]" unless VALID_COMMANDS.include?(options.command)
  options
end