Class: Orchestrator::Cli

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

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



9
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
# File 'lib/orchestrator/cli.rb', line 9

def self.parse(args)
  options = OpenStruct.new
  options.args = Object.new

  parser = OptionParser.new
  parser.banner = "Usage: #{$0} [options]"

  options.config = Orchestrator::Settings
  parser.on( '--config [PATH]', 'Path to settings yaml' ) do |config|
    options.config = config
    options.args.instance_variable_set(:@config,config)
  end

  options.statefile = nil
  options.name = nil
  parser.on( '--name NAME', 'Name of the cron job to run' ) do |name|
    options.name = name
    options.args.instance_variable_set(:@name,name)
  end

  parser.on( '--statefile PATH', 'Path to state file yaml' ) do |statefile|
    options.statefile = statefile
    options.args.instance_variable_set(:@statefile,statefile)
  end

  options.reset = false
  parser.on( '--reset', 'Do not use state file if it exists' ) { |reset| options.reset = true }

  parser.on( '--args ARGS,', 'extra args for interpolation as arg1=val1[,arg2=val2[]]' ) do |a|
    a.split(',').each do |x|
      arg,val = x.split('=')
      options.args.instance_variable_set("@#{arg}".to_sym,val)
    end
  end

  options.verbose = false
  parser.on( '--verbose') { options.verbose = true }

  options.email = true
  parser.on( '--no-email') { options.email = false }

  options.sms = true
  parser.on( '--no-sms') { options.sms = false }

  parser.on( '-h', '--help', 'Display this screen' ) { puts parser; exit }

  parser.parse!(args)

  unless options.name
    puts opts
    exit 1 
  end

  options
end